Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # better ruby form of what you had..
  4. bottles = 99
  5. while bottles != 0
  6. puts "#{bottles} bottles of beer on the wall #{bottles} bottles of beer on the wall"
  7. bottles -= 1
  8. puts "Take one down, pass it around, #{bottles} bottles of beer on the wall."
  9. end
  10.  
  11. # more rubyish way to do it...
  12. 99.downto(1) do |i|
  13. puts "#{i} bottles of beer on the wall " * 2
  14. puts "Take one down, pass it around, #{i - 1} bottles of beer on the wall."
  15. end
  16.  
  17. # or my code golf solution...
  18. c=' on the wall'
  19. def v(i)"#{i} bottle#{i>1?'s':''} of beer"end
  20. 99.downto(1){|i|print s=v(i),c,', ',s,".
  21. ",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?".
  22.  
  23. ":"."}
  24.  
  25. # long hand form
  26. c=' on the wall'
  27. def bottles_line(i)
  28. "#{i} bottle#{i > 1 ? 's' : ''} of beer"
  29. end
  30.  
  31. 99.downto(1) do |i|
  32. str = bottles_line(i)
  33. puts "#{str}#{c}, #{str}."
  34. if i > 1
  35. puts "Take one down and pass it around, #{bottles_line(i - 1)}."
  36. else
  37. print "Go to the store and buy some more, #{bottles_line(99)}."
  38. end
  39. end
  40.  
  41.  
  42. # more fun
  43. template = "%d bottles of beer on the wall, %d bottles of beer"
  44. 99.downto(1) { |i| puts template % [i,i] }
Add Comment
Please, Sign In to add comment