Shay Friedman, author of IronRuby Unleashed (check out my review) kindly agreed to do a guest post on my blog.

He explores how IronRuby can be very useful to a .NET developer, even if that developer is a die hard C# or Visual Basic developer who has no plans (Surely not! ) to use IronRuby in finished products.

The power of an Interactive Console should not be underestimated – it can give you tremendous productivity gains, which is why I also recommend http://linqpad.net/ to offer similar behaviour if you want to stick with C# or VB. However I will leave it to Shay to convince you that all you really need is IronRuby. If you are even slightly convinced,  then please do check out my 5 Steps to getting Started with IronRuby.

This is Shay doing his Mission Impossible 2 party piece and his great book which you can order on Amazon:

shay120x120image

Practical IronRuby – The Interactive Console by Shay Friedman

IronRuby is a wonderful addition to the .NET world which opens an astonishing amount of new possibilities to .NET and Ruby developers.

These new possibilities go across all aspects of development, from tooling to core development. One of them, which I'd like to introduce to you today, is the IronRuby interactive console.

IronRuby is a REPL (Read Evaluate Print Loop) language. This means that instead of the conservative compiler, there is an interpreter. The interpreter goes through the code line by line, and executes each one immediately. As a result, it is possible to create an application that gets code as input and executes it instantly.

This is exactly what the IronRuby interactive console is.

The interactive console allows you to write Ruby code and see the result straightaway. For example, the next figure shows an IronRuby interactive console session that includes defining a variable (named num) and playing with strings:

image

Apart from just playing with Ruby syntax, the IronRuby interactive console can be used as a very powerful tool. For example, once a day (in average) I run into a situation when I need to check how a method reacts to specific parameters. There are a few solutions to that currently like looking for the documentation (which doesn't always help) or opening another Visual Studio window and create a simple test project. The problem with these methods is that they take time (at least a couple of minutes) and harms your line of thought. And it's just to test a single method.

The alternative I offer is to take advantage of the IronRuby interactive console. This can decrease the time you spend on the task to only a few seconds. For example, I never remember whether System.IO.Path.GetExtension() return-value contains a dot or not. To check that, all I need to do is:

1. Start -> Run -> cmd

image

2. Write ir and hit Enter

3. Write System::IO::Path.GetExtension('C:\test\d.txt') and hit Enter

4. See the return value (it contains a dot!):

image

5. Continue with your work.

In addition, you are not constrained to use only the base class library. You can also check the behavior of custom assemblies. For instance, assuming you have an assembly named "CustomAssembly.dll", you can load it using the require method:

require "CustomAssemblly.dll"

# Or using full path

require 'C:\path\to\assembly\CustomAssembly.dll"

As soon as the assembly is loaded, you can use its classes and execute its methods:

image

In conclusion, the IronRuby interactive console is a wonderful tool that can boost your productivity in a very simple way. And it is just a few minutes away - just download and install IronRuby and you have it.

Keep in mind that this is only one small feature of IronRuby and there is much more to it. You just need to try.

Related Links