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#?
on 2012-07-26 08:13
on 2012-07-27 20:09
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#?
on 2012-07-27 20:51
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?
on 2012-08-03 08:39
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.@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@gmail.com | Blog:
http://IronShay.com<http://ironshay.com/> |
Twitter: http://twitter.com/ironshay
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.