Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created January 11, 2016 23:31
Show Gist options
  • Save domgetter/86ad84366e6b30da4775 to your computer and use it in GitHub Desktop.
Save domgetter/86ad84366e6b30da4775 to your computer and use it in GitHub Desktop.
["hello", "world", "hahahahaha"].map(&:length)
#=> [5, 5, 10]
# So, what's happening is, :length gets #to_proc called on it, which results in map doing the following:
["hello", "world", "hahahahaha"].map do |str|
:length.to_proc.call(str)
end
# Now, :length.to_proc.call(str) does the following:
str = "hello"
str.send(:length)
#=> 5
# try it yourself!
:+.to_proc.call(2, 3) == 2.send(:+, 3)
#=> true
# str.send(:length) is (basically) the same as
str.length
#=> 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment