CuriousMind
CuriousMind

Reputation: 34145

How do I use Ruby's new lambda syntax?

Ruby has lambda syntax, so I can use the -> symbol:

a = 0
new  -> { a < 5 }  do
   puts a
   a += 1
end

This works very well, but when I try to do this:

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] })

all of the return the same syntax error:

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

Am I missing something?

Upvotes: 13

Views: 11561

Answers (3)

G. Allen Morris III
G. Allen Morris III

Reputation: 1042

It seems you are mixing -> and lambda syntax

match( "/", to:  lambda { |e| [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )

And

match( "/", to:  -> (e) { [404, {}, ["Hello! I am micro rack app"]] }, via: [:get] )

Personally I would use the 'lambda' syntax as it is more rubyish.

Upvotes: 1

user946611
user946611

Reputation:

I think the syntax should be like this.

->(e) { [404, {}, ["Hello! I am micro rack app"]]

Upvotes: 9

lis2
lis2

Reputation: 4264

I think that new syntax should be

match "/", to:  ->(e) { [404, {}, ["Hello! I am micro rack app"]] }, via: [:get]

Upvotes: 21

Related Questions