faithfulgeek.org

Agile web development

Evil Fun With IronRuby

June 17th, 2008

I was playing with IronRuby a bit last night while driving back from Columbus Ruby Brigade with Ken Adair. It started with wondering if it’s possible to .each on a .NET ArrayList (System.Collections.ArrayList) and iterate via a closure ala Ruby arrays. Here’s the result of my testing (testing/output via IR.cmd – IronRuby’s IRB – comments added later):


# Create a Ruby variable a and set it to a new instance of a .NET ArrayList
>>> $a = System::Collections::ArrayList.new
=> []
# Add the string "j" to the list
>>> $a << "j" 
=> ["j"]
# Add the number 1 to the list
>>> $a << 1
=> ["j", 1]
# Add the string "joe" to the list using ArrayList.
>>> $a.Add("joe")
=> 2
# Call each and print out each element using a closure
>>> $a.each { |i| puts i }
j
1
joe
=> ["j", 1, "joe"]

So to answer the question, yes, it does iterate via a closure just like in Ruby! As an added bonus, we also found out that the add operator (<<) works with ArrayList too! Very cool. The question is, how does this work? Is there a layer that adds these methods to ArrayList or is ArrayList simply mapped to a Ruby array under the covers?

So we decided to take this experiment one step further: one of the awesome features of Ruby is open classes. So, what happens when I try to open a .NET type?


>>> class System::String
... def say_hello
... "Hello " + self
... end
... end

In the above snippet we opened up System.String and defined a say_hello method. So what happens when we try to call it:


>>> "joe".say_hello
:0:in `Initialize': undefined local variable or method `say_hello' for joe:String (NoMethodError)

At first glance you might think that it didn’t work. However, this is actually expected because “joe” is an instance of a Ruby String not a .NET string. Now if we do this:


>>> "joe".ToString.say_hello
=> "Hello joe" 

Much better! So, somehow I can open System.String and add methods to it. Seems kind of evil to me. I wonder what the guidance will be around opening up .NET classes. Especially when you can’t even inherit from some of them in C#!

Fun with Ruby.NET!

December 26th, 2007

I started really playing with the Gardens Point Ruby.NET compiler. I wrote a little script that shows the true coolness of Ruby.NET:


require 'mscorlib.dll'

#Using System.Console.WriteLine to print to the screen
System::Console.WriteLine "Hello World!" 

names = ['Joe', 'Katie', 'Bob', 'Sue', 'Ann']

names.each do |name| puts "Hello #{name}!" end

# Create a .NET ArrayList
list = System::Collections::ArrayList.new

# Add the Ruby array to the .NET ArrayList
list.AddRange names

# Iterate through ArrayList and print Hello, name to the screen using System.Console.WriteLine
0.upto list.Count-1 do |i|
  System::Console.WriteLine "Hello {0}!", list[i]
end

Enjoy!

CLR String.Format in Ruby

December 8th, 2007

I love using String.Format instead of string concatenation in C#. It’s become one of my favorite functions. For those who don’t know C#, String.Format works like this:


String.Format("Hello, {0}, today's date is {1}!", "Joe", 
    DateTime.Now.ToShortDateString());  
    // <-- This will return "Hello, Joe, today's date is 12/8/2007!" 

I’ve been coding in Ruby quite a bit lately. I couldn’t find a similar function, so I wrote my own:

class String

    def self.format(format, *args)
        index = 0
        args.each do |arg|
            format.sub!("{#{index}}", arg)
            index = index + 1
        end
        format
    end

end
The most beautiful part of this is I can still type String.format as I did before. The Ruby String.format actually redefines the String class, allowing it to define the class method I created. This feature is included in C# 3.0, known as Extension Methods (I’m not sure what they call it in Ruby). I guess I should mention it’s been in Ruby since the beginning.

Anyway, now in my Ruby classes, I can type:

String.format "Hello, {0}, today's date is {1}", "Joe", Date::today 
    # <-- Returns "Hello, Joe, today's date is 12/8/2007", just like before

If you would like to be able to use String.Format in Ruby, feel free to add this method to your Ruby project! Also, if you have any questions or comments, please feel free to leave them in the comments section!

Update [12/10/2007]: I have since learned that I can use variables directly in strings with Ruby, for example “Hello, ${name}, today’s date is ${Date::today}”. This is yet another example of where the beauty of Ruby makes a major feature in .NET completely obsolete. I’ll definitely be doing some refactoring to get rid of my String.format.

Alt.Net: Put yourself on the map! And other resources

December 7th, 2007

Scott Reynolds kindly created us alt.netters something new to be addicted to checking. As if I didn’t already have enough. I’ve already started an email thread with a guy in Brecksville, not far from my home in Fairview Park. Here are a few more resources for those interested in learning more about the alt.net lifestyle:

Introduction to Monorail (not mine)

September 1st, 2007

I’ve been working on an introduction to Monorail. Admittedly, it’s been taking me awhile to get it done. In the process, I found an article that presents a good overview of getting started with Monorail.

Enjoy that! Hopefully I’ll have my introduction up soon.

Update (9/24): I haven’t forgotten about this and hope to have it finished soon, when/if my personal/professional workload subsides somewhat.