Created
April 18, 2017 20:49
-
-
Save yswallow/2ef6503e3df0e97956b89071ed008f19 to your computer and use it in GitHub Desktop.
RubyにSmallTalk的な「メソッドとしての条件分岐」を追加する
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
| require './smalltalklike' | |
| 100.times { |i| | |
| ( i%15 == 0 ).if { | |
| puts 'FizzBuzz' | |
| }.else { | |
| ( i%3 == 0 ).if { | |
| puts 'Fizz' | |
| }.else { | |
| ( i%5 == 0 ).if { | |
| puts 'Buzz' | |
| }.else { | |
| puts i | |
| } | |
| } | |
| } | |
| } |
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 SmallTalkLike | |
| def if( lmda = nil ) | |
| if self | |
| if lmda | |
| lmda.call | |
| else | |
| yield | |
| end | |
| end | |
| return self | |
| end | |
| def else( lmda = nil ) | |
| unless self | |
| if lmda | |
| lmda.call | |
| else | |
| yield | |
| end | |
| end | |
| return self | |
| end | |
| end | |
| class TrueClass; include SmallTalkLike; end | |
| class FalseClass; include SmallTalkLike; end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment