#attr back to 1.8.6 an older behavior?

What happened to dropping the optional true/false parameter from #attr
in 1.9? About a year or so ago, the method was changed to support
multiple symbols, and if you wanted a writer you just add one with an
‘=’ on the end, e.g.

attr :x, :x=

That was really nice b/c it is explicit. In general “true” parameters
are not very readable. It also made it a touch easier to override
#attr to add options like default values (attr :x, :default=>10).

But I was just playing around with the latest 1.9.2 release and #attr
is back to behaving just like 1.8.6 and older. Why the move back?

Thomas S. wrote:

What happened to dropping the optional true/false parameter from #attr
in 1.9? About a year or so ago, the method was changed to support
multiple symbols, and if you wanted a writer you just add one with an
‘=’ on the end, e.g.

attr :x, :x=

FWIW, it looks like 1.9.1 doesn’t allow attr :x= anyway (reader
attributes only)

irb(main):001:0> class Foo
irb(main):002:1> attr :x, :x=
irb(main):003:1> end
NameError: invalid attribute name x=' from (irb):2:inattr’
from (irb):2:in <class:Foo>' from (irb):1 from /usr/local/bin/irb191:12:in
irb(main):004:0> RUBY_DESCRIPTION
=> “ruby 1.9.1p429 (2010-07-02 revision 28523) [x86_64-linux]”

On Aug 26, 10:39 am, Brian C. [email protected] wrote:

Thomas S. wrote:

What happened to dropping the optional true/false parameter from #attr
in 1.9? About a year or so ago, the method was changed to support
multiple symbols, and if you wanted a writer you just add one with an
‘=’ on the end, e.g.

attr :x, :x=

FWIW, it looks like 1.9.1 doesn’t allow attr :x= anyway (reader
attributes only)

I don’t really care about the ‘x=’ option either way, though I do
think it is a nice convenience. I do think dropping the optional true/
false option is a good thing though for three reasons. 1) true/false
parameters are very unreadable. They give you no information
whatsoever as to what behavior is being toggled, 2) multiple symbols
can be given, and 3) it can be useful to override #attr to take
options, eg.

attr :x, :default => 10

I have found this useful in a number of cases, and the idiom is very
common with variants of attr too. Having to take the true/false
parameter into account, while not a huge deal, does leave open an
argument ambiguity, since a Hash evaluates as true.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs