Array containing numbers from 1 to N

I am trying to return an array containing the numbers from 1 to N, where N will never be less than 1 , with the following conditions: If the value is a multiple of 3 : use the value 'Fizz' instead. If the value is a multiple of 5 : use the value 'Buzz' instead. If the value is a multiple of 3 and 5 : use the value 'FizzBuzz' instead. This is what I have right now.

包含从1到N的数字的数组

我试图返回一个包含数字从1到N的数组,其中N永远不会小于1 ,并具有以下条件: 如果该值是3的倍数:则改为使用值'Fizz' 。 如果该值为5的倍数:请改为使用值'Buzz' 。 如果该值是3和5的倍数:则改为使用值'FizzBuzz' 。 这就是我现在所拥有的。 我正朝着正确的方向走吗? def fizzbuzz(n) x = [1..n] x.map { |i| if (i % 3 == 0 && i % 5 == 0) 'FizzBuzz' elsif (

Ruby challange: experienced developers please reply

I'm working on some ruby problems geared towards new developers, but I would like the opinions of experienced developers on this. Sorry for the long post, and I really appreciate your time and opinions. Problem Question Write a function, nearest_larger(arr, i) which takes an array and an index. The function should return another index, j : this should satisfy: (a) arr[i] < arr[j] ,

Ruby challange:有经验的开发者请回复

我正在研究面向新开发人员的一些ruby问题,但我希望有经验的开发人员对此有所意见。 对不起,我很感激你的时间和意见。 问题问题 编写一个函数, nearest_larger(arr, i) ,它接受一个数组和一个索引。 该函数应该返回另一个索引, j :这应该满足: (a) arr[i] < arr[j] ,AND (b)在arr[i] < arr[j]地方没有比j更接近i j2 。 如果有关系(请参阅下面的示例),请选择两个索引中最早的(最左边的)。 如

Ruby substitute for goto

How can I do something like this in Ruby? if variable = something do A do B do D elsif variable = something different do A do B do C do D else do A do C do D A = set of loops with if else B = set of loops with if else C = set of loops with if else D = final steps Looking for a way to accomplish something like this in Ruby. I'm sure this ques

Ruby替代goto

我如何在Ruby中做这样的事情? if variable = something do A do B do D elsif variable = something different do A do B do C do D else do A do C do D A = set of loops with if else B = set of loops with if else C = set of loops with if else D = final steps 在Ruby中寻找一种方式来完成这样的事情。 我确信这个问题是在某个地方回答的,但我不知道它会被称为什

How to change hash keys

I have a hash like this: test => {"QTC-1 test"=>"pass", "QTC-2 test"=>"fail"} I want to take each key in the hash and remove all characters after the numbers, example: "QTC-1 test" should equal "QTC-1" I am close to the solution but not fully there: str = test.keys[0] => "QTC-1 test" new = str.slice(0..(str.index(/d/))) => "QTC-1" But need some hel

如何更改散列键

我有这样的哈希: test => {"QTC-1 test"=>"pass", "QTC-2 test"=>"fail"} 我想把每个关键字都放在散列表中,并删除数字后面的所有字符,例如: “QTC-1测试”应该等于“QTC-1” 我接近解决方案但并不完全: str = test.keys[0] => "QTC-1 test" new = str.slice(0..(str.index(/d/))) => "QTC-1" 但需要一些帮助来做到这一点与散列键。 奖金 将值更改为相应的数字值: 所以如果value = pass,那

How does the 'case' statement work with constants?

I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have the following method: # Note: The 'class_name' parameter is a constant; that is, it is a model class name. def my_method(class_name) case class_name when Article then make_a_thing when Comment then make_another_thing when ... then ... else raise("Wrong #{class_name}!") end end I would like to understand why, in the ca

'case'语句如何与常量一起工作?

我正在使用Ruby 1.9.2和Ruby on Rails 3.2.2。 我有以下方法: # Note: The 'class_name' parameter is a constant; that is, it is a model class name. def my_method(class_name) case class_name when Article then make_a_thing when Comment then make_another_thing when ... then ... else raise("Wrong #{class_name}!") end end 我想了解为什么在上面的case语句中,当我执行my_method(Artic

else structure in Ruby

I have written something like this, the same if-else logic I knew from Visual Basic 6.0, but I am sure there is a better "Ruby way" of writing it. Can you please show me how it would look like in Ruby world? if params[:medication_name].nil? med_name = 'all' elsif params[:medication_name] == 'undefined' med_name = 'all' else med_name = params[:medication_name] end

Ruby中的其他结构

我写过类似这样的东西,这是我从Visual Basic 6.0知道的同样的if-else逻辑,但是我确信有更好的写它的“Ruby方法”。 你能告诉我它在Ruby世界中的样子吗? if params[:medication_name].nil? med_name = 'all' elsif params[:medication_name] == 'undefined' med_name = 'all' else med_name = params[:medication_name] end 对于你的情况,这样的事情: med_name = params[:medication_name] med_name

Why is `return a or b` a void value expression error in Ruby?

This is just fine: def foo a or b end This is also fine: def foo return a || b end This returns void value expression : def foo return a or b end Why? It doesn't even get executed; it fails the syntax check. What does void value expression mean? return a or b is interpreted as (return a) or b , and so the value of return a is necessary to calculate the value of (return a) or

为什么在Ruby中`返回a或b`是一个void值表达式错误?

这很好: def foo a or b end 这也很好: def foo return a || b end 这将返回void value expression : def foo return a or b end 为什么? 它甚至没有被执行; 它没有通过语法检查。 void value expression意味着什么? return a or b被解释为(return a) or b ,因此return a的值对计算(return a) or b的值是必要的,但是由于return从不在原地留下值(因为它从那个位置),它并不旨在在原始位置返回有效值。

Ruby on Rails Switch

This question already has an answer here: How to write a switch statement in Ruby? 21 answers I assume you refer to case/when. case a_variable # a_variable is the variable we want to compare when 1 #compare to 1 puts "it was 1" when 2 #compare to 2 puts "it was 2" else puts "it was something else" end or puts case a_variable when 1 "it was 1" when 2 "it was 2" else "it

Ruby on Rails开关

这个问题在这里已经有了答案: 如何在Ruby中编写switch语句? 21个答案 我假设你参考case / when。 case a_variable # a_variable is the variable we want to compare when 1 #compare to 1 puts "it was 1" when 2 #compare to 2 puts "it was 2" else puts "it was something else" end 要么 puts case a_variable when 1 "it was 1" when 2 "it was 2" else "it was something else" end 编辑

Prawn templates not working

I´m trying this simple script: require 'prawn' template_file_name = File.join(File.dirname(__FILE__), 'template.pdf') pdf_file = Prawn::Document.new(:template => template_file_name) pdf_file.text('Hello World') pdf_file.render_file('output.pdf') There is a template.pdf file in the same dir as the script but the output.pdf comes only with the Hello Wordl Is there anything else that I shoul

虾模板无法正常工作

我正在尝试这个简单的脚本: require 'prawn' template_file_name = File.join(File.dirname(__FILE__), 'template.pdf') pdf_file = Prawn::Document.new(:template => template_file_name) pdf_file.text('Hello World') pdf_file.render_file('output.pdf') 在与脚本相同的目录中有一个template.pdf文件,但output.pdf仅带有Hello Wordl 还有什么我应该关注的? 谢谢 如果你想用另一个PDF文件来标记,并且你正在

Executing grep from Ruby

I want to run the following grep from within a Ruby script: grep "word1" file1.json | grep -c "word2" This will find the number of lines in the file with both words appearing. I could do this with Ruby regex, but it seems that Unix grep is much faster. So my question is how do I run this command within a script and return the result back to a Ruby variable? I'd love to hear alternative

从Ruby执行grep

我想在Ruby脚本中运行以下grep: grep "word1" file1.json | grep -c "word2" 这将找到文件中出现两个单词的行数。 我可以用Ruby正则表达式来做这件事,但看起来Unix的grep速度要快得多。 所以我的问题是如何在脚本中运行此命令并将结果返回给Ruby变量? 我很乐意听到不使用grep的替代解决方案,如果它们的速度更快或更快。 使用反引号执行shell命令: result = `grep "word1" file1.json | grep -c "word2"`