Skip to content

Instantly share code, notes, and snippets.

@yswallow
Created April 18, 2017 20:49
Show Gist options
  • Select an option

  • Save yswallow/2ef6503e3df0e97956b89071ed008f19 to your computer and use it in GitHub Desktop.

Select an option

Save yswallow/2ef6503e3df0e97956b89071ed008f19 to your computer and use it in GitHub Desktop.
RubyにSmallTalk的な「メソッドとしての条件分岐」を追加する
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
}
}
}
}
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