Created
August 4, 2017 13:52
-
-
Save bobisme/7228898bb3745a787adc05a6dbf5603c to your computer and use it in GitHub Desktop.
Playing with ways to call map in Ruby.
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
# Originally posted here: https://glot.io/snippets/es9vhlp4hs | |
# ©2017 Bob. No rights reserved. | |
def some_func(x) | |
x + " sucks" | |
end | |
a = ["primus", "bob", "black hole"] | |
puts "\nthe most sane way" | |
puts a.map{ |x| some_func(x) } | |
puts "\nwhere is this going?" | |
puts a.map(&method(:some_func)) | |
puts "\nleast sane: redefine it as a lamda" | |
some_func = method(:some_func) | |
puts a.map(&some_func) | |
puts "\nyeah, I guess you could do that..." | |
some_func = lambda { |x| x + " sucks" } | |
puts a.map(&some_func) | |
puts "\nok, sure... but the procs only use the first argument" | |
some_func = Proc.new { |x| x + " sucks" } | |
puts a.map(&some_func) | |
class String | |
def some_func | |
self + " sucks" | |
end | |
end | |
puts "\nDON'T do this" | |
puts a.map(&:some_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment