映射语法错误,意外的tIDENTIFIER

时间:2015-04-26 21:25:02

标签: ruby-on-rails

我无法弄清楚为什么我收到以下代码的错误。请帮忙。

def add_two(map_this_array)
  map_this_array.map { |num| "#{num} + 2 = "  num + 2 }
end

规格:

describe "add_two" do
  it "adds 2 to each element in an array" do
    a = [1, 2, 3]
    r = ["1 + 2 = 3", "2 + 2 = 4", "3 + 2 = 5"]
    expect( add_two(a) ).to eq(r)
  end
  it "adds 2 to each element in a longer array" do
    a = [5, 7, 3, 12, 15]
    r = ["5 + 2 = 7", 
         "7 + 2 = 9", 
         "3 + 2 = 5", 
         "12 + 2 = 14", 
         "15 + 2 = 17"]
    expect( add_two(a) ).to eq(r)
  end
end

我收到此错误:

exercise_spec.rb:2:require': exercise.rb:2: syntax error, unexpected tIDENTIFIER, expecting '}' (SyntaxError) map_this_array.map { |num| "#{num} + 2 = " num + 2 } ^ from exercise_spec.rb:2:in'

1 个答案:

答案 0 :(得分:1)

试试这个:

def add_two(map_this_array)
  map_this_array.map { |num| "#{num} + 2 = "  << (num + 2).to_s }
end