I''m using IronRuby 1.1.3 and .Net 4. In IronRuby I''ve got two files: 1) Module.rb # This file contains a class with static method GetObjectHash. The idea is to parse the object find all it''s properties and make a hash like: {property_name => [property_type, property_value], ...} 2) Main.rb # This file creates several objects from different classes and calls GetObjectHash method. Then I try to execute Main.rb using C#: string path = "../../RubyTest.rb"; var runtime = IronRuby.Ruby.CreateRuntime(); var engine = runtime.GetEngine("rb"); engine.ExecuteFile(path); Ok, it executes. And if in Main.rb I write the last command as hashString = ModuleClass.GetObjectHash(testObject).to_s print hashString it prints into the console the hash I want to see. But. I''d like not to use the last "print" command, and get the hashString variable into C# where I can use it as normal string. I tried to do that like: dynamic netString; engine.ExecuteFile(path).TryGetVariable("hashString", out netString); string hash = netString as String; Console.WriteLine(hash); But hashString remained null. (Variable name is ok, I checked it). Am I doing something wrong? How can I get "hashString" with stored data into C#? -- Posted via http://www.ruby-forum.com/.
Hi Alexander, did you try to convert the strings in the hash (which are a special type of string called mutablestring) to a System::String. I usually use: a = "This is A String" print a.class # => String print a.to_clr_string # => System::String (which is the standard .net string type) I would use: hashString = ModuleClass.GetObjectHash(testObject).to_s.to_clr_string (now hashString is a Standard C# string) Alexander Ranger wrote in post #1070241:> I''m using IronRuby 1.1.3 and .Net 4. > > In IronRuby I''ve got two files: > 1) Module.rb > # This file contains a class with static method GetObjectHash. The idea > is to parse the object find all it''s properties and make a hash like: > {property_name => [property_type, property_value], ...} > > 2) Main.rb > # This file creates several objects from different classes and calls > GetObjectHash method. > > Then I try to execute Main.rb using C#: > > string path = "../../RubyTest.rb"; > var runtime = IronRuby.Ruby.CreateRuntime(); > var engine = runtime.GetEngine("rb"); > engine.ExecuteFile(path); > > Ok, it executes. And if in Main.rb I write the last command as > > hashString = ModuleClass.GetObjectHash(testObject).to_s> print hashString > > it prints into the console the hash I want to see. But. I''d like not to > use the last "print" command, and get the hashString variable into C# > where I can use it as normal string. > > I tried to do that like: > > dynamic netString; > engine.ExecuteFile(path).TryGetVariable("hashString", out netString); > string hash = netString as String; > Console.WriteLine(hash); > > But hashString remained null. (Variable name is ok, I checked it). > > Am I doing something wrong? How can I get "hashString" with stored data > into C#?-- Posted via http://www.ruby-forum.com/.
Thanks for your answer, Eduardo. I''ve done it as you adviced: hashString = ModuleClass.GetObjectHash(testObject).to_s.to_clr_string # in IronRuby code and tried to call it in C#: string netString; engine.ExecuteFile(path).TryGetVariable("hashString", out netString); Console.WriteLine(netString); Well, there are no mistakes in compiling Ruby code, but the netString is null after executing the ruby file. I guess that the hashString after executing just seems to be empty. Is it something wrong with calling it in C# or is it just expected to be so? -- Posted via http://www.ruby-forum.com/.
Why not use the ModuleClass directly from C#? For example, if your ruby file looks like that: class ModuleClass def getObjectHash(hashString) return hashString.to_s.to_clr_string endend Then in C# you''ll use it as follows: var engine = IronRuby.Ruby.CreateEngine(); var scope engine.ExecuteFile("module_class.rb"); dynamic globals = scope.Engine.Runtime.Globals;dynamic module globals.ModuleClass. at new();string s = module.getObjectHash("yo yo");Console.WriteLine(s ?? "NULL"); Shay. -------------------------------------------------------- Shay Friedman | CodeValue <http://codevalue.net/> Co-Founder, Dynamic Languages and Web Technologies Expert | Microsoft Visual C# MVP | Author of IronRuby Unleashed Email: shay.friedman at gmail.com | Blog: http://IronShay.com<http://ironshay.com/> | Twitter: http://twitter.com/ironshay On Fri, Jul 27, 2012 at 9:51 PM, Alexander Ranger <lists at ruby-forum.com>wrote:> Thanks for your answer, Eduardo. > > I''ve done it as you adviced: > > hashString = ModuleClass.GetObjectHash(testObject).to_s.to_clr_string > # in IronRuby code > > and tried to call it in C#: > > string netString; > engine.ExecuteFile(path).TryGetVariable("hashString", out netString); > Console.WriteLine(netString); > > Well, there are no mistakes in compiling Ruby code, but the netString is > null after executing the ruby file. > > I guess that the hashString after executing just seems to be empty. Is > it something wrong with calling it in C# or is it just expected to be > so? > > -- > 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/20120803/61669577/attachment.html>