What I want to do is create an attribute, allow it to have a default
value, and have the attribute documented as such (and not as a method)
with rdoc. So, would the proper syntax for accomplishing this
elementary feat be:
# Field (row) delimiter. Default is a comma.
attr_reader fld_delimiter
# :call-seq:
# fld_delimiter
···
#
# Returns the field (column) delimiter. Default is comma.
def fld_delimiter #:nodoc:
!@fld_delimiter.nil? ? @fld_delimiter : (@fld_delimiter = ',')
end
# :call-seq:
# fld_delimiter(String)
#
# Assign a field (column) delimiter.
def fld_delimiter=(d) @fld_delimiter = d
end
What I want to do is create an attribute, allow it to have a default
value, and have the attribute documented as such (and not as a method)
with rdoc. So, would the proper syntax for accomplishing this
elementary feat be:
# Field (row) delimiter. Default is a comma.
attr_reader fld_delimiter
# :call-seq:
# fld_delimiter
#
# Returns the field (column) delimiter. Default is comma.
def fld_delimiter #:nodoc:
!@fld_delimiter.nil? ? @fld_delimiter : (@fld_delimiter = ',')
end
# :call-seq:
# fld_delimiter(String)
#
# Assign a field (column) delimiter.
def fld_delimiter=(d) @fld_delimiter = d
end
I would tidy this up as:
attr_accessor :fld_delimiter
def fld_delimiter #:nodoc: @fld_delimiter ||= ','
end
That assumes you never want to assign nil or false as a fld_delimiter.
If you do, then
What I want to do is create an attribute, allow it to have a default
value, and have the attribute documented as such (and not as a method)
with rdoc. So, would the proper syntax for accomplishing this
elementary feat be:
Something else to consider, along with the other suggestions, is the -A switch in rdoc:
--accessor, -A accessorname[,..]
comma separated list of additional class methods
that should be treated like 'attr_reader' and
friends. Option may be repeated. Each accessorname
may have '=text' appended, in which case that text
appears where the r/w/rw appears for normal accessors.
So you could define a class method just to let rdoc know that your method should be documented as an attribute. The actual implementation of the method can be anything.
You class method could also arrange the default value....
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Thanks guys. This is exactly the kind of information I'm looking
for...and need to get better at Ruby development. This is good
stuff. Thanks again.
···
On Nov 26, 6:17 am, Brian Candler <b.cand...@pobox.com> wrote:
dkmd_nielsen wrote:
> What I want to do is create an attribute, allow it to have a default
> value, and have the attribute documented as such (and not as a method)
> with rdoc. So, would the proper syntax for accomplishing this
> elementary feat be:
> # Field (row) delimiter. Default is a comma.
> attr_reader fld_delimiter
> # :call-seq:
> # fld_delimiter
> #
> # Returns the field (column) delimiter. Default is comma.
> def fld_delimiter #:nodoc:
> !@fld_delimiter.nil? ? @fld_delimiter : (@fld_delimiter = ',')
> end
> # :call-seq:
> # fld_delimiter(String)
> #
> # Assign a field (column) delimiter.
> def fld_delimiter=(d)
> @fld_delimiter = d
> end
I would tidy this up as:
attr_accessor :fld_delimiter
def fld_delimiter #:nodoc: @fld_delimiter ||= ','
end
That assumes you never want to assign nil or false as a fld_delimiter.
If you do, then
Brain is right on. The archetypal Ruby best-practice will look like
this:
DEFAULT_FLD_DELIMITER = ','.freeze
attr_accessor :fld_delimiter
def initialize @fld_delimiter = DEFAULT_FLD_DELIMITER
end
···
On Nov 26, 7:17 am, Brian Candler <b.cand...@pobox.com> wrote:
dkmd_nielsen wrote:
> What I want to do is create an attribute, allow it to have a default
> value, and have the attribute documented as such (and not as a method)
> with rdoc. So, would the proper syntax for accomplishing this
> elementary feat be:
> # Field (row) delimiter. Default is a comma.
> attr_reader fld_delimiter
> # :call-seq:
> # fld_delimiter
> #
> # Returns the field (column) delimiter. Default is comma.
> def fld_delimiter #:nodoc:
> !@fld_delimiter.nil? ? @fld_delimiter : (@fld_delimiter = ',')
> end
> # :call-seq:
> # fld_delimiter(String)
> #
> # Assign a field (column) delimiter.
> def fld_delimiter=(d)
> @fld_delimiter = d
> end
I would tidy this up as:
attr_accessor :fld_delimiter
def fld_delimiter #:nodoc: @fld_delimiter ||= ','
end
That assumes you never want to assign nil or false as a fld_delimiter.
If you do, then