Busy Day with IronRuby

OK, so I couldn’t get some of the simple examples of using RubyEngine
to start calling Ruby from C#…

So I spent a bit reading through the mailing list archives (realizing
while doing that, that I’d asked a frequently asked question).

Now, I hope I’m not asking another stupid question.

I found another post (
http://rubyforge.org/pipermail/ironruby-core/2008-January/000616.html
) that suggests a method of running Ruby.

However, I get an error on this line

        IScriptEngine engine = IronRuby.GetEngine(runtime);

Which shows this stack trace

at System.RuntimeMethodHandle._InvokeConstructor(Object[] args,
SignatureStruct& signature, IntPtr declaringType)
at System.RuntimeMethodHandle.InvokeConstructor(Object[] args,
SignatureStruct signature, RuntimeTypeHandle declaringType)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags
bindingAttr, Binder binder, Object[] args, CultureInfo culture,
Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Microsoft.Scripting.Utils.ReflectionUtils.CreateInstance[T](Type
actualType, Object[] args) in
c:\Libraries\IronRuby\trunk\src\microsoft.scripting\Utils\ReflectionUtils.cs:line
127


Greg A.
Software Development Manager
SSI Services

http://kc.vanadium.com
http://www.pghcodingdojo.org
http://www.insomnia-consulting.org/monologue

hello_world.rb only contains: puts “Hello, World!!!”

hth
Ivan

Hi Greg,

This is what i used at some point, that should still work atm.

class Program
{
static void Main(string[] args)
{
//TODO: review this when the hosting api changes.
IScriptEnvironment runtime =
ScriptEnvironment.GetEnvironment();

        Console.WriteLine("Executing from file:");
        runtime.ExecuteFile("..\\..\\hello_world.rb");

        IScriptEngine engine = IronRuby.GetEngine(runtime);
        IScriptScope scope = engine.CreateScope();

        List<string> strings = new List<string>();
        strings.Add("Hello, ");
        strings.Add("World!!!");
        IronRuby.GetExecutionContext(runtime).GlobalVariables[

SymbolTable.StringToId(“strings”)] = strings;

        SourceUnit script = engine.CreateScriptSourceFromString(

@"
puts ““Hello World! There are #{$strings.count.to_s} strings:””
$strings.each_with_index { |s,i| puts “”#{i}: #{s}"" }
");

        Console.WriteLine("Executing from string:");
        engine.Execute(scope, script);
    }
}

I’m actually getting ready to work on an IronRuby quick start blog
post, based on my experiences, but this is the simplest “hello world”
using the hosting mechanism I could come up with:

using System;
using Ruby.Runtime;
using Microsoft.Scripting.Hosting;
namespace RubyExample
{
static class Program
{
///


/// The main entry point for the application.
///

[STAThread]
static void Main()
{
IScriptEnvironment scriptenvironment =
ScriptEnvironment.GetEnvironment();
IScriptEngine rubyengine =
scriptenvironment.GetEngine(“ruby”);
scriptenvironment.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(“puts
‘Hello World!\nPress Any Key To Continue…’”) );
Console.ReadKey();

    }
}

}

be sure to add references to Microsof.Scripting.dll and IronRuby.dll
(and IronRuby.Libraries.dll if you’re so inclined).

You can also use scriptenvironment.ExecuteFile(pathtofile) if you have
your ruby script inn a standalone file.

On Jan 27, 2008 6:16 PM, Greg A. [email protected] wrote:

) that suggests a method of running Ruby.
SignatureStruct signature, RuntimeTypeHandle declaringType)
actualType, Object[] args) in
http://www.insomnia-consulting.org/monologue


Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core


Michael L.
[Polymath Programmer]
http://michaeldotnet.blogspot.com

Thanks to both of you. I’ll try those approaches.

Incidentally, the line which threw my error was in
Microsoft.Scripting.Hosting.PlatformAdaptationLayer at line 90

#if !SILVERLIGHT
return Assembly.Load(name); <== ERROR OCCURRED ON THIS
LINE while “name” == “IronRuby.Libraries”
#else
return Assembly.Load(LookupFullName(name));
#endif

On Jan 27, 2008 6:34 PM, Michael L. [email protected]
wrote:

{

‘Hello World!\nPress Any Key To Continue…’") );
your ruby script inn a standalone file.

at System.RuntimeMethodHandle._InvokeConstructor(Object[] args,
Object[] activationAttributes)


Michael L.
[Polymath Programmer]
http://michaeldotnet.blogspot.com


Greg A.
Software Development Manager
SSI Services

http://kc.vanadium.com
http://www.pghcodingdojo.org
http://www.insomnia-consulting.org/monologue

Success!!

I think a traumatic experience a few months ago has caused the part of
my brain which deals with Assembly loading to stop working :wink:

Obviously, from reading my message (below) there is a problem loading
IronRuby.Libraries.

When I glanced back over Michael’s email, I realized that I HAVE to
reference IronRuby.Libraries.dll

Michael? Are you NOT referencing those?

On Jan 27, 2008 6:41 PM, Greg A. [email protected] wrote:

#endif

using System;
static void Main()
}

to start calling Ruby from C#…
However, I get an error on this line
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
127


http://michaeldotnet.blogspot.com
http://kc.vanadium.com
http://www.pghcodingdojo.org
http://www.insomnia-consulting.org/monologue


Greg A.
Software Development Manager
SSI Services

http://kc.vanadium.com
http://www.pghcodingdojo.org
http://www.insomnia-consulting.org/monologue

In case anyone is interested:
http://michaeldotnet.blogspot.com/2008/01/ironruby-quick-start.html

As far as I know that’s correct as of rev 75. Any mistakes or
omissions please let me know. (I’m still not sure about that SIGNED
symbol…)

On Jan 27, 2008 6:34 PM, Michael L. [email protected]
wrote:

{

‘Hello World!\nPress Any Key To Continue…’") );
your ruby script inn a standalone file.

at System.RuntimeMethodHandle._InvokeConstructor(Object[] args,
Object[] activationAttributes)


Michael L.
[Polymath Programmer]
http://michaeldotnet.blogspot.com


Michael L.
[Polymath Programmer]
http://michaeldotnet.blogspot.com

Hi Michael

I think we can work together for that, seen as I will have a bunch of
samples I’m writing for the book

I haven’t gotten round to putting a website/blog up because most of the
CMS/blog systems out there don’t do exactly what I want (which is not
much).
I want it to be ruby of course.

Email me off list if you like the idea?

Cheers
Ivan

On Jan 28, 2008 12:34 PM, Michael L. [email protected]