Question

I'm a bit miffed why this:

cattr_accessor :aggregate { true }

fails with the error:

syntax error, unexpected '{', expecting keyword_end
  cattr_accessor :aggregate { true }
                            ^

while exactly the same thing, with do/end seems to be doing the right thing:

cattr_accessor :aggregate do true end

(but is whhhay too verbose ;)

The source from :http://api.rubyonrails.org/classes/Class.html#method-i-cattr_writer is a bit too crufty given the time I have to spend on this. Thoughts?

Was it helpful?

Solution

Isn't this just following the language binding precedence?

  • Brace form has higher precedence and will bind to the last parameter if invocation made w/o parens.
  • do/end form has lower precedence and will bind to the invocation even without parens.

If you don't want to write the do end form, you will need to put parenthesis around the call.

cattr_accessor(:is_admin) { true }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top