Ironing code, geek t-shirts and even presentations!


C# Recorder using IronRuby

[This post is the second in my series of IronRuby samples. Read the first one here]

The release of Visual Studio 2010 Beta 2 and IronRuby .Net 4.0 Beta 2 CTP has brought some AMAZING abilities to the .Net world like the dynamic keyword. This keyword is a revolutionary little thing. It takes everything you know about C# and throws it away – explicit types, locating syntax errors in compilation time, compiled code…

Sounds bad? well, it is just AWESOME!!! The dynamic keyword brings so much goodness to our beloved C# language, that if it was possible I would have hugged it and asked it to join my family.

Well, enough with the nonsense, let’s get down to business. IronRuby is Microsoft’s implementation of the Ruby language. It runs on top of the DLR and provides a seamless integration with .Net code. In short, it ROCKZZZZZ. This post is about IronRuby’s seamless integration with .Net and the ability to use the great power of Ruby inside C#.

The Ruby language has some very powerful metaprogramming abilities. One of those is the method_missing method. When you declare it in your class, every call to a method that doesn’t exist will be routed to it. You can then do whatever you want with the call – execute a different method, raise an exception, interpret the call somehow or just do whatever you want (jump in the air? do your little Irish dance thing?).

Another nice metaprogramming feature is the ability to send requests to objects by using the send method. The concept is very similar to C#’s reflection method – Invoke.

Now if we combine method_missing and send, we can create a class that saves calls and playbacks them upon request. I will call it… tam tam tam… Recorder:


class Recorder
  # Initialize an array that will save the calls
  def initialize
    @calls = []
  # Save the calls to method_missing	
  def method_missing(name, *args, &block)
    @calls << [name, args, block]

  # Playback the calls on a given object	
  def playback(obj)
    @calls.each do |name, args, block|
      obj.send name, *args, &block


I think this code is pretty straight forward, no special things here. With this class defined, we can record Ruby calls and playback them on Ruby objects:


# Record calls
rec =
rec.insert 2, "ABAB"
rec.delete! "A"

# Playback them on a real object
str = "Hello World"
puts str # Prints "dlBBroW olleH" 



It is AWESOME, but the great thing about it is that with .Net 4.0 and the dynamic keyword, it is available in C# too!

To try the next code by yourself, first open Visual Studio 2010 Beta 2, create a new C# console application and add references to IronRuby.dll, IronRuby.Libraries.dll, Microsoft.Scripting.dll and Microsoft.Dynamic.dll (remember to use the CTP assemblies and not the regular IronRuby assemblies).

The following code loads the Ruby recorder class file (recorder.rb) to the C# environment, creates an instance of the Recorder class, records a few operations and playbacks them on .Net objects:


static void Main(string[] args) 
  // Load the recorder IronRuby file
  var engine = IronRuby.Ruby.CreateEngine();
  dynamic ruby = engine.Runtime.Globals;

  // Initialize IronRuby's recorder class
  dynamic recorder = ruby.Recorder.@new();

  // Record

  // Playback on CLR's List object
  List<int> list = new List<int>();

  // Print the results!
  foreach (var item in list)

  // Record console printing
  recorder = ruby.Recorder.@new();
  recorder.WriteLine(" and .Net 4.0");

  // Playback on console

The output to the console will be:
IronRuby and .Net 4.0

Try it out and see the magic happens right in front of your very own eyes!

In my opinion, this joint venture is incredibly helpful and useful. I predict that as time goes by we will see more and more dynamic language code make its way to the conservative .Net world, enhancing it and adding it powerful abilities that it never has had before.

All the best,

Share it: kick it on Shout it

IronRuby Sample #1 – Hello World

A bunch of readers have asked me to post IronRuby samples. I took your advice and I am starting a series of posts where I’ll write some IronRuby samples so you can see what’s IronRuby code is all about.

I’ll begin with the simplest sample and as the series goes on, I’ll try to bring you more complex samples. If you’d like to see a specific sample, please contact me and let me know about it.

So the first sample is a Hello World sample. The regular Ruby Hello World app, which of course runs on IronRuby as well, is as follows:

puts "Hello World"


Now, the great thing about IronRuby is the ability to use .Net classes. The next code also writes Hello World to the console, but this time it uses .Net’s System.Console class. The sample also sets the color of the text to green:

orig_color = System::Console.foreground_color

System::Console.foreground_color =
System::Console.write_line "Hello World"

System::Console.foreground_color = orig_color


To run that, just save this text into a file and execute it by ir.exe <file name>. This is how its execution will look like:

IronShay - IronRuby Sample #1

.Net developers pay attention - CLR objects get Ruby’s naming conventions when they are converted to IronRuby objects. This operation is called “name mangling”.  This is why System.Console.ForegroundColor appears as System::Console.foreground_color in IronRuby. You can read more about name mangling in the .Net Interoperability Fundamentals chapter in my book. These chapters are available for free through the Rough Cuts program.

If you’d like to see a certain sample next time, let me know.

All the best, 

Upcoming Session: Riding IronRuby on Rails at WDC, Israel

I’m glad to announce that another IronRuby session is getting closer, this time at the Israeli Web Developers Community!


Sunday, 1st of  November, 2009. 17:00-20:00.


Microsoft Offices, 2 HaPnina St., Rannana.


The session is all about IronRuby, with a focus on Ruby’s most famous member – Ruby on Rails. The session will  start with an overview of the Ruby language and its powerful abilities, continue with IronRuby and  the stuff it brings to your everyday work and end with a Ruby On Rails overview, including live demo of building a Web 2.0 application from scratch.


Because IronRuby can enhance your everyday work, speed up your development process and make it enjoyable and fun! In addition, you will see another web development framework, Ruby on Rails, which will show you another way of web development and will also give you an idea about where today’s web MVC frameworks got all their ideas and probably their future ones as well  :)
And most importantly, there will be free food and drinks!


Register to the event at: 

By the way, If you’d like me to come and present at your event (conference, user group, internal gatherings, courses…), please contact me.

Hope to see you there,

My Presentation at the Nes Ziona User Group

Yesterday I did a presentation at the Nes Ziona user group. My session was about IronRuby and my main goal was to show the audience what they can do with IronRuby in their everyday .Net work.

First of all, thanks everyone for coming! I had a lot of fun!

I discussed the following subjects:

  • Creating internal tools and POCs with IronRuby – taking advantage of the Ruby language abilities and libraries to write code faster.
  • Using Ruby REPL abilities in .Net apps – can be used for polishing, debugging and extending .Net applications.
  • Cucumber – the amazing Ruby test framework that now can help test .Net code.
  • Gestalt – oh yea!

You can download all the demos from here: (4.82Mb)
I didn’t have any slides, so no ppt this time!

Some resources to get you started:

If you have any questions, suggestions, complaints, consulting needs or whatever, don’t hesitate to contact me (also via twitter)!

Thanks for attending!

Upcoming Session: IronRuby on the Nes Ziona User Group

It’s time for another session about IronRuby!


The Nes Ziona user group, HP Indigo Offices – Scientific Park – 20 Einstein St. Nes Ziona.

Nes Ziona!


Tuesday, 13-October-2009, 17:00 – 20:15


The session will be separated into two parts – the first one will be passed by Vitaly Kushner who works with Ruby everyday in the company Astrails. Vitaly will introduce Ruby to you – syntax, Ruby on Rails and general coolness. On the second part (starting from 19:00) I will take everything Vitaly has told you about and bring it to the .Net world. I will introduce you IronRuby (shortly) and then move forward to explain you how you can use it in your everyday work in order to enhance yours and your team development life.


Because IronRuby rocks and I really believe it can help any one of you in your everyday work.


Register at:
there’ll be food and drinks for free!!!

Hope to see you there!

.Net Developers Hear Me Out! IronRuby is IN DA HOUSE!

Everywhere I go and preach about IronRuby I come across .Net devs who don’t understand what’s in it for them. This time I’ve decided to take action and tell you all about what’s in it for you and why you should be as excited as I am about the upcoming IronRuby 1.0.

This post is written as a conversation between me and my imaginary .Net developer friend (don’t you have one too?). Enjoy!

Friend: Hey Shay, what is this IronRuby that you can’t stop talking about?

Me: Oh imaginary friend, finally you’re being productive! well, IronRuby is Microsoft’s implementation of the Ruby language which runs on top of the DLR. Its unique power, comparing to other Ruby implementations (MRI, JRuby…), is its ability to interop with .Net assemblies with ease.

Friend: So is it an entire new language?

Me: Ruby isn’t a new language, it’s been around since 1996. However, it is a new language for the .Net framework. It is the first time for Ruby to be a fully .Net language. As a result of IronRuby being a .Net language, just like you can run VB.Net from C# and C# from VB.Net, you can run C# from IronRuby and vice versa.

Friend: Why did Microsoft choose Ruby from all the languages out there?

Me: First of all, Ruby rocks! secondly, along with the development of the Dynamic Language Runtime (DLR), Microsoft decided to develop .Net implementations of Ruby and Python. These languages appear in the top 10 of programming languages (look here) and with these .Net implementations, Microsoft will open its doors to thousands of developers worldwide, who until now had no reason to take a look at the .Net framework.

Friend: What’s in it for Ruby developers? Why would they want to use IronRuby?

Me: For Ruby devs, using IronRuby means that they can use .Net framework frameworks like WCF, WPF, ASP.Net MVC, Silverlight and others. Even though there are many libraries and frameworks available in Ruby, Microsoft does have some killer frameworks which are hard to find elsewhere like Silverlight. Another advantage is the possibility to use Microsoft infrastructure. For example, running Ruby on Rails on IIS.

Friend: Ok, so if I want to use IronRuby, I need to learn Ruby, right?

Me: Correct.

Friend: But I don’t know the language!

Me: Just like many other .Net developers, you will need to learn the Ruby language in order to write IronRuby code. Do NOT be afraid. Ruby is an easy language to learn and I promise you that learning it will open your eyes to a whole new world (which might amaze you, beware!). When I was learning Ruby I was surprised time after time about its syntax and the fun feeling you have while coding. Moreover, learning another programming language is a good practice to increase your knowledge of programming in general (read my other post about how to increase code quality).

Friend: Assuming I have learned Ruby, the company I work for will never abandon C#/VB.Net for IronRuby.

Me: I agree, I don’t believe companies will stop using C# and start coding in IronRuby. It isn’t gonna happen. However, when companies realize the benefits of using IronRuby in different scenarios, you will start seeing IronRuby code over there.

Friend: How can a company benefit from using IronRuby?

Me: A lot. As I said, I don’t expect companies to change their core code to IronRuby. There’s no reason to do that. But, there are several other areas where you can use IronRuby – tests, POCs, automation, internal tools and more.

Friend: How can IronRuby improve my tests?

Me: IronRuby has incredible test frameworks. For me, Cucumber is a killer test framework. It’s one of the most brilliant frameworks I’ve run into. To make it possible for QA and product managers to write specs in a human-readable syntax and run them as tests afterwards is just astonishing. Ruby has other great and widely-used test frameworks like RSpec and even web test frameworks like Watir.

Friend: Sounds pretty promising I must say. And why did you say it was good for POCs?

Me: Ruby is a language that enables you to write code fast. This is why writing a POC in IronRuby will take less time to develop. Remember that when you develop in IronRuby, everything is a CLR object so it is almost like writing C#/VB.Net code. Once the IronRuby POC is done and the feature goes into production development, transferring the IronRuby code to C#/VB.Net is very easy (SharpDeveloper even has a feature that does the conversion automatically). Same goes for internal tools – you write the tools faster and continue with your other tasks (look at my first ever IronRuby post where I was excited how easy and fast it was to write a tool I needed) . By the way, writing in a different language from time to time can also be a breeze of fresh air and a great routine breaker.

Friend: Great! and what about automation?

Me: Ruby has Rake which is similar to Nant in concept, just uses Ruby syntax. Once you know Ruby, there is not need to learn Nant as well… just use Rake.

Friend: You make faces like there is more. Is there?

Me: Yes! Except all the great Ruby stuff, you can also use IronRuby from your static language code. This is great because it lets you extend your application in a very easy way (take a look at my post about the subject). Another thing, the DLR along with Silverlight gives client scripting a whole new life and makes it bearable (which is not an easy task at all). Take a look at the amazing Gestalt project for more about that.

Friend: Now let’s say I write something in IronRuby. How do I deploy it? is it hard?

Me: The deployment is a matter of seconds. It is actually just copy and paste. All you need in order to run IronRuby on a different machine is to copy the Dlls there.

Friend: Nice! Well, I’ve waited till now and here is the million dollar question – can I write IronRuby inside Visual Studio?

Me: Well, in the meantime - no. There is a voting going on to convince Microsoft to do that ASAP. However, there are some great Ruby IDEs (like NetBeans, RubyMine, SciTE and more) that you can use. There is also the Ruby-in-Steel Visual Studio addin which makes it possible to work on Ruby files inside Visual Studio (it doesn’t work with IronRuby yet, but it is included in their plans). These IDEs do not have the intellisense that you are used to in VS and C#, but they are good for the job. I must say, once you get familiar with the language, you don’t need intellisense anymore. Really.

Friend: You got me convinced! I want to start IronRubying!!!

Me: Awesome! You can start with and download the latest IronRuby version from there. If you have any questions, make sure to check the IronRuby mailing list or contact me and I’ll do my best to help. By the way, IronRuby is currently in version 0.9.1 and will advance to version 1.0 in the next couple of months.

Friend: Thanks Shay! now I see what IronRuby gives me and I’m willing to give it a try!

Me: Glad I could help! see ya!

Readers, do you have any questions? post them as comments and I’ll address them as soon as I can.

Enjoy IronRubying!

kick it on Shout it

IronRuby Tip: Using Generic CLR Methods from IronRuby

A while ago I wrote a post about using Generic CLR classes from IronRuby. This time I’ll share with you its less intuitive friend – using generic CLR methods from IronRuby.

As a result of Ruby being a duck-typed language which works with types implicitly, generics is not really needed. The whole language is generic… This is why using generic CLR types might become a bit odd for the language. Nonetheless, this fact will not stop us. The price to pay in order to enjoy Ruby in the .Net framework is worth it! :)

Anyway, assuming we have the following C# class defined in file sample.dll:

namespace ClassLibrary1
    public class Class1
        public string Test<T>(T param)
                return param.ToString();

In order to use it with a specific type, you need to do two things – get the generic method object (by using the method method) and provide it with the type via the special of method. For example, the next code invokes Test with a string and pass it “Shay Friedman”:

require "sample.dll"

sample =
sample.method(:test).of(String).call("Shay Friedman") # returns "Shay Friedman"

And that’s all there is to know.

All the best,

Configuring SciTE to Run IronRuby

I talked with Ariel today and recommended him my favorite Ruby editor for small scripts and apps – SciTE.

The nice thing about SciTE is the ability to run your script file from the application via the F5 key. Since we were talking about IronRuby, SciTE gave us good syntax highlighting and auto-completion features, but we couldn’t execute our code on the IronRuby interpreter.

BUT… this is a problem of the past now! with a few simple steps, you can turn your SciTE environment to an IronRuby stupendous development environment! or at least make it use the IronRuby interpreter…

Now, to the job:

  1. Open SciTE.
  2. Go to Options->Open
    Running IronRuby via SciTE Step 1
  3. A file with a lot of settings will be opened inside the editor window. Look for the text “ruby $(FileNameExt)” (I have it on line  108 and  116).
  4. Change “ruby $(FileNameExt)” to “ir $(FileNameExt)” on both places (if you’re on Windows, changing the first one will be enough):
    Running IronRuby via SciTE Step 2
    Pay attention that there is the rbw line (look at line 110 on the image). I haven’t changed it because it’s not that common to use rbw files and also IronRuby doesn’t have the option as far as I know… I just left it there to use the regular MRI interpreter.
    If you want to make sure that every Ruby file on SciTE environment is run via IronRuby, change “rubyw $(FileNameExt)” to “ir $(FileNameExt)” as well.
  5. Note
    You must  have ir.exe defined in your PATH environment variable in order for this to work.  It is a good practice to do that, but if you don’t want to, just write the full path to ir.exe instead of just “ir”.
  6. Save the file.

That’s it, your’re done! now go and create an rb file and hit F5. IronRuby will run your script now.

Running IronRuby via SciTE Step 3


My Presentation at the AltNetCrossGroup in London

I had the honor to be the first speaker of the cross language talks in altnet London group.

I really had a great time – the people were great, the MRM office was awesome (even though Kevin might think otherwise :) ) and most of the demos worked smoothly. Microsoft organized Pizzas and beer and the atmosphere was just incredible. Special thanks for Seb, Neil and MRM for making this happen.

And there was a talk too! I talked about Ruby, IronRuby, Cucumber and Ruby on Rails. I really tried to bring some real-world demos that you can take with you and implement right away. Hopefully, you will.

If you have any questions, requests, comments, notes or whatever don’t hesitate to contact me.

So, to those who came and to those who didn’t make it, here are the slides and demo files:

Some pics from the event (click to open a higher resolution version):

Talking… pegs, cucumbers…
Starting with a story about clothes pegs Introducing the Cucumber framework

When you drink and present, you get huge exceptions…
Beers are NOT good for demos

Audience! Thanks  for coming!
The session attendants #1 The session attendants #1

At the end of the session with Neil:
End of the session with Neil Robbins

Hope you enjoyed it as much as I did,

Upcoming Session: IronRuby on the AltNetCrossGroup in London

Announcement!  Announcement!  Announcement! 

I’m coming to London this Friday and I’m going to do a session about IronRuby on the AltNetCrossGroup!

IronRuby Session on AltNetCrossGroup

The session will take place in MRM London (76 Southwark Street) on Friday, September 25th, between 18:30 and 20:30.
The agenda is:

  • Introduction to the Ruby language
  • IronRuby – using CLR objects from IronRuby and using IronRuby from C#
  • Cucumber – test your code with style
  • Ruby on Rails

This is the current plan. If you have ideas or requests, please let me know (via the contact page or twitter)!

Register here:
If you’re interested, make sure to register ASAP since the amount of seats is limited.

I am very excited to be the first speaker on the AltNetGroup series of cross language talks and I hope to see you there!