# Why can't I call this without parentheses?

**URL:** <https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141>\
**Category:** Rails\
**Created:** [June 26, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141 "2006-06-26T19:50:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [June 26, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141/1 "2006-06-26T19:50:09Z")

</div>

I have a little helper that lets me add legacy fields to models where  
they have common prefixes, suffixes, or something else weird. Here is  
the method signature:

def legacy\_fields(options = {}, \*attributes)  
…  
end

options is a hash containing things like :prefix =\> “post\_” - and  
attributes is a list of attribute names. This gets mixed into the  
class with extend, so I call it much like the attr\* methods. Can  
someone explain why I get syntax errors if I try to call this method  
like this (which is how I want to call it to be consistent with  
rails):

legacy\_fields :prefix = “post\_”, :author, :title

or even this:

legacy\_fields {:prefix = “post\_”}, :author, :title

but this works fine:

legacy fields({:prefix = “post\_”}, :author, :title)

The signature looks similar to things like link\_to and url\_for, which  
can be called without parentheses. Any idea of what I’m missing?

thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [June 26, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141/2 "2006-06-26T21:47:38Z")

</div>

> The thing is, the full anatomy of a Ruby method call is:
> 
> receiver.method\_name(arg1, arg2, …) { code block }
> 
> You need the parens when your first argument is a literal hash,  
> because otherwise it will be assumed to be a code block.
> 
> David

Ah that makes sense. I have your book David, and enjoyed it quite a  
bit, btw. Apparently I should re-read some parts of it. 🙂 So,  
basically having a hash as the first parameter just means you always  
need parens, right?

- rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [June 26, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141/3 "2006-06-26T20:26:50Z")

</div>

Hi –

On Mon, 26 Jun 2006, Rob S. wrote:

> class with extend, so I call it much like the attr\* methods. Can  
> but this works fine:
> 
> legacy fields({:prefix = “post\_”}, :author, :title)
> 
> The signature looks similar to things like link\_to and url\_for, which  
> can be called without parentheses. Any idea of what I’m missing?

The thing is, the full anatomy of a Ruby method call is:

receiver.method\_name(arg1, arg2, …) { code block }

You need the parens when your first argument is a literal hash,  
because otherwise it will be assumed to be a code block.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

See what the readers are saying about “Ruby for Rails”!

> **[Ruby on Rails Framework | Ruby Power and Light](https://www.rubypowerandlight.com/)**
>
> Main aspects of the Ruby language with focus on Rails - the most widespread and powerful Web Application Framework of the language, which implements the MVC pattern.

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [June 26, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141/4 "2006-06-26T22:49:47Z")

</div>

On Jun 26, 2006, at 1:46 PM, Rob S. wrote:

> Ah that makes sense. I have your book David, and enjoyed it quite a  
> bit, btw. Apparently I should re-read some parts of it. 🙂 So,  
> basically having a hash as the first parameter just means you always  
> need parens, right?

Yes, _except_ in the case where your last parameter is a hash, in  
which case you need not add the braces since Ruby will gracefully let  
you pass in the hash, almost like named parameters:

def receiver.method\_name(arg1, arg2 = {}); end

receiver.method\_name “duane”, :a =\> 1, :b =\> 2

# arg1 = “duane”

# arg2 = {:a =\> 1, :b =\> 2}

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [June 27, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/why-cant-i-call-this-without-parentheses/96141/5 "2006-06-27T18:18:33Z")

</div>

It might also be worth pointing out that your arguments aren’t  
formatted correctly, i.e.

> legacy fields({:prefix = “post\_”}, :author, :title)

should really by

legacy\_fields({:prefix =\> “post\_”}, :author, :title)

=\>, not =

- james
