Fizz Buzz in Ruby for dummies

折月煮酒 提交于 2019-12-04 16:20:52

Thats ok being a novice, we all have to start somewhere right? Ruby is lovely as it get us to use blocks all the time, so to count to 100 you can use several methods on fixnum, look at the docs for more. Here is one example which might help you;

1.upto 100 do |number|
  puts number
end

For your second question maybe take a quick look at the small implementation i whipped up for you, it hopefully might help you understand this problem:

 1.upto 100 do |i|
  string = ""

  string += "Fizz" if i % 3 == 0
  string += "Buzz" if i % 5 == 0

  puts "#{i} = #{string}"

end

First question: this problem has several solutions. For example,

10.times { |i| puts i+1 }

For true novice: https://github.com/bbatsov/ruby-style-guide

another method that can be helpful :

 puts (1..100).map {|i|
  f = i % 3 == 0 ? 'Fizz' : nil
  b = i % 5 == 0 ? 'Buzz' : nil
  f || b ? "#{ f }#{ b }" : i
    }

In Regards to your failed code, your conditional statements should be like this:

if number % 3 == 0
   puts "Fizz"
end
if number % 5 == 0
   puts "Buzz"
end

You don't want the last elsif statement because it will never get executed (if a number is not divisible by 3 or divisible by 5, then it is certainly not divisible by 15) Adjust for this by changing the second elsif to simply and if and if the number is divisble by 5 and not by 3, then Fizz will not be outputted but Buzz Will be

I'm just showing you how to correct your code, but as others have pointed out, there are far more elegant solutions in Ruby.

Not the most beautiful way to write it but good for beginners and for readability.

def fizzbuzz(n)
  (1..n).each do |i|
    if i % 3 == 0 && i % 5 == 0
      puts 'fizzbuzz'
    elsif i % 3 == 0
      puts 'fizz'
    elsif i % 5 == 0
      puts 'buzz'
    else
      puts i
    end
  end
end

fizzbuzz(100)
StateOfB
1.upto(100).each do |x| # Question #1 The 'upto' method here takes is  
                        # what you would use to count in a range.               
  if (x % 3 == 0) && (x % 5 == 0)
    puts " Fizzbuzz"
  elsif x % 3 == 0
    puts " Fizz"
  elsif x % 5 == 0
    puts " Buzz"
  else
    puts x
  end
end

Question #2 Yes you can but I would look for a more elegant way to write this as a part of a definition like

def fizzbuzz(last_number)
  1.upto(last_number).each do |x|              
    if (x % 3 == 0) && (x % 5 == 0)
      puts " Fizzbuzz"
    elsif x % 3 == 0
      puts " Fizz"
    elsif x % 5 == 0
      puts " Buzz"
    else
      puts x
    end
  end
end

This is the answer that helped me to understand that no variables are being created with the .each method. Sorry about my indenting. Still learning how to use Stackoverflow text editing.

Here is my most "idiomatic ruby" solution:

class FizzBuzz
  def perform
    iterate_to(100) do |num,out|
      out += "Fizz" if num.divisable_by?(3)
      out += "Buzz" if num.divisable_by?(5)
      out || num
    end
  end

  def iterate_to(max)
    (1..max).each do |num|
      puts yield num,nil
    end
  end
end

class Fixnum
  def divisable_by?(num)
    self % num == 0
  end
end

class NilClass
  def +(other)
    other
  end
end

FizzBuzz.new.perform

And it works:

https://gist.github.com/galori/47db94ecb822de2ac17c

First question:

for i in 1..100
    puts i
end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!