Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # better ruby form of what you had..
- bottles = 99
- while bottles != 0
- puts "#{bottles} bottles of beer on the wall #{bottles} bottles of beer on the wall"
- bottles -= 1
- puts "Take one down, pass it around, #{bottles} bottles of beer on the wall."
- end
- # more rubyish way to do it...
- 99.downto(1) do |i|
- puts "#{i} bottles of beer on the wall " * 2
- puts "Take one down, pass it around, #{i - 1} bottles of beer on the wall."
- end
- # or my code golf solution...
- c=' on the wall'
- def v(i)"#{i} bottle#{i>1?'s':''} of beer"end
- 99.downto(1){|i|print s=v(i),c,', ',s,".
- ",i>1?"Take one down and pass it around, ":"Go to the store and buy some more, ",v(i>1?i-1:99),c,i>1?".
- ":"."}
- # long hand form
- c=' on the wall'
- def bottles_line(i)
- "#{i} bottle#{i > 1 ? 's' : ''} of beer"
- end
- 99.downto(1) do |i|
- str = bottles_line(i)
- puts "#{str}#{c}, #{str}."
- if i > 1
- puts "Take one down and pass it around, #{bottles_line(i - 1)}."
- else
- print "Go to the store and buy some more, #{bottles_line(99)}."
- end
- end
- # more fun
- template = "%d bottles of beer on the wall, %d bottles of beer"
- 99.downto(1) { |i| puts template % [i,i] }
Add Comment
Please, Sign In to add comment