【发布时间】:2020-04-03 18:56:14
【问题描述】:
我想知道是否有办法避免在 Bottle_of_beer 变量上编写 .to_s 方法? (DRY原则)
bottles_of_beer = 99
while bottles_of_beer != 1
beer_left = bottles_of_beer - 1
bottles_of_beer = beer_left
puts bottles_of_beer.to_s + ' bottles of beer on the wall ' + bottles_of_beer.to_s + ' bottles of beer.'
puts 'Take one down and pass it around, ' + beer_left.to_s + ' of beer on the wall.'
end
if bottles_of_beer == 1
puts bottles_of_beer.to_s + ' bottle of beer on the wall, ' + bottles_of_beer.to_s + ' bottle of beer.'
puts 'Take one down and pass it around, no more bottles of beer on the wall.'
end
puts 'No more bottles of beer on the wall, no more bottles of beer.'
puts 'Go to the store and buy some more, 99 bottles of beer on the wall.'
我想知道是否有办法避免在 Bottle_of_beer 变量上编写 .to_s 方法?
【问题讨论】:
-
源代码请使用appropriate formatting。
标签: ruby string variables methods dry