Search code examples
consoleironruby

Load assemblies at ironruby console startup


I'm trying to load some default assemblies and init some variables with ironruby, and I don't know where to start.

What I'm trying to create is something similar to rails script/console: you run that script and get a console where all rails Classes are available, but using a couple of my own library.

The question is how can I start an IronRuby console with some assemblies loaded (required) by default?


Solution

  • If you want the console to preload assemblies, you'd have to use iirb and not ir (practically the same with a slightly different UI). This is, by the way, the tool that rails script/console uses.

    Preloading assemblies (or ruby modules) is done via the -r switch. For example, if you'd like to preload "MyAssembly.dll", run the next command:

    iirb -r "MyAssembly.dll"
    

    If you want to do several different operations like loading several assemblies and initializing some variables, I'd recommend writing an rb file that does all that. For example:

    require "MyAssembly.dll"
    include MyNamespace
    
    my_variable = "Hello!"
    ...
    

    Assuming this code file is named "init.rb", then call the iirb tool as follows:

    iirb -r "init.rb"
    

    You can then create a batch file that runs this command line to ease its use.

    P.S. you can use the --simple-prompt switch as well to get the same "UI" of the ir.exe console:

    iirb -r "init.rb" --simple-prompt