1

Why is the following returning an error?

[1, 2, 3, 4].each( { |num| puts num } )

Why is this not equivalent to:

[1, 2, 3, 4].each { |num| puts num }

What am I missing here about the 'everything is a method' hence should follow the same pattern and parantheses can be omitted if there is no ambiguity methodology?

2
  • The different argument types are covered in Calling Methods Commented Apr 3, 2014 at 14:34
  • I cannot parse your last paragraph. And, what is 'everything is a method'? I have never heard of it. Commented Apr 3, 2014 at 14:43

3 Answers 3

7

Because that's syntactically invalid. That isn't a supported syntax for passing a block to a method.

Ruby's syntax allows for one special block argument to each method, apart from the regular arguments. The argument is defined differently (via the { |args| ... } syntax) and (optionally) accepted differently through a final &variable parameter:

def my_method(arg1, arg2, &block)
  block.call("ok1")
  # or, a special syntax for calling the &block:
  yield "ok!"
end

my_method(value_for_arg_1, value_for_arg_2) do |arg|
  # when invoked by my_method, arg will be "ok!"
end

You can pass the block as part of the argument list, but again, a specific syntax is required:

my_block = Proc.new { puts "I'm a proc" }

my_method(value1, value2, &my_block)

The above &my_block is required only if you intend to pass the proc through as the &block parameter; you can pass an arbitrary number of procs through without using &, but they aren't callable via yield.

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

8 Comments

That isn't a supported syntax for passing a block to a method. - Not a concrete statement. The way OP write the code, MRI never will treat/know that it is a block. It will treat it as a Hash. But Hash syntax is not as per the Ruby Hash syntax, so the error popped up. It is not due to the valid block IMHO
@ArupRakshit That is absolutely a concrete statement. The syntax is not supported. Full stop. I don't have to elucidate why that's invalid syntax; you might as well ask why I can't write if x in C++ instead of if (x): It's not valid syntax.
Yes.. It is not to the point.. What I just said, is the actual point.
I don't know other languages, I wouldn't consider them also. I am in Ruby. My opinions are only based on Ruby. You said it is not a valid syntax for passing blocks.It should be by observation, It is not a valid syntax of passing Hash. Error comes due to wrong syntax of Hash, not blocks.
He's not trying to pass a hash. He's trying to pass a block. That it is interpreted as a hash is incidental and unrelated. It isn't a valid syntax for defining a block.
|
1

Because a block is not an object. An argument must be an object. You cannot replace an argument with a block or vice versa. You cannot enclose a block within parentheses and treat it like an argument.

Comments

0

Because if you pass block into method (like in this case), you put it outside parantheses, even if you use them for other arguments.

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.