Question

To begin, here's the code of a ruby file that I set in bin/debug folder of my SharpDevelop project:

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

The code above allow to serialize the Employee_Info object(s) to a file. Let's note that I have installed IronRuby.

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.

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);
}

Finally, here's the ruby code of another ruby project set in another different ruby environment.

@my_foo = File.open("Data/employee_sheet.es", "rb") {|f| Marshal.load(f)}
@name = @my_foo[0].name

And of course, I also made sure to have the Employee_Info class available in that other environment.

class Employee_Info

    attr_accessor :id
    attr_accessor :name

    def initialize(id, name)
        @id = id
        @name = name
    end

end

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.

When I run the other environment, the program crash and produce this error: "ArgumentError occured. Undefined class/module System:: "

I made a few more tests and I realized that when I alter my above code to this (see line with the triple '*':

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

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?

Thank you!

Was it helpful?

Solution

I can't explain how and why, but I solved the problem this way:

class Run_Marshal

    def initialize(id, name)
        @list = []
        @list[0] = Employee_Info.new(id, name.to_s)

        File.open("employee_sheet.es", "wb") {|f| Marshal::dump(@list, f)}
    end
end

I added to_s to the passed string. Apparently Ruby didn't recognize the argument as a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top