attr_reader, default attribute value, and rdoc of attribute

Discussion in 'Ruby' started by dkmd_nielsen, Nov 26, 2008.

  1. dkmd_nielsen

    dkmd_nielsen Guest

    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

    Thanks for your suggestions.
    dvn
    dkmd_nielsen, Nov 26, 2008
    #1
    1. Advertising

  2. I don't know about rdoc, but perhaps the following might help.
    See first class Test and usage, to find out if this is
    what you need

    #!/usr/bin/env ruby

    #-----------------------------------------------
    #-----------------------------------------------
    module MyAccessor
    def my_attribute (nou, defval=nil)
    if defval == nil
    code ="def #{nou} (s=nil)
    @#{nou} = s || @#{nou}
    end"
    elsif defval.class==String
    code ="def #{nou} (s=nil)
    @#{nou} = s || @#{nou} || '#{defval}'
    end"
    else
    code ="def #{nou} (s=nil)
    @#{nou} = s || @#{nou} || #{defval}
    end"
    end
    puts "defining ", code
    class_eval code
    end # def my_attr
    end # module


    #----------------------------------------
    class Test

    extend MyAccessor

    my_attribute:)color, "red")
    my_attribute:)width, 20)
    my_attribute:)text, "no name")


    def initialize (&block)
    instance_eval(&block)
    end
    end

    #----------------------------------
    # usage

    puts " -------------- "
    t = Test.new {
    color "blue"
    }
    puts " -------------- "

    puts t.color # => blue
    puts t.width # => 20
    puts t.text # => no name

    t.width 50

    puts t.width # => 50
    cibercitizen1, Nov 26, 2008
    #2
    1. Advertising

  3. Re: attr_reader, default attribute value, and rdoc of attrib

    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

    attr_accessor :fld_delimiter
    def fld_delimiter #:nodoc
    @fld_delimiter = ',' unless defined? @fld_delimiter
    @fld_delimiter
    end

    Another option is never to assign when defaults are being used: using a
    constant lets you document the default.

    DEFAULT_FLD_DELIMITER = ','.freeze
    attr_accessor :fld_delimiter
    def fld_delimiter
    defined? @fld_delimiter ? @fld_delimiter : DEFAULT_FLD_DELIMITER
    end

    However I think it would be simpler just to set @fld_delimiter=',' in
    your object's initialize method.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Nov 26, 2008
    #3
  4. dkmd_nielsen

    dkmd_nielsen Guest

    Re: attr_reader, default attribute value, and rdoc of attrib

    On Nov 26, 6:17 am, Brian Candler <> 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
    >
    > attr_accessor :fld_delimiter
    > def fld_delimiter #:nodoc
    >   @fld_delimiter = ',' unless defined? @fld_delimiter
    >   @fld_delimiter
    > end
    >
    > Another option is never to assign when defaults are being used: using a
    > constant lets you document the default.
    >
    > DEFAULT_FLD_DELIMITER = ','.freeze
    > attr_accessor :fld_delimiter
    > def fld_delimiter
    >   defined? @fld_delimiter ? @fld_delimiter : DEFAULT_FLD_DELIMITER
    > end
    >
    > However I think it would be simpler just to set @fld_delimiter=',' in
    > your object's initialize method.
    > --
    > Posted viahttp://www.ruby-forum.com/.


    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.
    dkmd_nielsen, Nov 26, 2008
    #4
  5. dkmd_nielsen

    Trans Guest

    Re: attr_reader, default attribute value, and rdoc of attrib

    On Nov 26, 7:17=A0am, Brian Candler <> 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. =A0So, would the proper syntax for accomplishing this
    > > elementary feat be:

    >
    > > =A0 =A0# Field (row) delimiter. =A0Default is a comma.
    > > =A0 =A0attr_reader fld_delimiter

    >
    > > =A0 =A0# :call-seq:
    > > =A0 =A0 # =A0 fld_delimiter
    > > =A0 =A0 #
    > > =A0 =A0 # Returns the field (column) delimiter. =A0Default is comma.
    > > =A0 =A0 def fld_delimiter #:nodoc:
    > > =A0 =A0 =A0 !@fld_delimiter.nil? ? @fld_delimiter : (@fld_delimiter =3D=

    ',')
    > > =A0 =A0 end

    >
    > > =A0 =A0 # :call-seq:
    > > =A0 =A0 # =A0 fld_delimiter(String)
    > > =A0 =A0 #
    > > =A0 =A0 # Assign a field (column) delimiter.
    > > =A0 =A0 def fld_delimiter=3D(d)
    > > =A0 =A0 =A0 @fld_delimiter =3D d
    > > =A0 =A0 end

    >
    > I would tidy this up as:
    >
    > attr_accessor :fld_delimiter
    > def fld_delimiter #:nodoc:
    > =A0 @fld_delimiter ||=3D ','
    > end
    >
    > That assumes you never want to assign nil or false as a fld_delimiter.
    > If you do, then
    >
    > attr_accessor :fld_delimiter
    > def fld_delimiter #:nodoc
    > =A0 @fld_delimiter =3D ',' unless defined? @fld_delimiter
    > =A0 @fld_delimiter
    > end
    >
    > Another option is never to assign when defaults are being used: using a
    > constant lets you document the default.
    >
    > DEFAULT_FLD_DELIMITER =3D ','.freeze
    > attr_accessor :fld_delimiter
    > def fld_delimiter
    > =A0 defined? @fld_delimiter ? @fld_delimiter : DEFAULT_FLD_DELIMITER
    > end
    >
    > However I think it would be simpler just to set @fld_delimiter=3D',' in
    > your object's initialize method.


    Brain is right on. The archetypal Ruby best-practice will look like
    this:

    DEFAULT_FLD_DELIMITER =3D ','.freeze

    attr_accessor :fld_delimiter

    def initialize
    @fld_delimiter =3D DEFAULT_FLD_DELIMITER
    end
    Trans, Nov 26, 2008
    #5
  6. Re: attr_reader, default attribute value, and rdoc of attrib

    > 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


    Another common pattern:

    class Foo
    DEFAULT_OPTS = {
    :fld_delimiter => ',',
    }
    def initialize(opts = {})
    opts = DEFAULT_OPTS.merge(opts)
    @fld_delimiter = opts[:fld_delimiter]
    end
    end

    f = Foo.new
    g = Foo.new:)fld_delimiter => ' ')
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Nov 26, 2008
    #6
  7. 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:


    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
    Joel VanderWerf, Nov 26, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivek
    Replies:
    3
    Views:
    105
    Patrick Hurley
    Dec 22, 2005
  2. Joe Ruby MUDCRAP-CE

    attr_reader + attr_writer?

    Joe Ruby MUDCRAP-CE, Oct 13, 2006, in forum: Ruby
    Replies:
    6
    Views:
    94
    Trans
    Oct 13, 2006
  3. Daniel Finnie

    attr_reader, etc for class variables

    Daniel Finnie, Dec 10, 2006, in forum: Ruby
    Replies:
    3
    Views:
    81
  4. libsfan01

    attr_reader explained

    libsfan01, Mar 3, 2007, in forum: Ruby
    Replies:
    5
    Views:
    92
    Eric Hodel
    Mar 5, 2007
  5. Mike Onofrietto

    attr_reader vs Objects and Attributes

    Mike Onofrietto, Feb 10, 2011, in forum: Ruby
    Replies:
    7
    Views:
    127
    Mike Onofrietto
    Feb 15, 2011
Loading...

Share This Page