IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

My Interview on Herding Code is Published!

At this year’s NDC I had the honor to chat with Jon Galloway and Scott Allen, who are half of the Herding Code crew. We chatted about subjects related to my NDC talks – Roslyn, C#’s dynamic capabilities, and the DLR. 

Last week our chat was published as an Herding Code episode, and it is available to hear and download at http://herdingcode.com/?p=463.

Enjoy the episode and thanks Herding Code for having me!

All the best,
Shay.



Sample Code from my “What?!? C# Could Do That?!?” Session

In the last few months I had the honor of presenting my session “What?!? C# Could Do That?!?” at different conferences and user groups around the world. The session is mainly about different things you can do with C#’s dynamic capabilities, IronRuby and also a bit about the upcoming Roslyn “Compiler as a Service” project.

I’ve received several requests to upload my sample code. Therefore, I’ve just made it available on my github page - https://github.com/shayfriedman/WhatCSharpCouldDoThat-Sample.
If you have any questions about the code, don’t hesitate to contact me through twitter or the contact page.

Additionally, if you want me to come and do this session (or others) at your user group/conference, let me know!
All the best,
Shay.



Slides and Code Samples from my Talk at LIDNUG - What?!? C# Could Do That???

On Thursday I had the honor to do a virtual talk at LIDNUG – the LinkedIn .NET User Group. A stage where lots of .NET celebs like Scott Gu, Jeffery Richter, Jeff Prosise and others have talked in the past.

I’d like to thank all the attendees and the LIDNUG crew who made this possible – Inbar, Peter and Brian – you guys rock!

About the talk – I focused on the dynamic capabilities of C#. Started with some black magic done using the dynamic keyword, then moved on to practice witchcraft with the combination of IronRuby and C#, and ended with the new and shiny .NET spell-book also known as project “Roslyn”.

The talk was recorded and it can be found on YouTube:

The code samples from the talk are also available – click here to download them [2.47Mb].

I had a blast, hope you did as a well.
All the best,
Shay.



Slides and Videos of my Talks at Epicenter and NDC

I’ve just uploaded the slides from my recent sessions at the Epicenter and NDC conferences. They appear on the presentation page as well.

NDC 2010 – Riding IronRuby on Rails (Norway, June 2010)

Abstract: The most famous Ruby–driven framework is, by far, Ruby on Rails. With IronRuby, .NET developers can now take advantage of this incredible web framework without leaving their comfort zone. In this session, Shay Friedman will build an entire Web 2.0 site from scratch while using and explaining the key features of Ruby on Rails.
Come and see what Ruby on Rails is all about and what's made it the success it is today.

You can also watch the session video – streaming, iPod video (320x240), MPEG-4 for QuickTime (1280x480)

NDC 2010 – Practical IronRuby (Norway, June 2010)

Abstract: Ruby has been a home for some great innovative frameworks like Ruby on Rails, Cucumber and Rake. IronRuby version 1.0 has recently been released, unleashing the power of Ruby to the .NET world.
In this session you will get familiar with the Ruby language and its amazing ecosystem and you will learn to take advantage of it in your everyday development tasks.
Come and see how this great new addition to the .NET family makes your development process faster, clearer and happier!

You can also watch the session video – streaming, iPod video (320x240), MPEG-4 for QuickTime (1280x480)

Epicenter 2010 – Ruby on Rails Vs. ASP.NET MVC (Ireland, June 2010)

Abstract: 2010 is the year when two great web development frameworks arrive at the .NET world – ASP.NET MVC 2.0 and Ruby on Rails (via IronRuby). It is the time to get to know these frameworks and learn their advantages and disadvantages. In this session I will walk you through the good, the bad and the ugly of both frameworks providing you points to consider when coming to choose one of them.
Come and see how these two wonderful web development frameworks collide!

Epicenter 2010 – ASP.NET MVC (Ireland, June 2010)

Abstract: Since the early days of ASP and later on ASP.NET, web development has always been a confusing experience. Several different architectures were suggested over the years, all of them struggled with the same goal – to make the code clear, clean and easily maintainable. Eventually, Microsoft took up the glove and created the framework that would change web development in Microsoft environment forever – ASP.NET MVC.
In this session Shay Friedman will take you on a journey to explore the different capabilities of ASP.NET MVC 2.0 by building an entire web application from scratch. Come and see the future of web development in the .NET world!

Epicenter 2010 – Practical IronRuby (Ireland, June 2010)

Abstract: Ruby has been a home for some great innovative frameworks like Ruby on Rails, Cucumber and Rake. IronRuby version 1.0 has recently been released, unleashing the power of Ruby to the .NET world.
In this session you will get familiar with the Ruby language and its amazing ecosystem and you will learn to take advantage of it in your everyday development tasks.
Come and see how this great new addition to the .NET family makes your development process faster, clearer and happier!

Enjoy!
Shay.



A Mini-Review-Benchmark of Ruby’s Different Testing Frameworks

On of the most shining features of the Ruby language for .NET developers is, in my opinion, its testing frameworks. Ruby has got such amazing testing frameworks that it is such a shame that people still use other languages to test code.

The goal of this post is simple – to show you how to test a given code in various different frameworks, so you can choose the one for you. In the meanwhile I will also take the amount of time taken for each framework to run the tests and compare the times at the end.

For that I’m using my Ruby implementation of choice – IronRuby RC4 (1.8.6 compatible) and my powerful computer (running Windows 7 64-bit). Each testing framework will contain the same 7 tests of the tested code and be executed from the command line.

Note: The testing frameworks I bring in this post are my own random picks. I tried to bring the popular ones and some other small but interesting ones. There are more testing frameworks in Ruby and if you think I should add another ones, let me know and I’ll add them to the list too.

The Tested Code

The code I’m going to test is a C# code (IronRuby FTW!) which resides in an assembly named WaterHelper.dll. The code is as follows:

namespace Demo
{
    public class WaterHelper
    {                
        public bool IsWaterBoiled(decimal celsius)
        {
            return celsius >= 100;
        }

        public bool IsWaterFrozen(decimal celsius)
        {
            return celsius <= 0;
        }

        public string GetWaterStatus(decimal celsius)
        {
            if (IsWaterBoiled(celsius))
            {
                return "Steam";
            }
            else if (IsWaterFrozen(celsius))
            {
                return "Ice";
            }
            return "Liquid";
        }
    }
}

Not much of complication here – three methods that do some water temperature related calculation.

Test::Unit

Official site: http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html

The first testing framework I’m using is Ruby’s built-in one – Test::Unit. It is very similar to NUnit (in .NET) or JUnit (in Java).

The Test::Unit test code which tests the WaterHelper class is as follows:

require 'test/unit'
require "WaterHelper.dll"

class TC_WaterHelper < Test::Unit::TestCase
  def setup
    @instance = Demo::WaterHelper.new
  end
  
  def test_water_boiling_point
    result = @instance.is_water_boiled(100)
    assert_equal true, result
  end
  def test_water_is_boiled
    result = @instance.is_water_boiled(150)
    assert_equal true, result
  end
  
  def test_water_freezing_point
    result = @instance.is_water_frozen(0)
    assert_equal true, result
  end
  def test_water_frozen
    result = @instance.is_water_frozen(-50)
    assert_equal true, result
  end
  
  def test_water_status_steam
    result = @instance.get_water_status(300)
    assert_equal "Steam", result
  end  
  def test_water_status_liquid
    result = @instance.get_water_status(70)
    assert_equal "Liquid", result
  end
  def test_water_status_Ice
    result = @instance.get_water_status(-5)
    assert_equal "Ice", result
  end
end

Execution time: 0.082005 seconds.

RSpec

Official site: http://rspec.info/
Version: 1.3.0

By following BDD principles and providing a clean and elegant DSL (Domain Specific language), RSpec has gained a lot of fans. It is maybe the most popular testing framework in the Ruby world currently.

The code to test the WaterHelper.dll with RSpec is as follows:

require "rubygems"
require "spec"
require "spec/autorun"
require "WaterHelper.dll"

describe "Testing WaterHelper class" do
  before(:each) do
    @instance = Demo::WaterHelper.new
  end
  
  it "should be boiling water when it is 100 degrees" do
    result = @instance.is_water_boiled(100)
    result.should be_true
  end  
  it "should be boiling water when it is 150 degrees" do
    result = @instance.is_water_boiled(150)
    result.should be_true
  end
  
  it "should be frozen water when it is 0 degrees" do
    result = @instance.is_water_frozen(0)
    result.should be_true
  end
  it "should be frozen water when it is -50 degrees" do
    result = @instance.is_water_frozen(-50)
    result.should be_true
  end
  
  it "returns Steam for 300 degress" do
    result = @instance.get_water_status(300)
    result.should == "Steam"
  end
  it "returns Liquid for 70 degress" do
    result = @instance.get_water_status(70)
    result.should == "Liquid"
  end
  it "returns Ice for -5 degress" do
    result = @instance.get_water_status(-5)
    result.should == "Ice"
  end
end

Execution time: 0.201012 seconds.

Cucumber

Official site: http://cukes.info/
Version: 0.6.4

Cucumber is one of the most innovative frameworks out there. It took BDD one step further by providing a simple way to write requirement documents in a language called Gherkin (which is plain English with a few rules) and interpret them via code.

The next Gherkin code contains the requirements for the WaterHelper class:

Feature: WaterHelper
  As all users
  I want to know the status of the water
  To know how to treat it
  
  Scenario: Water are boiled at 100 degrees
    Given the temperature is 100 degrees
    When I check whether the water is boiled
    Then I should find out that it is
    
  Scenario: Water are boiled at 150 degrees
    Given the temperature is 150 degrees
    When I check whether the water is boiled
    Then I should find out that it is
    
  Scenario: Water are frozen at 0 degrees
    Given the temperature is 0 degrees
    When I check whether the water is frozen
    Then I should find out that it is
    
  Scenario: Water are frozen at -50 degrees
    Given the temperature is -50 degrees
    When I check whether the water is frozen
    Then I should find out that it is
    
  Scenario Outline: Water status
    Given the temperature is <temperature> degrees
    When I check the water status
    Then I should find out it is "<status>"
    
    Examples:
      | temperature | status |
      |     300     | Steam  |
      |     70      | Liquid |
      |     -5      |  Ice   |

And this is the Ruby code file to interpret the requirements:

require "WaterHelper.dll"

Before do
  @instance = Demo::WaterHelper.new
end

Given /the temperature is (.*) degrees/ do |temperature|
  @temperature = temperature.to_f
end

When /I check whether the water is boiled/ do
  @result = @instance.is_water_boiled(@temperature)
end
When /I check whether the water is frozen/ do
  @result = @instance.is_water_frozen(@temperature)
end
When /I check the water status/ do
  @result = @instance.get_water_status(@temperature)
end

Then /I should find out that it is/ do
  @result.should == true
end
Then /I should find out it is "(.*)"/ do |status|
  @result.should == status
end

Execution time: 0.883 seconds.

Shoulda

Official site: http://github.com/thoughtbot/shoulda
Version: 2.10.3
Modification: there is currently a bug in IronRuby which prevents Shoulda from running. I altered the IronRuby code to make it work and I’m in contact with the IronRuby team about the problem.

Shoulda is another popular testing framework. It is built on top of the Test::Unit testing framework and it makes it more developer-friendly.

The test code is as follows:

require 'rubygems'
require 'shoulda'
require "WaterHelper.dll"

class WaterHelperTest < Test::Unit::TestCase
  context "WaterHelper" do
    setup do
      @instance = Demo::WaterHelper.new
    end
    
    should "be boiling water when it is 100 degrees" do
      result = @instance.is_water_boiled(100)
      assert_equal true, result
    end
    should "be boiling water when it is 150 degrees" do
      result = @instance.is_water_boiled(150)
      assert_equal true, result
    end
    should "be frozen water when it is 0 degrees" do
      result = @instance.is_water_frozen(0)
      assert_equal true, result
    end
    should "be frozen water when it is -50 degrees" do
      result = @instance.is_water_frozen(-50)
      assert_equal true, result
    end
    should "return Steam for 300 degress" do
      result = @instance.get_water_status(300)
      assert_equal "Steam", result
    end
    should "return Liquid for 70 degress" do
      result = @instance.get_water_status(70)
      assert_equal "Liquid", result
    end
    should "return Ice for -5 degress" do
      result = @instance.get_water_status(-5)
      assert_equal "Ice", result
    end
  end
end

Execution time: 0.096005

riot

Official site: http://github.com/thumblemonks/riot
Version: 0.10.13
Modification: changed code to use iron-term-ansicolor (IronRuby’s equivalent to term/ansicolor)

The riot testing framework is a cute little thing. Its main goal is to make it quicker to write tests and to execute them. The code turns out very minimalistic, which makes this framework a good choice when you need to write some quick unit tests.

The next code contains the unit tests for the WaterHelper class written with the riot framework:

require "rubygems"
require "riot"
require "WaterHelper.dll"

context "Tests WaterHelper class" do
  setup {  Demo::WaterHelper.new }  
  
  asserts("the water is boiling when it is 100 degrees") {  topic.is_water_boiled(100) }    
  asserts("the water is boiling when it is 150 degrees") {  topic.is_water_boiled(150) }    
  asserts("the water is frozen when it is 0 degrees") {  topic.is_water_frozen(0) }    
  asserts("the water is frozen when it is -50 degrees") {  topic.is_water_frozen(-50) }
  asserts("you get steam when it is 300 degress") { topic.get_water_status(300) == "Steam" }
  asserts("you get liquid when it is 70 degress") { topic.get_water_status(70) == "Liquid" }
  asserts("you get ice when it is -5 degress") { topic.get_water_status(-5) == "Ice" }
end

Execution time: 0.083005 seconds.

Protest

Official site: http://rubyprotest.org/
Version: 0.3

Protest is another small and fun testing framework. It is described as a “small, simple and easy-to-extend testing framework” on its web site. It keeps it promise, I tell ya!

The next code tests the WaterHelper class using the Protest framework:

require "rubygems"
require "protest"
require "WaterHelper.dll"

Protest.context "WaterHelper class" do
  setup do
    @instance = Demo::WaterHelper.new
  end
  
  test "should be boiling water when it is 100 degrees" do
    result = @instance.is_water_boiled(100)
    assert result == true
  end
  test "should be boiling water when it is 150 degrees" do
    result = @instance.is_water_boiled(150)
    assert result == true
  end
  test "should be frozen water when it is 0 degrees" do
    result = @instance.is_water_frozen(0)
    assert result == true
  end
  test "should be frozen water when it is -50 degrees" do
    result = @instance.is_water_frozen(-50)
    assert result == true
  end
  test "returns Steam for 300 degress" do
    result = @instance.get_water_status(300)
    assert result == "Steam"
  end
  test "returns Liquid for 70 degress" do
    result = @instance.get_water_status(70)
    assert result == "Liquid"
  end
  test "returns Ice for -5 degress" do
    result = @instance.get_water_status(-5)
    assert result == "Ice"
  end
end

Execution time: 0.164009 seconds.

Stories

Official site: http://github.com/citrusbyte/stories
Version: 0.1.3

The Stories testing framework is built on top of Test::Unit and provides an entire different syntax for it. It has a convenient DSL for writing tests as “stories”.

The next code tests the WaterHelper class using the Stories framework:

require "rubygems"
require "stories"
require "WaterHelper.dll"

class WaterHelperClass < Test::Unit::TestCase
  story "As a user I want to know the status of the water" do
    setup do
      @instance = Demo::WaterHelper.new
    end
    scenario "Given 100 degrees, the water should be boiled" do
      result = @instance.is_water_boiled(100)
      assert_equal true, result
    end
    scenario "Given 150 degress, the water should be boiled" do
      result = @instance.is_water_boiled(150)
      assert_equal true, result
    end
    scenario "Given 0 degress, the water should be frozen" do
      result = @instance.is_water_frozen(0)
      assert_equal true, result
    end
    scenario "Given -50 degress, the water should be frozen" do
      result = @instance.is_water_frozen(-50)
      assert_equal true, result
    end
    scenario "On 300 degrees, water status should be steam" do
      result = @instance.get_water_status(300)
      assert_equal "Steam", result
    end
    scenario "On 70 degrees, water status should be liquid" do
      result = @instance.get_water_status(70)
      assert_equal "Liquid", result
    end
    scenario "On -5 degrees, water status should be ice" do
      result = @instance.get_water_status(-5)
      assert_equal "Ice", result
    end
  end
end

Execution time: 0.118007 seconds.

Lemon

Official site: http://proutils.github.com/lemon/
Version: 10.03.06

Lemon is an interesting unit testing framework. It provides a DSL which makes it very clear to identify which class and method you are testing. Moreover, it has code coverage capabilities which can report you which methods are not covered by your code. It is dependant on a bunch of other gems which makes it a bit slower than other frameworks.

The next code tests the WaterHelper class using the Lemon framework:

require "WaterHelper.dll"

TestCase Demo::WaterHelper do
  Concern "Water statuses are returned as expected."

  Before { @instance = Demo::WaterHelper.new }

  Unit :is_water_boiled => "returns true for 100 degress" do
    result = @instance.is_water_boiled(100)
    result.assert == true
  end
  Unit :is_water_boiled => "returns true for 150 degress" do
    result = @instance.is_water_boiled(150)
    result.assert == true
  end
  
  Unit :is_water_frozen => "returns true for 0 degress" do
    result = @instance.is_water_frozen(0)
    result.assert == true
  end
  Unit :is_water_frozen => "returns true for -50 degress" do
    result = @instance.is_water_frozen(-50)
    result.assert == true
  end
  
  Unit :get_water_status => "returns Steam for 300 degress" do
    result = @instance.get_water_status(300)
    result.assert == "Steam"
  end
  Unit :get_water_status => "returns Liquid for 70 degress" do
    result = @instance.get_water_status(70)
    result.assert == "Liquid"
  end
  Unit :get_water_status => "returns Ice for -5 degress" do
    result = @instance.get_water_status(-5)
    result.assert == "Ice"
  end
end

Execution time: 1.204097 seconds.

bacon

Official site: http://rubyforge.org/projects/test-spec
Version: 1.1

bacon is a small RSpec clone which is written in 300 lines of code. Its syntax is very similar to RSpec with a small difference on the expectation method (should).

The next code tests the WaterHelper class with the bacon framework:

require "rubygems"
require "bacon"
require "WaterHelper.dll"

describe "Testing WaterHelper class" do
  before do
    @instance = Demo::WaterHelper.new
  end
  
  it "should be boiling water when it is 100 degrees" do
    result = @instance.is_water_boiled(100)
    result.should.be.true
  end
  it "should be boiling water when it is 150 degrees" do
    result = @instance.is_water_boiled(150)
    result.should.be.true
  end  
  it "should be frozen water when it is 0 degrees" do
    result = @instance.is_water_frozen(0)
    result.should.be.true
  end
  it "should be frozen water when it is -50 degrees" do
    result = @instance.is_water_frozen(-50)
    result.should.be.true
  end
  
  it "returns Steam for 300 degress" do
    result = @instance.get_water_status(300)
    result.should.equal "Steam"
  end
  it "returns Liquid for 70 degress" do
    result = @instance.get_water_status(70)
    result.should.equal "Liquid"
  end
  it "returns Ice for -5 degress" do
    result = @instance.get_water_status(-5)
    result.should.equal "Ice"
  end  
end

Execution time: 0.120007 seconds.

Contest

Official site: http://rdoc.info/projects/citrusbyte/contest and http://github.com/citrusbyte/contest
Version: 0.1.2

Contest’s target is to bring contexts to Test::Unit. Its syntax is similar to Shoulda’s just with a different test method name (named test).

The code:

require 'rubygems'
require 'contest'
require "WaterHelper.dll"

class WaterHelperTest < Test::Unit::TestCase
  context "WaterHelper" do
    setup do
      @instance = Demo::WaterHelper.new
    end
    
    test "be boiling water when it is 100 degrees" do
      result = @instance.is_water_boiled(100)
      assert_equal true, result
    end
    test "be boiling water when it is 150 degrees" do
      result = @instance.is_water_boiled(150)
      assert_equal true, result
    end
    test "be frozen water when it is 0 degrees" do
      result = @instance.is_water_frozen(0)
      assert_equal true, result
    end
    test "be frozen water when it is -50 degrees" do
      result = @instance.is_water_frozen(-50)
      assert_equal true, result
    end
    test "return Steam for 300 degress" do
      result = @instance.get_water_status(300)
      assert_equal "Steam", result
    end
    test "return Liquid for 70 degress" do
      result = @instance.get_water_status(70)
      assert_equal "Liquid", result
    end
    test "return Ice for -5 degress" do
      result = @instance.get_water_status(-5)
      assert_equal "Ice", result
    end
  end
end

Execution time: 0.077004

Custom Testing Framework

This is a custom testing framework built in one line of code. I found it in a blog post by Paul Berry. It is not really for production purposes but it works well, it’s cool and it shows you how powerful Ruby is.

This next piece of code contains both the tests and the testing framework implementation. I surrounded everything with the Benchmark library to get the amount of time it takes to execute the tests:

require "benchmark"
require "WaterHelper.dll"

total_time = Benchmark.measure do
  tests = {
    "is_water_boiled(100)" => true,
    "is_water_boiled(150)" => true,
    "is_water_frozen(0)" => true,
    "is_water_frozen(-50)" => true,
    "get_water_status(300)" => "Steam",
    "get_water_status(70)" => "Liquid",
    "get_water_status(-5)" => "Ice"
  }

  instance = Demo::WaterHelper.new
  
  # The next line goes over all tests and executes them
  tests.each{|e,v| puts((r=eval("instance." + e))==v ? ". #{e}" : "! #{e} was '#{r}', expected '#{v}'")}
end

puts total_time

Execution time: 0.156001 seconds.

Benchmark Conclusion

Benchmarking testing frameworks is not such a good idea. Performance is not something you should care about when you write tests. It is much more important to have a maintainable set of tests instead of fast running ones that need a week of work when a requirement changes.

But, it’s cool and interesting to have charts in blog posts! so, here it is… the comparison between the execution time of all testing frameworks in this post:

Ruby's testing framework execution time comparison

It turned out that Contest is the quickest one but except Cucumber and Lemon, the rest of the frameworks are behind only by a small difference.

Interesting facts:

  • All frameworks finished the tests under 1 second.
  • Test::Unit is more than 2 times faster than RSpec.
  • Contest, which is build on top of Test::Unit, actually runs a bit faster (I tried that multiple times!)
  • The custom testing framework got a real nice spot in the middle.
  • Cucumber is not the quickest one at all but it is still the coolest one :-)

Conclusion

IronRuby opens a whole new world of opportunities for .NET developers and Rubyists. In this post I focused on testing code but of course there is much more to it than just that. However, this showcase gets my point through – this post includes a variety of 11 (!!!) different testing frameworks. Each frameworks has its own uniqueness, making it very easy for you to choose the framework that works best for you.

And just for you to know - I had so much fun writing this post! Ruby is just awesome. Period.

All the best,
Shay.

Share: DZone | RubyFlow | Reddit

Shout it kick it on DotNetKicks.com



Executing IronPython Code from IronRuby

One of the advantages of the Dynamic Language Runtime (DLR) is the fact that it makes sharing code between the languages that are written on top of it (and on top of the CLR as well). Therefore, it is possible to share code between IronPython and IronRuby (and any other DLR language as well like IronScheme).

This means that IronPython libraries can be used from IronRuby code and vice versa. Ruby on Rails in Python? Django in Ruby? feels like the end of days, isn’t it? perhaps we should really start preparing to year 2012

In this post I’ll show you how to run simple IronPython code from IronRuby so you can take it and do whatever your imagination guides you to.

Assuming we have a demo.py IronPython file with the next content:

class MyPythonClass:
  def add(self, x, y):
    return x + y

welcome_message = "Hello from Python!"

 

To those of you who don’t know Python, the code  above declares a class named MyPythonClass with a single method named add that combines two numbers and return the result. It also contains a variable named welcome_message.

Note

Because executing Python code from IronRuby involves DLR services, methods that you call from IronRuby must have self as their first argument just like IronRuby-targeted C# methods have (if the method accepts no parameters, then it should have only the self argument). This argument contains the caller class instance.
This means that python code that should be executed by IronRuby should be modified to match the requirements.

After we have the python file in place we can use it from IronRuby. The key for doing so is loading the python file using the IronRuby.require method. This method is similar to the Kernel#require method but with a small difference – it returns the DLR scope object of the loaded script.  This enables you to call the script members via the scope, just like when you load a script manually via the DLR LoadFile method.

The next IronRuby is pretty straight-forward:

# Load the python file
python = IronRuby.require('demo.py')

# Get an instance of MyPythonClass
python_class = python.MyPythonClass()
# Execute the add method (pay attention that there's no 
# need to pass the self parameter, this is done automatically)
puts python_class.add(1, 5)

# Get the python variable and print its value
puts python.welcome_message
# Set the python variable
python.welcome_message = "Hello from Ruby!"
# Print its new value
puts python.welcome_message

 

The output is:
6
Hello from Python!
Hello from Ruby!

Note that for this sample to run, you need IronRuby and IronPython installed on your machine, both compiled with the same Microsoft.Scripting project. I just compiled IronRuby’s and IronPython’s sources to make  it work.

In conclusion, the DLR opens a bunch of new and exciting possibilities specifically in the field of code sharing between dynamic languages and dynamic and static languages. Go ahead and try it, it’s magical!

All the best,
Shay.

kick it on DotNetKicks.com Shout it



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



Make Your Application Extendable Using the DLR

It’s very common for applications to have a way to extend them. Extensibility comes in various ways and have multiple names too – plug-ins, add-ins, addons, etc. It seems, though, that one kind of extensibility was left to the very few – application macros.
The concept is very simple – You don’t need to create a special Dll, implement a specific interface and register it somehow, you don’t even have to install an IDE. All you have to do is to open the relevant window, write some code and voila – you’ve extended the application.

What is this post about?

I’ll start with the result.
I built a simple application – it has a textbox, a “Send” button and a larger textbox for the application output. The flow is very simple – the user writes text in the textbox, hits “Send” and a “Hello” message appears in the larger textbox:

Extending Applications using the DLR - Sample #1

The power lies in the button on the bottom of the form - “Extensions”. Clicking there will open the extensions dialog, where you can write a macro which will be run after the user clicks “Send”. The users can choose the language for their macro, which will be one of the DLR’s languages – IronRuby or IronPython. After the user writes the code and hits “Save”, the application is extended!

Extending Applications using the DLR - Sample #2

 

Now when the user clicks send, look what happens:

Extending Applications using the DLR - Sample #3

This sample is very simple but it took me about 20 minutes to get it done. It’s just magic!

How was it done?

Well, all the credit goes to the DLR. This amazing little thing, makes it soooo easy to integrate dynamic languages into the .Net environment, that it’s a shame not to do so.

The heart of this application is the ExtensionRunner class and its Run method:


public static bool Run(Form1 frm, string code, bool rubyCode)

The method receives 3 parameters – the form object to pass to the executed code, the code that the user has written and a boolean value indicating whether it’s ruby or python code.

Firstly, I declare a ScriptScope and a ScriptEngine variables and fill them with the chosen language implementation. Because all DLR languages implement the same interfaces and classes, this is the only language-related code here. After that, the code will fit every DLR language. This means that adding IronScheme and Nua here, for instance, will be a matter of seconds:


ScriptScope
scope = null; ScriptEngine engine = null; if (rubyCode) { engine = Ruby.CreateEngine(); scope = Ruby.CreateRuntime().CreateScope(); } else { engine = Python.CreateEngine(); scope = Python.CreateRuntime().CreateScope(); }

Now we declare variables that the macro code will be able to use. We declare 2 variables:

  • frm – which is the form object. you wouldn’t want to do that in your application, the right way will be to pass a class with targeted functionality for the extensions.
  • return_value – the extension will put a value here. It’s a boolean value which indicates whether we should continue with the regular flow of the application after the extension has been executed or stop.

scope.SetVariable(
"frm",frm); scope.SetVariable("return_value", false);

The rest of the code is pretty straight forward – I create a ScriptSource object from the given code and execute it. Then I get the return_value value and return it to my main application.


// Run the code!
ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements); source.Execute(scope); // Get the return value bool retVal = scope.GetVariable<bool>("return_value"); return retVal;


This is it. The other code of the application is just a regular WinForms code so I won’t deep dive into it.

Conclusion

In conclusion, the DLR makes it very simple to run dynamic languages from your .Net code. I showed you here only one of the possibilities that are made available with this kind of integration.

Go ahead and try!

Download the source code

All the best,
Shay.

Like it? Share it: Loves soccer? Kick it  | Angry? Shout it  | In DA ZONE? DZone it

IronRuby, IronPython and DLR Daily Builds RSS Feed Is Available!

With the help of Harry Pierson (AKA DevHawk), there is an RSS feed that will notify you when a new nightly build is available!image 
The builds are daily and you get the latest versions of IronRuby, IronPython and the DLR in Silverlight or desktop versions.

So all of you early adapters, go ahead and subscribe to the feed!

Comment: the files in the feed are the built files, not the source code.

Enjoy,
Shay.

Getting Started With Dynamic Languages

I've grouped together some resources and blogs for all of you out there who are willing to start working with dynamic languages that are built on top of the DLR. Enjoy!

IronRuby IronRuby

Installation

Currently you'll have to get the code from the IronRuby SVN repository (svn://rubyforge.org/var/svn/ironruby  OR HTTP://ironruby.rubyforge.org/svn/trunk/) and build the project yourself.
Justin Etheredge has posted a step-by-step walk-through on his blog.

Resources

Recommended Blogs

IronPython IronPython

Installation

You can get the installer from the IronPython codeplex homepage: http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=8365

Resources

Recommended Blogs

IronScheme  IronScheme

 Installation

You can get the installer from: http://www.codeplex.com/IronScheme/Release/ProjectReleases.aspx?ReleaseId=14059

Resources

The Dynamic Language Runtime DLR - The Dynamic Language Runtime

There is no need to download the DLR code separately because it already comes with the installation of the other languages. If you want to write your own language on top of the DLR, this is the place to start for you.

Installation

You can get the binaries and the code from the codeplex homepage: http://www.codeplex.com/dlr/Release/ProjectReleases.aspx?ReleaseId=20378

Resources

Hope it helps,
Shay.

kick it on DotNetKicks.com