Submitted by transami (Wed Apr 12 15:42:57 UTC 2006)
# create reader attr :foo # create reader and writer attr :foo=
First, using a "boolean" parameter like this is not very programmer-friendly b/c it gives no indication of what the parameter does; so it's not nice Literate Programming. This topic has been discussed before on Ruby-talk.
Second and more importantly, the parameter makes it inconvenient to expand on attr's functionality without causing potential compatability issues with other's prior code.
In our specific case in Nitro, we would like to extend #attr to be able to take annotations. Right now we can do this for example:
attr :x ann :x, :default => 10
But we'd like to do:
attr :x, :default => 10
The boolean parameter on #attr is the only thing preventing a _clean_ extension.
I've looked at Ruby source and there are only a few places that use the 'true' parameter.
ext/tk/sample/tcltklib/sample2.rb: attr :com_disk, TRUE ext/tk/sample/tcltklib/sample2.rb: attr :oval, TRUE lib/shell.rb: attr :cascade, true lib/shell.rb: attr :debug, true lib/shell.rb: attr :verbose, true lib/shell.rb: attr :umask, true lib/shell.rb: attr :record_separator, true lib/shell.rb: attr :verbose, true lib/shell.rb: attr :debug, true lib/sync.rb: attr :sync_mode, true lib/sync.rb: attr :sync_waiting, true lib/sync.rb: attr :sync_upgrade_waiting, true lib/sync.rb: attr :sync_sh_locker, true lib/sync.rb: attr :sync_ex_locker, true lib/sync.rb: attr :sync_ex_count, true lib/tracer.rb: attr :verbose, true lib/tracer.rb: attr :stdout, true lib/irb/ruby-token.rb: attr :name, true lib/rdoc/parsers/parse_rb.rb: attr :skip_space, true lib/rdoc/parsers/parse_rb.rb: attr :read_auto_clean_up, true lib/rdoc/parsers/parse_rb.rb: attr :exception_on_syntax_error, true sample/drb/dchats.rb: attr :name, true
Understandably there is the issue of backwards compatability. But thankfully the feature isn't used extensively; usually attr_accessor is used instead, and updating ones code to take it into account is very simple and straightfoward.
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
I would actually think it would make sense for attr to do what attr_accessor does currently. _reader and _writer could then be the specializations.
David Black
I agree with David. It doesn't make sense that
attr_writer
,attr_reader
, andattr_accessor
accept multiple attribute names, andattr
only accepts one. I think the most common case isattr_accessor
, soattr
should match that: