簡體   English   中英

映射語法錯誤,意外的tIDENTIFIER

[英]Mapping syntax error, unexpected tIDENTIFIER

我不知道為什么我在以下代碼中出錯。 請幫忙。

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:in 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 require': exercise.rb:2: syntax error, unexpected tIDENTIFIER, expecting '}' (SyntaxError) map_this_array.map { |num| "#{num} + 2 = " num + 2 } ^ from exercise_spec.rb:2:inrequire': exercise.rb:2: syntax error, unexpected tIDENTIFIER, expecting '}' (SyntaxError) map_this_array.map { |num| "#{num} + 2 = " num + 2 } ^ from exercise_spec.rb:2:in

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM