hoodwink.d enhanced

Wed Apr 19

Unroll an iterator when no block is given

class Module def unroll(iterator, unrolled = iterator) old_iterator = "iterator_#{iterator}" alias_method old_iterator, iterator class_eval <<-EVAL, __FILE__, __LINE__ + 1 def #{iterator}(*args, &block) collection = [] unless block_given? send(:#{old_iterator}, *args) do |*objects| # No room for a ternary ;_; if block_given? yield(*objects) else collection.push(*objects) end end collection unless block_given? end EVAL alias_method unrolled, iterator unless iterator == old_iterator alias_method iterator, old_iterator end end end module ObjectSpace class << self include Enumerable unroll :each_object, :objects alias_method :each, :each_object end end p ObjectSpace.objects.size => 368 p ObjectSpace.objects(Array).first => [:iterator_each_object, Array] p ObjectSpace.objects.last(3) => [Class, Module, Object]

Rails core

Marcel M. - the best part of that is the "limbering up" part
Dave T. - If they don't limber up, they tend to pull muscles
Marcel M. - esp. the older ones i'd imagine
Dave T. - the older ones we eat
Marcel M. - pragmatic
Mike C. - geriatric
Dave T. - balsamic
Teams of PDF-aware gerbils are this very moment limbering up to deliver your content.

After requesting a PDF from the Pragmatic Programmers


Jason F. - 7 color sharpies in the hizzie
Jason F. - look out Pointalists

St. Paul Church in Toronto

Rails core

Dave T. - Anyone heard of a filesharing network called Winny?
Dave T. - Like bad English rock bands, it's apparently big in Japan