Skip to content

Instantly share code, notes, and snippets.

@st98
Last active December 28, 2015 04:39
Show Gist options
  • Save st98/7443634 to your computer and use it in GitHub Desktop.
Save st98/7443634 to your computer and use it in GitHub Desktop.
Rubyの練習。99 Bottles of Beerです。
def bottles_of_beer(n)
def bottle(n)
bottles = case n
when 0
"No more bottles"
when 1
"#{n} bottle"
else
"#{n} bottles"
end
"#{bottles} of beer"
end
def take_down(n)
if n > 0 then
"Take one down and pass it around"
else
"Go to the store and buy some more"
end
end
n.downto(0) do |i|
s = <<-"eos"
#{bottle i} on the wall, #{bottle i}.
#{take_down i}, #{bottle i == 0 ? n : i - 1} on the wall.
eos
if block_given? then
yield s
else
puts s + "\n"
end
end
end
bottles_of_beer(99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment