Skip to content

Instantly share code, notes, and snippets.

@kschiess
Forked from floere/symbol_hash_to_a.rb
Created May 3, 2010 06:50
Show Gist options
  • Save kschiess/387826 to your computer and use it in GitHub Desktop.
Save kschiess/387826 to your computer and use it in GitHub Desktop.
Exploration on to_proc and each
# Its easy really, you want the proc to 'plus' to arguments really, so
# just put a plus in there!
# (Or: Most flagrant abuse of unary plus)
class AsArgument < Struct.new(:symbol)
def to_proc
proc { |el| send(symbol, el) }
end
end
class Symbol
def +@
AsArgument.new(self)
end
end
a = ["hello", "HeLlO"]
a.each(&+:p)
@floere
Copy link

floere commented May 3, 2010

Big thumbs up! And a nice solution too.
I really wanted to use the splat since it is such a "Map-Reduce-Thingy". I (apparently falsely) assumed it would call try to call to_ary before calling to_a… Will need to investigate further.

@kschiess
Copy link
Author

kschiess commented May 3, 2010

yep. Based on the assumption that Ruby enforces the array return on splat, I just used something equally short and memorable. But of course, the splat would be much more related to the problem - good luck in that search ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment