[Ironruby-core] Linq etc.

Brent Rowland brent.rowland at gmail.com
Tue Jan 22 16:00:46 EST 2008


Rather than monkey patching Enumerable--thereby affecting (and possibly
tromping over) every object of every class that includes it--how about
extending objects on a case-by-case basis?

I've made good use of patterns like the following:

foobar = foo.map { |f| bar(f) }.extend(Statistics)
avg = foobar.avg(:bam)

(where Statistics is a mixin module that implements sum, avg, etc, which
make use of the enumerable methods)

This way any function--including 3rd-party code--that returns an array or
other enumerable type can be extended on-the-fly and new functions can
return "pre-enriched" collections when appropriate.

numbers = [5, 10, 8, 3, 6, 12].extend(System::Linq)
numbers.where { |num| num % 2 == 0 }.order_by { |n| n }.each { |i|
write("#{i} ") }

Brent


On Jan 21, 2008 4:57 PM, Mike Moore <blowmage at gmail.com> wrote:

>
>
> On Jan 21, 2008 4:45 PM, John Messerly <jomes at microsoft.com> wrote:
>
> >
> > 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
> >    ...
> >  end
> >  def where
> >    ...
> >  end
> > end
> >
> > ... so requiring System.Core just opens up Enumerable and sticks some
> > methods on it.
> > At least, that's been my thinking on it.
>
>
> I gotcha.  So in your example System.Core.rb was monkey-patching
> Enumerable.  You weren't actually resolving those method calls to the
> extension methods in the System.Linq.Enumerable class.
>
>
> > Maybe we add an extra step like:
> >
> > require 'System.Core'
> > include System::Linq
> >
> > I'm not sure how Ruby-ish that is, though.
> >
>
> I agree.  In C# you add the namespace and those extension methods are only
> available in that source file.  So you have to add the namespace to each
> file you want to use the extension methods.  But in Ruby once they are added
> to the object they are available everywhere.  So I wonder what a Ruby-ish
> solution would be.  Does anyone on the list have any suggestions how
> enabling extension methods could/should look?
>
>
> > - John
> >
> > _______________________________________________
> > Ironruby-core mailing list
> > Ironruby-core at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/ironruby-core
> >
>
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20080122/cf2d8c29/attachment.html 


More information about the Ironruby-core mailing list