is there a way to add attributes to instances?

Discussion in 'Ruby' started by ako..., Oct 7, 2005.

  1. ako...

    ako... Guest

    hello,

    is there a way to add properties to instances and access them later
    just like if they were defined in the object's class?


    a.newproperty = 123
    puts a.newproperty

    i know how to dynamically add newproperty to the class of instance 'a',
    but is there a way to do it per instance? or do i have to maintain an
    explicit hash table on my objects and access these attributes through
    the table?

    thanks for the help
    konstantin
     
    ako..., Oct 7, 2005
    #1
    1. Advertising

  2. On Oct 6, 2005, at 9:36 PM, ako... wrote:

    > hello,
    >
    > is there a way to add properties to instances and access them later
    > just like if they were defined in the object's class?
    >
    >
    > a.newproperty = 123
    > puts a.newproperty
    >
    > i know how to dynamically add newproperty to the class of instance
    > 'a',
    > but is there a way to do it per instance? or do i have to maintain an
    > explicit hash table on my objects and access these attributes through
    > the table?


    Here's two different ideas to get you going:

    irb(main):001:0> require "ostruct"
    => true
    irb(main):002:0> a = OpenStruct.new
    => <OpenStruct>
    irb(main):003:0> a.new_property = 123
    => 123
    irb(main):004:0> a.property
    => nil

    irb(main):018:0> class Magic
    irb(main):019:1> def method_missing( meth, *args, &block )
    irb(main):020:2> if meth.to_s =~ /^(\w+)=$/
    irb(main):021:3> instance_variable_set("@#{$1}", *args)
    irb(main):022:3> elsif instance_variables.include? "@#{meth}"
    irb(main):023:3> instance_variable_get("@#{meth}")
    irb(main):024:3> else
    irb(main):025:3* super
    irb(main):026:3> end
    irb(main):027:2> end
    irb(main):028:1> end
    => nil
    irb(main):029:0> b = Magic.new
    => #<Magic:0x1b0764>
    irb(main):030:0> b.new_property = 123
    => 123
    irb(main):031:0> b.new_property
    => 123

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Oct 7, 2005
    #2
    1. Advertising

  3. ako...

    James Britt Guest

    ako... wrote:
    > hello,
    >
    > is there a way to add properties to instances and access them later
    > just like if they were defined in the object's class?
    >
    >
    > a.newproperty = 123
    > puts a.newproperty
    >
    > i know how to dynamically add newproperty to the class of instance 'a',
    > but is there a way to do it per instance? or do i have to maintain an
    > explicit hash table on my objects and access these attributes through
    > the table?


    class <<a
    attr_accessor :newproperty
    end


    James

    --

    http://www.ruby-doc.org - The Ruby Documentation Site
    http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
    http://www.rubystuff.com - The Ruby Store for Ruby Stuff
    http://www.jamesbritt.com - Playing with Better Toys
     
    James Britt, Oct 7, 2005
    #3
  4. ako...

    Rob Rypka Guest

    On 10/6/05, James Edward Gray II <> wrote:
    > On Oct 6, 2005, at 9:36 PM, ako... wrote:
    > > is there a way to add properties to instances and access them later
    > > just like if they were defined in the object's class?

    >
    > Here's two different ideas to get you going:
    >
    > irb(main):001:0> require "ostruct"
    > =3D> true
    > irb(main):002:0> a =3D OpenStruct.new
    > =3D> <OpenStruct>
    > irb(main):003:0> a.new_property =3D 123
    > =3D> 123
    > irb(main):004:0> a.property
    > =3D> nil


    I think OpenStruct is what you want to use. If you feel you have to
    implement it yourself, or it's not quite right, think about using
    method_missing. It is perhaps the most awsomely useful concepts in
    Ruby.

    --
    Rob
     
    Rob Rypka, Oct 7, 2005
    #4
    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. John Wohlbier
    Replies:
    2
    Views:
    398
    Josiah Carlson
    Feb 22, 2004
  2. Jonathon McKitrick

    Best way to add attributes to shelved objects?

    Jonathon McKitrick, Apr 23, 2004, in forum: Python
    Replies:
    0
    Views:
    271
    Jonathon McKitrick
    Apr 23, 2004
  3. P4trykx
    Replies:
    2
    Views:
    1,891
    bruce barker
    Jan 31, 2007
  4. Replies:
    8
    Views:
    488
    James Stroud
    Jan 29, 2009
  5. Andrew Berg
    Replies:
    3
    Views:
    114
    Steven D'Aprano
    Oct 2, 2011
Loading...

Share This Page