"attr" like functions

Discussion in 'Ruby' started by Ben Giddings, Jun 26, 2004.

  1. Ben Giddings

    Ben Giddings Guest

    I came across an interesting problem today. Say you wanted to have an
    object where all the instance variables have an associated description.
    You also wanted to have a 'pretty_print' method, that would print out
    the description of the variable, along with the value. Here's how the
    ideal code would look:

    class Coordinate < DescribedObject
    attr_with_desc :x, "X Coordinate"
    attr_with_desc :y, "Y Coordinate"
    attr_with_desc :z, "Z Coordinate"
    end

    coord = Coordinate.new
    coord.x = 1
    coord.y = 2
    coord.z = 3

    coord.pretty_print
    X Coordinate: 1
    Y Coordinate: 2
    Z Coordinate: 3


    I managed to get that kind of behaviour with this code:


    class Module
    def attr_with_desc(sym, desc="Instance var")
    attr_accessor sym
    eval_str = "@@desc[\"@#{sym.id2name}\"] = \"#{desc.to_s}\""
    puts(eval_str)
    module_eval(eval_str)
    end
    end

    class DescribedObject
    @@desc = Hash.new

    def pretty_print
    instance_variables.each {
    |var|
    if @@desc.has_key?(var)
    puts "#{@@desc[var]}: #{eval(var)}"
    else
    puts "Instance var #{var}: #{eval(var)}"
    end
    }
    end
    end

    class Coordinate < DescribedObject
    attr_with_desc :x, "X Coord"
    attr_with_desc :y, "Y Coord"
    attr_with_desc :z, "Z Coord"
    end


    But there are still a couple of things that I don't think are ideal
    about this, and wonder if it is possible to fix:

    1) Is there a way to add the 'attr_with_desc' method without having to
    modify the Module class? I'd like to keep it pristine, if possible.

    2) Both Module and DescribedObject have to know that the variable is
    named '@@desc', so if they get out of sync, then that could be bad. Is
    there a way of grouping all the code into one class/module?

    Ben
     
    Ben Giddings, Jun 26, 2004
    #1
    1. Advertising

  2. Ben Giddings

    leon breedt Guest

    On Sat, 26 Jun 2004 09:22:57 +0900, Ben Giddings
    <> wrote:
    > 1) Is there a way to add the 'attr_with_desc' method without having to
    > modify the Module class? I'd like to keep it pristine, if possible.

    not sure why you're putting a class method in Module, putting it in
    the class itself would suffice?

    --<snip>--

    class Describable
    def Describable.attr_with_desc(id, description)
    attr_accessor id
    @@desc ||= {}
    @@desc["@#{id.id2name}"] = description.to_s
    end
    def pretty_print
    instance_variables.each { |var|
    if @@desc.has_key?(var)
    puts "#{@@desc[var]}: #{eval(var)}"
    else
    puts "Instance var #{var}: #{eval(var)}"
    end
    }
    end
    end

    class Test<Describable
    attr_with_desc :name, "Name of the test"
    attr_with_desc :version, "Version of the test"
    end

    t = Test.new
    t.name = "test"
    t.version = 1.0
    t.pretty_print

    --<snip>--

    regards,
    leon
     
    leon breedt, Jun 26, 2004
    #2
    1. Advertising

  3. Perhaps making Describable a module, rather than class, would be more
    appropriate here. If nothing else, the 'attr_with_desc' method should
    be available in objects that subclass a different type than
    Describable.

    Lennon
     
    Lennon Day-Reynolds, Jun 28, 2004
    #3
    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. NULL
    Replies:
    0
    Views:
    370
  2. T. Onoma

    Attr; and Moving Namespaces

    T. Onoma, Aug 23, 2004, in forum: Ruby
    Replies:
    5
    Views:
    99
    Pit Capitain
    Aug 25, 2004
  3. Sam Roberts
    Replies:
    3
    Views:
    159
    Sam Roberts
    Nov 14, 2004
  4. Gioele Barabucci

    class methods like attr:

    Gioele Barabucci, Jan 21, 2006, in forum: Ruby
    Replies:
    3
    Views:
    104
    Wilson Bilkovich
    Jan 23, 2006
  5. Chad Murphy

    Defining attr like methods

    Chad Murphy, Apr 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    151
    Daniel Finnie
    Apr 21, 2008
Loading...

Share This Page