在Ruby中嘶嘶作响的假人

时间:2023-02-13 21:54:26

Spoiler alert: I am a true novice. Tasked with figuring out fizz buzz in ruby for a class and while I have found more than a few versions of code that solve the problem, my understanding is so rudimentary that I cannot figure out how these examples truly work.

剧透警告:我是一个真正的新手。我的任务是为一个类计算ruby中的fizz buzz,虽然我已经找到了不止几个版本的代码来解决这个问题,但我的理解是如此初级,以至于我无法弄清楚这些示例是如何工作的。

First question(refer to spoiler alert if you laugh out loud at this): How do i print out numbers one through 100 in Ruby?

第一个问题(如果你大声笑,请参阅剧透警告):我如何在Ruby中打印出1到100的数字?

Second question: can 'if else" be used to solve this? My failed code is below(attachment has screen shot):

第二个问题:是否可以用if else来解决这个问题?我的失败代码如下(附件有截图):

puts('Lets play fizzbuzz')
print('enter a number: ')
number = gets()
puts(number)
if number == % 3
  puts ('fizz')
elsif number == % 5
  puts ('buzz')
elsif number == %15
  puts ('fizzbuzz')
end

Thanks,

谢谢,

8 个解决方案

#1


4  

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;

做一个新手是可以的,我们都必须从某个地方开始,对吗?Ruby非常棒,因为它让我们可以一直使用块,所以要数到100,您可以在fixnum上使用多种方法,请查看文档以了解更多信息。这里有一个例子可以帮助你;

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

#2


2  

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

对真正的新手:https://github.com/bbatsov/ruby-style-guide

#3


2  

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
    }

#4


1  

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

你不想最后一个elsif语句,因为它永远不会得到执行(如果一个号码是不能被3或5整除整除,那么它肯定不是整除15)调整为通过改变第二个elsif只是如果,如果能被5而不是3,然后饮料不会输出但Buzz

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

我只是向您展示如何修改您的代码,但是正如其他人指出的,Ruby中有更优雅的解决方案。

#5


1  

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)

#6


1  

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

问题2是的,你可以,但是我想找一种更优雅的方式把它作为定义的一部分

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 * text editing.

这是帮助我理解没有使用.each方法创建变量的答案。对不起关于我的缩进。还在学习如何使用*文本编辑。

#7


0  

Here is my most "idiomatic ruby" solution:

下面是我最“惯用的ruby”解决方案:

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

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

#8


-2  

First question:

第一个问题:

for i in 1..100
    puts i
end

#1


4  

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;

做一个新手是可以的,我们都必须从某个地方开始,对吗?Ruby非常棒,因为它让我们可以一直使用块,所以要数到100,您可以在fixnum上使用多种方法,请查看文档以了解更多信息。这里有一个例子可以帮助你;

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

#2


2  

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

对真正的新手:https://github.com/bbatsov/ruby-style-guide

#3


2  

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
    }

#4


1  

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

你不想最后一个elsif语句,因为它永远不会得到执行(如果一个号码是不能被3或5整除整除,那么它肯定不是整除15)调整为通过改变第二个elsif只是如果,如果能被5而不是3,然后饮料不会输出但Buzz

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

我只是向您展示如何修改您的代码,但是正如其他人指出的,Ruby中有更优雅的解决方案。

#5


1  

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)

#6


1  

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

问题2是的,你可以,但是我想找一种更优雅的方式把它作为定义的一部分

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 * text editing.

这是帮助我理解没有使用.each方法创建变量的答案。对不起关于我的缩进。还在学习如何使用*文本编辑。

#7


0  

Here is my most "idiomatic ruby" solution:

下面是我最“惯用的ruby”解决方案:

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

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

#8


-2  

First question:

第一个问题:

for i in 1..100
    puts i
end