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 = []
  end
   
  # Save the calls to method_missing	
  def method_missing(name, *args, &block)
    @calls << [name, args, block]
  end

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

 

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 = Recorder.new
rec.reverse!
rec.insert 2, "ABAB"
rec.delete! "A"

# Playback them on a real object
str = "Hello World"
rec.playback(str)
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();
  engine.ExecuteFile("../../recorder.rb");
  dynamic ruby = engine.Runtime.Globals;

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

  // Record
  recorder.Add(1);
  recorder.Add(2);

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

  // Print the results!
  foreach (var item in list)
  {
    Console.WriteLine(item);
  }

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

  // Playback on console
  recorder.playback(Console.Out);
}


The output to the console will be:
1
2
IronRuby and .Net 4.0
Rock!!!!!!!!!!

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,
Shay.

Share it: kick it on DotNetKicks.com Shout it




Comments

October 26. 2009 06:34 PM

trackback

C# Recorder using IronRuby

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

October 26. 2009 06:34 PM

trackback

C# Recorder using IronRuby

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

October 26. 2009 07:33 PM

Kevin Radcliffe

Very nice and interesting use of dynamic. It really lowers the bar to entry in using dynamic languages from C#. Like for me, it sounds like you had done enough interop with C# previous to dynamic to really see the difference it makes. Thanks very much for posting!

Kevin Radcliffe

October 27. 2009 10:38 PM

trackback

C# Recorder с использованием IronRuby

Thank you for submitting this cool story - Trackback from progg.ru

progg.ru

October 27. 2009 10:40 PM

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        IronShay | C# Recorder using IronRuby
        [ironshay.com]
        on Topsy.com

topsy.com

October 28. 2009 01:43 AM

trackback

Social comments and analytics for this post

This post was mentioned on Twitter by blooders: RT @ironshay: Just blogged: C# Recorder using IronRuby http://bit.ly/4w0tox (.Net 4.0 + IronRuby = LOVEEEE)

uberVU - social comments

October 28. 2009 02:19 AM

jbland

Wow ! i could imagine some serious applications for this. Imagine what this could do for automated testing ....

jbland

November 4. 2009 06:22 PM

trackback

IronRuby Sample #3: Creating a DSL

IronRuby Sample #3: Creating a DSL

IronShay

November 4. 2009 06:28 PM

trackback

IronRuby Sample #3: Creating a DSL

[ This is part 3 of my IronRuby samples series. You can read the first post (Hello World) and the second

IronShay

November 10. 2009 05:02 AM

waggi

Great post.

waggi

November 6. 2010 03:05 PM

Jonathan O.

Indeed it's a great feature and congrats on your post , you managed to get the key parts very well organized and explained.

Jonathan O.

November 28. 2010 11:56 PM

Ringback Tones

Another great IronRuby tutorial by you, thanks a bunch!

Ringback Tones

January 26. 2011 12:48 PM

pingback

Pingback from cheaphomeideas.com

How To Write Jargon-Free Business Documents | Cheaphomeideas.com

cheaphomeideas.com

December 3. 2011 12:56 PM

makeityourring diamond engagement rings

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

makeityourring diamond engagement rings

January 15. 2012 07:07 PM

feedy

Good information

feedy

January 18. 2012 09:51 PM

Buy cheap laptops

Thank you for submitting this cool story - Trackback from progg.ru

Buy cheap laptops

January 19. 2012 05:27 AM

cheap uggs

Thank you for submitting this cool story

cheap uggs

January 19. 2012 10:40 AM

pennyslvania limited liabilty company

Pretty insightful post. Never believed that it was this simple after all. I had spent a great deal of my time looking for someone to explain this subject clearly and you're the only 1 that ever did that.

pennyslvania limited liabilty company

January 20. 2012 12:11 PM

leeone

Hey , How are you? I like your blog.  It has great information that I can use.

leeone

January 23. 2012 05:45 PM

Big deals

Good post. I just happened to your blog and really need to subscribe to feeds to read bad blogs are playing the way I want to say posts.Any and hope you post back soon.

Big deals

January 24. 2012 01:18 PM

Reno carpet cleaning

Thank you for this interesting article. I will be sure to get the word out about this on my site.

Reno carpet cleaning

January 24. 2012 03:55 PM

soft toys

blog is absolutely fantastic! All great information can be helpful in some or the other way. Keep updating the blog, looking forward for more content. Keep updating the blog, looking forward for more content.

soft toys

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting