Created
November 21, 2008 05:46
-
-
Save nkpart/27368 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| numbers = %w{1.3 1.5 1.8} | |
| puts "# 1 - standard stuff" | |
| p numbers.map { |x| x.to_f } | |
| puts "# 2 - passing in a proc using &" | |
| to_f_proc = proc { |x| x.to_f } | |
| p numbers.map(&to_f_proc) | |
| puts "# 3 - passing in a dynamic proc" | |
| def sender method | |
| proc { |x| x.send(method) } | |
| end | |
| p numbers.map(&sender(:to_f)) | |
| puts "# 4 - ruby automagically calls to_proc on what's passed in as the block" | |
| class Sender | |
| def initialize sym | |
| @sym | |
| end | |
| def to_proc | |
| proc { |x| x.send(@sym) } | |
| end | |
| end | |
| p numbers.map(&Sender.new(:to_f)) | |
| puts "# 5 - monkey patching it onto symbol" | |
| class Symbol | |
| def to_proc | |
| proc { |x| x.send(self) } | |
| end | |
| end | |
| p numbers.map(&:to_f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment