Last active
August 29, 2015 13:56
-
-
Save closer/9027916 to your computer and use it in GitHub Desktop.
Symbol#to_procに引数を指定できるようにしてみる ref: http://qiita.com/closer/items/9e162994583b0c56435d
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
| class Symbol | |
| def to_proc(*args) | |
| -> receiver { receiver.send(self, *args) } | |
| end | |
| alias_method :[], :to_proc | |
| end |
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
| puts (0..5).map(&:*[10]) |
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
| [1, 10, 100].each do |number| | |
| puts "#{number} is" | |
| case number | |
| when :<[5] then puts "less than 5." | |
| when :<[50] then puts "less than 50." | |
| when :<[500] then puts "less than 500." | |
| end | |
| case number | |
| when :>[50] then puts "more than 50." | |
| when :>[5] then puts "more than 5." | |
| when :>[0] then puts "more than 0." | |
| end | |
| end |
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
| module Kernel | |
| def less(boundary) | |
| :<[boundary] | |
| end | |
| def more(boundary) | |
| :>[boundary] | |
| end | |
| end | |
| [1, 10, 100].each do |number| | |
| puts "#{number} is" | |
| case number | |
| when less(5) then puts "less than 5." | |
| when less(50) then puts "less than 50." | |
| when less(500) then puts "less than 500." | |
| end | |
| case number | |
| when more(50) then puts "more than 50." | |
| when more(5) then puts "more than 5." | |
| when more(0) then puts "more than 0." | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment