3

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
4
  • It may be related to usage of do and end instead of {}, since the block applies to the last expression here, not to the a method call. And it works with {} instead of do and end. But it also works with do/end and without inner block. So it may be a false trail. Commented Sep 10, 2015 at 11:17
  • Or add bracket like ... a(:a, -> do ... end) Commented Sep 10, 2015 at 11:19
  • Yes, It works, but I thought that {..} and do..end are equivalent Commented Sep 10, 2015 at 11:19
  • yes both will work, you need is to add ( ) Commented Sep 10, 2015 at 11:20

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.