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

January 7. 2013 09:47 PM

free deals

Thanks for sharing your perspective, I'll go this amongst my pals who may be interested on this subject, you've got yourself some common readers, and I'm considered one of them.

free deals

January 22. 2013 12:09 PM

PDF To Photo

I havent any word to appreciate this blog.....Really i am impressed from this blog....the person who create this post it was a great human..thanks for shared this with us.

PDF To Photo

January 23. 2013 11:22 AM

PDF To Image

The blog is very excellent.Let me learn a lot of knowledge.

PDF To Image

February 3. 2013 08:46 PM

Worldstarhiphop

Your details are very good; we got new knowledge from your site. Template also very good, color matching is well. I will keep visiting your site again. It is a great article thanks for sharing this information. Give your knowledge for all people. Because who likes to know more information. I saved some details to me.

Worldstarhiphop

February 16. 2013 08:56 AM

Barcode Generator

I bookmarked this blog a while ago because of the useful content and I am never being disappointed. Keep up the good work.

Barcode Generator

February 22. 2013 08:51 PM

music videos

This was exactly what i was searching for. Have been fighting for a while to do this, thanks for have posted.

music videos

March 23. 2013 01:01 PM

PDF Merger

I havent any word to appreciate this article.....Really i am impressed from this post....the person who create this post it was a great human..

PDF Merger

April 2. 2013 11:32 AM

How to cook

Great stuff from you, man.  I’ve read your stuff before and you’re just too awesome.  I love what you’ve got here, love what you’re saying and the way you say it.  

How to cook

April 8. 2013 11:48 AM

love song

Articles written is very true . Article content immersive . I have been concerned about your article . The content is very good .

love song

April 11. 2013 12:06 PM

cooking class

I am glad to found such useful post. I really increased my knowledge after read your post which will be beneficial for me. Thanks.

cooking class

April 12. 2013 09:40 PM

Yacht Charter in Dubai

Hi everyone, This site is excellent and so is how the subject matter was explained. I like some of the comments too although I would suggest everyone stays on the subject matter so that to add value to the message.

Yacht Charter in Dubai

April 16. 2013 12:52 PM

windows7supportnow.com

i got to know about some amazing facilities of visual studio 2010. the key word has turned out to be a revolutionary thing. i like such good and informative reads like yours and want more of this kind.

windows7supportnow.com

April 27. 2013 02:43 PM

tree care services

Really i am impressed from this blog....the person who create this post it was a great human..thanks for shared this with us.

tree care services

May 1. 2013 08:22 PM

Get More Followers on Google Plus

Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work. Thanks for this very useful info you have provided us.

Get More Followers on Google Plus

May 12. 2013 07:55 PM

Lucy V

Yeah I was a fan of Visual Studio 2010 back in the day. Latest updates not to shabby either.

Lucy V

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