#attr back to 1.8.6 an older behavior?

I

Intransition

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?
 
B

Brian Candler

Thomas said:
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:in `attr'
from (irb):2:in `<class:Foo>'
from (irb):1
from /usr/local/bin/irb191:12:in `<main>'
irb(main):004:0> RUBY_DESCRIPTION
=> "ruby 1.9.1p429 (2010-07-02 revision 28523) [x86_64-linux]"
 
I

Intransition

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

I don't really care about the 'x=3D' 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 =3D> 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top