Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby lambda syntax issue

Tags:

ruby

lambda

I have some Ruby code

def a(x, y)
  puts x, y.call
end

a :a, -> do
  [1, 2, 3].map! do |j|
    j
  end
end

I'm almost sure that it's correct, editor highlights it as correct, but I have such exception:

SyntaxError: (irb):6: syntax error, unexpected keyword_do_block, expecting keyword_end
  [1, 2, 3].map! do |j|
                   ^
(irb):9: syntax error, unexpected keyword_end, expecting end-of-input
like image 357
Alex Tonkonozhenko Avatar asked Jul 12 '26 15:07

Alex Tonkonozhenko


1 Answers

If I'm not totally mistaken, you need to wrap the method call in parentheses like this

def a(x, y)
  puts x, y.call
end

a(:a, -> do
  [1, 2, 3].map! do |j|
    j
  end
end)

Now there still is problem that you are passing two parameters to puts where only one is allowed, so you would need to concatenate the string with + or some other way.

like image 88
Roope Hakulinen Avatar answered Jul 15 '26 12:07

Roope Hakulinen