Emulate C# Using Statement in Ruby

Posted by Andrew on September 27, 2007

Ruby is great language for creating DSLs. One reason why is that the syntax is expressive enough to create what look like new language features. As a simple example lets see how to create the Ruby equivalent of the C# Using statement. In C# the statement looks like this:

using (IDisposable foo = new DisposableImpl())
{
  // do something with foo
}

The semantics of the statement are:

  • foo must implement the IDisposable interface.
  • foo.Dispose() is guaranteed to be called when the statement terminates.

So in Ruby we would want something like this:

require 'using'
 
class Foo
  def dispose
    puts "Disposing"
  end
end
 
using (foo = Foo.new) {
  puts "Inside using"
}

When we run this we get:

RubyMate r6354 running Ruby r1.8.2 (/usr/bin/ruby)
>>> test.rb

Inside using
Disposing


So how do we implement this? Basically, we just create a new method on Object (the root of all Ruby objects) called “using” and then we exploit the fact that in Ruby any method can be passed an optional “block” of code as it’s last argument (think anonymous delegate or lambda in C#). The sugar here is that this last block argument actually appears after the closing parenthesis of the argument list. Nice.

class Object
  def using(o)
    begin
      yield if block_given?
    ensure
      o.dispose
    end
  end
end

The Beauty of Ruby

Posted by Andrew on July 05, 2007

For anyone interested in programming languages there’s a pretty good video presentation up on InfoQ at the moment. Worth a look :-)

C# vs. VB

Posted by Andrew on July 03, 2007

Rowan posted about C# vs. VB:

> None, if challenged, would be able to build anything using
> C# that Phil couldn’t build just as well in VB.

This isn’t the point. Different languages have different levels of expressiveness and also different aesthetics. A program written in Haskell may be 10 times shorter than the same program written in C++ for example [1].

Personally, I have two favourite languages: Ruby and Haskell :-)

In Ruby I can write this:

10.times { |i| puts i }

But in C# I have to write this:

for (int i = 0; i < 10; i++)
{
  Console.Write(i);
}

Which do you find more beautiful? :-)

1. http://web.cecs.pdx.edu/~apt/cs457_2005/hudak-jones.pdf

Gardens Point Ruby.NET Compiler

Posted by Andrew on May 22, 2007

This seems to have gone under the radar a little bit, what with all the recent Microsoft DLR/IronRuby news, but the QUT guys released 0.7 of their Ruby .NET compiler this month too - and it sounds like they are making good progress!

Since the last release we have added support for debugging (by generating pdb files) and have created a Visual Studio integration package allowing users to edit, build, execute and debug Ruby programs within Visual Studio 2005. This includes syntax colouring, error highlighting, brace matching, hidden regions, Ruby.NET projects, project properties, project templates and project item templates. Ruby.NET projects (.rbproj) enable multiple Ruby source files to be compiled into a single .NET assembly.

I wonder where we will see Rails.NET running first? :-)