-
-
Save kschiess/387826 to your computer and use it in GitHub Desktop.
Exploration on to_proc and each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
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
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.