This post originated from an RSS feed registered with Ruby Buzz
by john hume.
Original Post: Module#to_proc h0tness
Feed Title: El Humidor
Feed URL: http://feeds.feedburner.com/ElHumidor
Feed Description: John D. Hume on Ruby and development with agile teams.
I don't need this right now, so I'm not putting it into any of the
codebases I touch. But if I ever do need it, I haven't thought of
a reason yet that I shouldn't pull it in.
describe "Module#to_proc"do
it "provides a proc that mixes the module into the yielded object"do
# Say you've got some stuff,
stuff =["jimmy","jane",Object.new]# and you want to have all that stuff extend a certain Module.
moduleFooabledeffoo()"This #{self.class} is fooing";endend# stuff.each {|thing| thing.extend Fooable } is just so many
# characters to type!
# With a little hackery in Module, you can do this.
stuff.each &Fooable# Now all that stuff will have your module mixed in,
stuff.each {|thing| thing.should be_a_kind_of(Fooable)}# and have interesting new behavior.
stuff.collect {|o| o.foo }.should ==["This String is fooing","This String is fooing","This Object is fooing"]endend
Here's all it takes to make it pass, plus some simple memoization so it's cheaper to call.