Codeacademy实际上传递了这个代码是正确的,但是在使用irb构建时遇到了这些错误。我迷路了!
错误:(ruby):1:警告:发现=有条件的,应该是== (ruby):8:语法错误,意外的keyword_end,期待$ end
def alphabetize(arr, rev=false)
if rev = true
arr.sort! { |item1, item2| item2 <=> item1 }
else
arr.sort! { |item1, item2| item1 <=> item2 }
end
end
puts arr
end
alphabetize("the", "world", "is", "a", "vampire")
答案 0 :(得分:3)
将if rev = true
写为if rev == true
(但更多Rubyish为if rev
)。 <{1}}还在end
之上,删除它。
puts arr
答案 1 :(得分:1)
正确的代码应该是这样的:
def alphabetize(arr, rev=false)
if rev == true
arr.sort! { |item1, item2| item2 <=> item1 }
else
arr.sort! { |item1, item2| item1 <=> item2 }
end
puts arr
end
alphabetize(["the", "world", "is", "a", "vampire"])
您有额外的end
,rev==true
是比较,您使用的是rev=true
另一个错误是调用方法时需要传递一个数组