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/.
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
Ivan Porto Carrero
2009-Jun-09 20:39 UTC
[Ironruby-core] How to fire IronRuby Method from C#
var operations = engine.CreateOperations(); operations.InvokeMember(rubyPerson, "greet"); http://github.com/casualjim/ironrubymvc/blob/ef59166eb4e5e6b06b9d90abf95d637c2126e5ed/IronRubyMvc/Core/RubyEngine.cs#L116 --- Met vriendelijke groeten - Best regards - Salutations Ivan Porto Carrero Blog: http://flanders.co.nz Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Tue, Jun 9, 2009 at 10:20 PM, Mohammad Azam <lists at ruby-forum.com> wrote:> 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090609/9d5ff2c4/attachment.html>
Awesome thanks! -- Posted via http://www.ruby-forum.com/.
Thanks Tom! I wonder why dynamic type variable in C# 4.0 is not able to translate the RubyClass and trigger the greet() method. -- Posted via http://www.ruby-forum.com/.
Is there any special way for calling the ExecuteFile method: engine.ExecuteFile("hello.rb",engine.CreateScope()); And here is the hello.rb file: class Person def greet() puts ''hello world'' end end def getPerson() return Person.new end getPerson() -- Posted via http://www.ruby-forum.com/.
Curt Hagenlocher
2009-Jun-10 02:19 UTC
[Ironruby-core] How to fire IronRuby Method from C#
If this is C# 4 (as you''ve suggested in another email), you should be able to say object personClass = engine.Runtime.Globals.GetVariable("Person"); dynamic person = engine.ObjectOperations.CreateInstance(personClass); person.greet(); -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Tomas Matousek Sent: Tuesday, June 09, 2009 1:40 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] How to fire IronRuby Method from C# 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 _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Awesome! I will try it out! -- Posted via http://www.ruby-forum.com/.
Curt Hagenlocher wrote:> If this is C# 4 (as you''ve suggested in another email), you should be > able to say > > object personClass = engine.Runtime.Globals.GetVariable("Person"); > dynamic person = engine.ObjectOperations.CreateInstance(personClass); > person.greet();I tried and it did not work out. First engine does not have ObjectOperations. There is engine.Operations which I used but I get the error when doing person.greet(). Error 5 Missing compiler required member ''Microsoft.CSharp.RuntimeBinder.CSharpBinaryOperationBinder..ctor'' ConsoleApplication1 Error 73 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? C:\Documents and Settings\AzamSharp\My Documents\Visual Studio 10\Projects\testvs2010\ConsoleApplication1\Program.cs 80 13 ConsoleApplication1 -- Posted via http://www.ruby-forum.com/.
Curt Hagenlocher
2009-Jun-10 03:29 UTC
[Ironruby-core] How to fire IronRuby Method from C#
Yes, sorry, you''re right -- it''s Engine.Operations (but the name of the class is "ObjectOperations"). The error message tells you exactly what you need to do! Make sure you''ve added references to Microsoft.CSharp.dll and System.Core.dll to your project. -----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 7:31 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] How to fire IronRuby Method from C# Curt Hagenlocher wrote:> If this is C# 4 (as you''ve suggested in another email), you should be > able to say > > object personClass = engine.Runtime.Globals.GetVariable("Person"); > dynamic person = engine.ObjectOperations.CreateInstance(personClass); > person.greet();I tried and it did not work out. First engine does not have ObjectOperations. There is engine.Operations which I used but I get the error when doing person.greet(). Error 5 Missing compiler required member ''Microsoft.CSharp.RuntimeBinder.CSharpBinaryOperationBinder..ctor'' ConsoleApplication1 Error 73 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? C:\Documents and Settings\AzamSharp\My Documents\Visual Studio 10\Projects\testvs2010\ConsoleApplication1\Program.cs 80 13 ConsoleApplication1 -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Curt Hagenlocher wrote:> Yes, sorry, you''re right -- it''s Engine.Operations (but the name of the > class is "ObjectOperations"). > > The error message tells you exactly what you need to do! Make sure > you''ve added references to Microsoft.CSharp.dll and System.Core.dll to > your project.The error is now gone! but I got the new error which says: RubyObject does not contain the definition for greet! object personClass = engine.Runtime.Globals.GetVariable("Person"); dynamic person = engine.Operations.CreateInstance(personClass); person.greet(); -- Posted via http://www.ruby-forum.com/.
Curt Hagenlocher
2009-Jun-10 17:38 UTC
[Ironruby-core] How to fire IronRuby Method from C#
Odd. It works for me. Here''s my complete code: namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var engine = IronRuby.Ruby.CreateEngine(); engine.Execute(@" class Person def greet() puts ''hello world'' end end "); object personClass = engine.Runtime.Globals.GetVariable("Person"); dynamic person = engine.Operations.CreateInstance(personClass); person.greet(); System.Console.ReadLine(); } } } -----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 8:37 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] How to fire IronRuby Method from C# Curt Hagenlocher wrote:> Yes, sorry, you''re right -- it''s Engine.Operations (but the name of the > class is "ObjectOperations"). > > The error message tells you exactly what you need to do! Make sure > you''ve added references to Microsoft.CSharp.dll and System.Core.dll to > your project.The error is now gone! but I got the new error which says: RubyObject does not contain the definition for greet! object personClass = engine.Runtime.Globals.GetVariable("Person"); dynamic person = engine.Operations.CreateInstance(personClass); person.greet(); -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
This is very strange! I copy pasted your code and it is giving me compile time errors: Here is the code: var rubyPerson = (engine.Execute(@" require ''open-uri'' class Person def greet() puts ''hello world'' end end Person.new() ")); object personClass = engine.Runtime.Globals.GetVariable("Person"); dynamic person = engine.Operations.CreateInstance(personClass); person.greet(); And here is the error: Error 1 Missing compiler required member ''Microsoft.CSharp.RuntimeBinder.CSharpBinaryOperationBinder..ctor'' ConsoleApplication1 Error 61 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? C:\Documents and Settings\AzamSharp\My Documents\Visual Studio 10\Projects\testvs2010\ConsoleApplication1\Program.cs 56 12 ConsoleApplication1 I have already added references to both the dll libraries. -- Posted via http://www.ruby-forum.com/.
Are you sure you have the appropriate version of IronRuby? There is a special build just for .NET 4.0. I was getting an error like the one you are until I used the .NET 4.0 build here: http://ironruby.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27606 Mohammad Azam wrote:> This is very strange! I copy pasted your code and it is giving me > compile time errors:> Error 1 Missing compiler required member > ''Microsoft.CSharp.RuntimeBinder.CSharpBinaryOperationBinder..ctor'' > ConsoleApplication1 > > Error 61 One or more types required to compile a dynamic expression > cannot be found. Are you missing references to Microsoft.CSharp.dll and > System.Core.dll? C:\Documents and Settings\AzamSharp\My > Documents\Visual Studio > 10\Projects\testvs2010\ConsoleApplication1\Program.cs 56 12 > ConsoleApplication1 > > > I have already added references to both the dll libraries.-- Posted via http://www.ruby-forum.com/.