if文
fizz_buzz.rb
def fizz_buzz(n)
if n % 15 == 0
"fizz_buzz"
elsif n % 3 == 0
"fizz"
elsif n % 5 == 0
"buzz"
else
n.to_s
end
end
int = [*1..100]
int.each do |n|
puts fizz_buzz(n)
end
# もしくは下記でもOK
# (1..100).each do |n|
# puts fizz_buzz(n)
# end
fizz_buzz.rb
def fizz_buzz
int = [*1..100]
int.each do |n|
if n % 15 == 0
puts "fizz_buzz"
elsif n % 3 == 0
puts "fizz"
elsif n % 5 == 0
puts "buzz"
else
puts n.to_s
end
end
end
fizz_buzz
case文
fizz_buzz.rb
def fizz_buzz(n)
case 0
when n % 15
"fizz_buzz"
when n % 3
"fizz"
when n % 5
"buzz"
else
n.to_s
end
end
int = [*1..100]
int.each do |n|
puts fizz_buzz(n)
end