This comment is reply for 4274 cho45: 完璧に出遅れで題意に従っているかあやしい...(変形Fizz-Buzz問題). Go to thread root.
cho45 #4285(2007/11/21 06:25 GMT) [ Ruby ] Rating1/1=1.00
せっかく Ruby なので、もうすこし踏みこんで 1.8 系でも動くようにしました。
TrueClass/FalseClass は true/false のクラスで、それを拡張しています。.result を付けくわえたのは if 文 (のようにみえるもの) が値を持つようにみせかけたかったからです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!ruby -Ku require "pp" class TrueClass def then @ret = yield self end def else self end def result @ret end end class FalseClass def then self end def else @ret = yield self end def result @ret end end (1..20).each do |i| puts "%2d:%s" % [i, (i % 3 == 0).then { (i % 5 == 0).then { "FizzBuzz" }.else { "Fizz" }.result }.else { (i % 5 == 0).then { "Buzz" }.else { "hoge" }.result }.result ] end
Rating1/1=1.00-0+
[ reply ]
cho45
#4285()
[
Ruby
]
Rating1/1=1.00
せっかく Ruby なので、もうすこし踏みこんで 1.8 系でも動くようにしました。
TrueClass/FalseClass は true/false のクラスで、それを拡張しています。.result を付けくわえたのは if 文 (のようにみえるもの) が値を持つようにみせかけたかったからです。
Rating1/1=1.00-0+