Note that there are some explanatory texts on larger screens.

plurals
  1. POIronRuby, How to pass argument variables from C# to Ruby?
    primarykey
    data
    text
    <p>To begin, here's the code of a ruby file that I set in bin/debug folder of my SharpDevelop project:</p> <pre><code>class Run_Marshal def initialize(id, name) @list = [] @list[0] = Employee_Info.new(id, name) File.open("employee_sheet.es", "wb") {|f| Marshal::dump(@list, f)} end end class Employee_Info attr_accessor :id attr_accessor :name def initialize(id, name) @id = id @name = name end end </code></pre> <p>The code above allow to serialize the Employee_Info object(s) to a file. Let's note that I have installed IronRuby.</p> <p>Below is the C# code I'm using to execute the ruby code. It's important that I pass arguments from C# to my new Ruby objects. </p> <pre><code>void Btn_exportClick(object sender, EventArgs e) { var engine = Ruby.CreateEngine(); engine.ExecuteFile("Ruby_Classes.rb"); dynamic ruby = engine.Runtime.Globals; int id = 0; string name = "John Coles"; dynamic foo = ruby.Run_Marshal.@new(id, name); } </code></pre> <p>Finally, here's the ruby code of another ruby project set in another different ruby environment.</p> <pre><code>@my_foo = File.open("Data/employee_sheet.es", "rb") {|f| Marshal.load(f)} @name = @my_foo[0].name </code></pre> <p>And of course, I also made sure to have the Employee_Info class available in that other environment. </p> <pre><code>class Employee_Info attr_accessor :id attr_accessor :name def initialize(id, name) @id = id @name = name end end </code></pre> <p>The code run perfectly from my SharpDevelop project and it output a serialized (marshal) file in the bin/debug folder. I then take that serialized file and put it in another folder of another ruby environment. </p> <p>When I run the other environment, the program crash and produce this error: "ArgumentError occured. Undefined class/module System:: "</p> <p>I made a few more tests and I realized that when I alter my above code to this (see line with the triple '*':</p> <pre><code>class Run_Marshal def initialize(id, name) @list = [] @list[0] = Employee_Info.new(0, "Beettlejuice") // *** File.open("employee_sheet.es", "wb") {|f| Marshal::dump(@list, f)} end end </code></pre> <p>It now open the serialized file without any problem. So I suspect the problem is due to the argument passed from the C# code to the ruby code. I truly need to pass c# variables for more complex tasks but so far I have no idea how to make it works. I hoped you guys could explain what's wrong. So how to pass argument variables from C# to Ruby?</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload