I came across this :
elements.sort_by(&
osition).each
Loosely-related aside:
I've been learning Io[1] recently. Io (like Lisp, I gather) allows
lazy evaluation of method arguments. In Ruby terms, this would mean
that I can write something like this:
# Ruby-esque pseudo-code; neither Io nor Ruby
class Array
def select
result = []
self.each{ |el| result << el if el.sendArg(0) }
result
end
end
my_array.select( isCool? )
my_array.select( > 5 )
my_array.select( roughlyEquals( jim.newSize ) )
In Io, the "isCool?" method/message isn't (necessarily) evaluated when
you call the select method. Instead, you can perform some
introspection on the parsed message tree for each argument and choose
to ignore it, change it to a string, or send it as a message to any
object you want.
To be clear, in the above, the "isCool?" message/method would be send/
invoked on each array element. Or each element would be sent a
">( 5 )" message.
The Symbol#to_proc technique is clever, but not quite as clean as
being able to write (in Io):
elements sortBy( position )
elements map( * 2 )
elements select( size > 5 )
This same ability in Io allowed me to add a debugging method "p" (in
homage of Ruby) that labels a value with the exact call you made. For
example:
# In Io with my custom method (not Ruby)
p( gk )
#=> gk is Person_0x4de0d8:
#=> name = "Gavin Kistner"
#=> nick = "Phrogz""
p( gk name )
#=> gk name is Gavin Kistner
p( gk nick size )
#=> gk nick size is 6
[1]
http://www.iolanguage.com/