【问题标题】:Ruby Array - Beginner Learning about Arrays and MethodsRuby Array - 数组和方法的初学者学习
【发布时间】:2014-01-13 04:01:06
【问题描述】:

我正在尝试解决 Ruby 教程中的一个问题,它询问以下问题 -

创建一个名为 new_array 的方法。它应该接受四个参数并返回一个具有相同参数的数组。首先定义方法结构:**

def new_array(a,b,c,d)
  # return an array consisting of the arguments here
end

然后它提供 Specs 对问题进行逆向工程。**

describe "new_array" do
  it "creates an array of numbers" do
    new_array(1,2,3,4).should eq([1,2,3,4])
  end
  it "creates an array of strings" do
    new_array("a", "b", "c", "d").should eq(["a", "b", "c", "d"])
  end
end

describe "first_and_last" do
  it "creates new array with numbers" do
    first_and_last([1,2,3]).should eq([1,3])
  end
  it "creates new array with strings" do
    first_and_last(["a", "b", "c", "d"]).should eq(["a", "d"])
  end
end

您将从您的解决方案中找到以下内容:

  • new_array 创建一个数字数组
  • new_array 创建一个字符串数组

到目前为止,我有以下代码,但似乎无法在方法 new_array 中放置另一个数组,以便我也可以生成一个字符串数组。

def new_array(a,b,c,d)
  # return an array consisting of the arguments here
  numbers = [1,2,3,4]
  string = ["a","b","c","d"]


end

new_array(1,2,3,4)
new_array("a","b","c","d")

如何在上述方法结构中获取四个参数并返回具有相同参数的数组?

【问题讨论】:

  • 可能是因为它需要两种方法,new_arrayfirst_and_last,所以您无法在教程中继续前进? @xdazz 的回答肯定会奏效。能否提供教程链接?
  • @CarySwoveland 感谢您的回复。实际上, first_and_last 方法并不重要,因为我还没有到达那里。我只是坚持如何在方法中直观地表示两个数组。但是,您是对的,xdazz 的答案应该有效。不幸的是,我没有分享教程的好方法。
  • 您的评论“直观地表示方法中的两个数组”向我表明您没有以正确的方式看待这个问题。当您将参数 x、y、z 传递给 new array 时,它们可以是任何对象。也许 x 是一个 Fixnum,比如 3,y 是类 Hash 的一个实例,而 y 是另一个方法。 new array 只不过是创建一个数组,将这些对象粘贴到数组中(以相同的顺序)并将数组传回给您。 new array 不在乎那些对象是什么——它们只是对象。所以,如果你传递了所有的 Fixnums,你会得到一个包含这些 Fixnums 的数组,以此类推。

标签: ruby arrays methods


【解决方案1】:

只是

def new_array(a,b,c,d)
  [a, b, c, d]
end

对于无限的参数,你也可以这样做:

def new_array(*arguments)
  arguments
end

【讨论】:

  • 感谢您的回答。但是我试过了,但它不满足答案,因为它不允许我在教程中继续前进。这是我所拥有的
  • @user1507375 至少它会让你的测试成功。其他要求是什么?
  • def new_array(a,b,c,d) # 返回一个由这里的参数组成的数组 [1,2,3,4] ["a","b","c", "d"] end new_array(1,2,3,4) new_array("a","b","c","d")
    这就是我所拥有的
    它满足了字符串元素,但我收到一个错误
    new_array 创建一个数字数组 RSpec::Expectations::ExpectationNotMetError 预期:[1,2,3,4] got: ["a", "b", "c" , "d"] (比较使用 ==) exercise_spec.rb:5:in `block (2 levels) in '
  • 如果您查看给出的规范,它也在寻找称为 first_and_last 的第二种方法,这将是他的代码未通过的部分原因。
【解决方案2】:

你在哪里:

def new_array(a,b,c,d)
  # return an array consisting of the arguments here
  numbers = [1,2,3,4]
  string = ["a","b","c","d"]
end

您的代码是准确的,但您没有返回任何内容。尝试再添加一行 numbers 以返回它。像这样:

def new_array(a, b, c, d)
  # return an array consisting of the arguments here
  numbers = [1, 2, 3, 4]
end

对于您的教程而言,可能会起作用。在实践中,xdazz 的答案是 100% 正确

【讨论】:

    【解决方案3】:

    本教程似乎需要两种方法:first_and_lastnew_array

    first_and_last 总是做同样的事情,所以我们先定义它。

    def first_and_last(*args)
        [args[0], args[-1]] #returns a new array containing first and last arguments
    end
    

    对于new_array,您有两种选择。一种只接受 4 个参数,另一种接受无限数量的参数。

    def new_array(a, b, c, d) #only takes four arguments
        [a, b, c, d] #returns a new array containing those arguments in order given
    end
    
    def new_array(*args) #takes unlimited arguments
        args #same thing, but more efficient on typing
    end
    

    first_and_last 当然也一样,格式类似。但是,这应该通过给定的规格。

    最重要的是,看起来你写的方法有点不对劲。 Ruby 返回方法中任何操作返回的最后一个值,设置一个值也会返回该值,eval 会告诉你。例如,代码x = 3 返回整数值3。因此,由于您将 new_array 方法定义为仅定义数字和字符串数组,并且字符串数组是第二个定义的,因此它将始终返回字符串数组的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      相关资源
      最近更新 更多