# Getting variable form IronRuby

**URL:** <https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690>\
**Category:** IronRuby\
**Created:** [July 26, 2012, 8:13am UTC](https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690 "2012-07-26T08:13:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 26, 2012, 8:13am UTC](https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690/1 "2012-07-26T08:13:07Z")

</div>

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], …}

1. 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#?

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [July 27, 2012, 8:09pm UTC](https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690/2 "2012-07-27T20:09:51Z")

</div>

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 R. 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], …}
> 
> 1. 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#?

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [July 27, 2012, 8:51pm UTC](https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690/3 "2012-07-27T20:51:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [August 3, 2012, 8:39am UTC](https://www.ruby-forum.com/t/getting-variable-form-ironruby/221690/4 "2012-08-03T08:39:18Z")

</div>

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 F. | CodeValue [http://codevalue.net/](http://codevalue.net/) Co-Founder, Dynamic  
Languages and Web Technologies Expert | Microsoft Visual C# MVP | Author  
of  
IronRuby Unleashed  
Email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) | Blog:  
[http://IronShay.com](http://IronShay.com)[http://ironshay.com/](http://ironshay.com/) |  
Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)
