1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

色々なfizz_buzz(逐次追記)

Last updated at Posted at 2019-10-03

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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?