[Ironruby-core] How to fire IronRuby Method from C#

Tomas Matousek Tomas.Matousek at microsoft.com
Tue Jun 9 16:39:31 EDT 2009


This should work:

var engine = IronRuby.Ruby.CreateEngine();

engine.Execute(@"
class Person
  def greet()
    puts 'hello world'
  end
end
");

object personClass = engine.Runtime.Globals.GetVariable("Person");
object person = engine.ObjectOperations.CreateInstance(personClass);
engine.ObjectOperations.InvokeMember(person, "greet");

Tomas

-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Mohammad Azam
Sent: Tuesday, June 09, 2009 1:20 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] How to fire IronRuby Method from C#

Hi,

I am returning an IronRuby object to C#. It is returned as a RubyClass
object. I can debug and see that it also has greet() method attached to
it but I am having difficulty in calling the greet() method from C#.
Here is the code.

 var scriptingRuntime = IronRuby.Ruby.CreateRuntime();
            var engine = scriptingRuntime.GetEngine("rb");

            RubyObject rubyPerson = ((RubyObject)engine.Execute(@"
            class Person

            def greet()
            puts 'hello world'
            end

            end

            def getPerson()
            return Person.new
            end

            getPerson()
            "));

            RubyClass rubyPersonClass = rubyPerson.ImmediateClass;
-- 
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core



More information about the Ironruby-core mailing list