Ruby有lambda语法,所以我可以使用->
符号:
a = 0
new -> { a < 5 } do
puts a
a += 1
end
这非常有效,但是当我尝试这样做时:
match "/", to: -> { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get]
match( "/", to: -> { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )
match( "/", { to: -> { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] })
所有返回相同的语法错误:
$ ruby -c -e 'match( "/", to: -> { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )'
-e:1: syntax error, unexpected '|'
match( "/", to: -> { |e| [404, {}, ["Hello! I am mi...
我错过了什么吗?
答案 0 :(得分:21)
我认为新语法应该是
match "/", to: ->(e) { [404, {}, ["Hello! I am micro rack app"]] }, via: [:get]
答案 1 :(得分:9)
我认为语法应该是这样的。
->(e) { [404, {}, ["Hello! I am micro rack app"]]
答案 2 :(得分:1)
您似乎在混合->
和lambda
语法
match( "/", to: lambda { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )
和
match( "/", to: -> (e) { [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )
我个人会使用'lambda'语法,因为它更加红宝石。