我无法弄清楚为什么我收到以下代码的错误。请帮忙。
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
'
答案 0 :(得分:1)
试试这个:
def add_two(map_this_array)
map_this_array.map { |num| "#{num} + 2 = " << (num + 2).to_s }
end