[Ironruby-core] Linq etc.

John Messerly jomes at microsoft.com
Mon Jan 21 18:45:56 EST 2008

Mike Moore:

> John, my understanding is the Where() and OrderBy() methods are
> extension methods defined in System.Linq.Enumerable and only available
> in C# if you include a reference to the System.Linq namespace in your
> C# code.  Are you saying that extension methods are available from
> IronRuby without having to require the namespace?  If not, how do we
> make them available?
> Here is an example using an extension method with the signature
> "public bool IsGuid(this String str)" in the class MyString in the
> namespace MyAssembly.MyStuff.
> C# pseudo code:
> using MyAssembly.MyStuff ;
> public class MyStupidApp() {
>  public void DoStuff(string val) {
>     if (val.IsGuid()) Console.WriteLine("Guid!)
>   }
> }
> What is the equivalent Ruby code?  Do we need to require the assembly?
> require 'MyAssembly.MyStuff'
> def do_stuff(val)
>   puts 'Guid' if val.is_guid
> end
> Or are the Linq extension methods the only ones that will be available?

Yup, you need to require the assembly. If you were writing Linq in Ruby, you'd have something like System.Core.rb:

module Enumerable
  def order_by
  def where

... so requiring System.Core just opens up Enumerable and sticks some methods on it.
At least, that's been my thinking on it. Maybe we add an extra step like:

require 'System.Core'
include System::Linq

I'm not sure how Ruby-ish that is, though.

- John

More information about the Ironruby-core mailing list