13

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...

我错过了什么吗?

4

3 回答 3

21

我认为新的语法应该是

match "/", to:  ->(e) { [404, {}, ["Hello! I am micro rack app"]] }, via: [:get]
于 2013-05-19T08:20:07.677 回答
9

我认为语法应该是这样的。

->(e) { [404, {}, ["Hello! I am micro rack app"]]
于 2013-05-19T08:20:17.323 回答
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”语法,因为它更红。

于 2014-02-12T00:05:08.140 回答