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?<br><br>I've made good use of patterns like the following:
<br><br><span style="font-family: courier new,monospace;">foobar = foo.map { |f| bar(f) }.extend(Statistics)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">avg = foobar.avg
(:bam)</span><br style="font-family: courier new,monospace;"><br>(where Statistics is a mixin module that implements sum, avg, etc, which make use of the enumerable methods)<br><br>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.
<br><br><span style="font-family: courier new,monospace;">numbers = [5, 10, 8, 3, 6, 12].extend(System::Linq)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">numbers.where
{ |num| num % 2 == 0 }.order_by { |n| n }.each { |i| write("#{i} ") }</span><br style="font-family: courier new,monospace;"><br>Brent<br><br><br><div class="gmail_quote">On Jan 21, 2008 4:57 PM, Mike Moore <
<a href="mailto:blowmage@gmail.com">blowmage@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br><br><div class="gmail_quote">
<div class="Ih2E3d">On Jan 21, 2008 4:45 PM, John Messerly <<a href="mailto:jomes@microsoft.com" target="_blank">jomes@microsoft.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><br></div>Yup, you need to require the assembly. If you were writing Linq in Ruby, you'd have something like System.Core.rb:<br><br>module Enumerable<br> def order_by<br> ...<br> end<br> def where
<br> ...<br> end<br>end<br><br>... so requiring System.Core just opens up Enumerable and sticks some methods on it.<br>At least, that's been my thinking on it. </blockquote></div><div><br>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.<br> </div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Maybe we add an extra step like:
<br><br>require 'System.Core'<br>include System::Linq<br><br>I'm not sure how Ruby-ish that is, though.<br><div><div></div><div></div></div></blockquote></div><div><br>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?
<br> </div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div>- John<br><br>_______________________________________________
<br>Ironruby-core mailing list<br><a href="mailto:Ironruby-core@rubyforge.org" target="_blank">Ironruby-core@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/ironruby-core" target="_blank">http://rubyforge.org/mailman/listinfo/ironruby-core
</a><br></div></div></blockquote></div></div><br>
<br>_______________________________________________<br>Ironruby-core mailing list<br><a href="mailto:Ironruby-core@rubyforge.org">Ironruby-core@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/ironruby-core" target="_blank">
http://rubyforge.org/mailman/listinfo/ironruby-core</a><br><br></blockquote></div><br>