From: Jacob Fugal Date: 2006-04-20T02:30:40+09:00 Subject: Re: Symbol#to_proc is just so beautiful On 4/19/06, Berger, Daniel wrote: > > -----Original Message----- > > From: Daniel Schierbeck [mailto:daniel.schierbeck@gmail.com] > > > > When is this ever getting into Ruby Core? > > > > class Symbol > > def to_proc > > proc{|obj| obj.send(self) } > > end > > end > > > > Consider this: > > > > class Numeric > > def positive? > > self > 0 > > end > > end > > > > [1, 2, 3].all? &:positive? => true > > [-1, 2, 3].all? &:positive? => false > > There must be a way to ditch that '&' somehow. I'd prefer this: > > [1,2,3].all?(:positive?) It's possible be adjusting the definition of Enumerable#all? $ cat test.rb class Symbol def to_proc proc{ |obj| obj.send(self) } end end module Enumerable alias :old_all? :all? def all?(non_block=nil, &block) if non_block if block.nil? and non_block.respond_to?(:to_proc) block = non_block.to_proc else raise ArgumentError end end old_all? &block end end class Numeric def positive? self > 0 end def even? (self % 2).zero? end end puts [1, 2, 3].all? puts [1, 2, 3].all? { |i| i < 4 } puts [1, 2, 3].all? { |i| i > 2 } puts [1, 2, 3].all?( :positive? ) puts [1, 2, 3].all?( :even? ) $ ruby test.rb true true false true false I'm not sure if there's anyway to do it without redefining the target function, however. Jacob Fugal