Adding object attributes

Hi

class A
  attr_accessor :a
end

a= A.new
b= A.new

#this adds a new attribute to the object 'b'
def b.dummy
  @dummy
end
def b.dummy=(val)
  @dummy= val
end

b.dummy= "hello world"
print "\n", b.dummy

Is there a shorthand notation for adding attributes to an object?

For example one could think of the equivalent of adding an attribute to a
class, like

b.attr_accessor(:dummy) #illegal in ruby

Of course I could define a sort of public 'attr_accessor' for the class
'A' by myself, but I do NOT want to change/extend the class, only some objects.

any suggestions?

Artur

Hi --

Hi

class A
attr_accessor :a
end

a= A.new
b= A.new

#this adds a new attribute to the object 'b'
def b.dummy
@dummy
end
def b.dummy=(val)
@dummy= val
end

b.dummy= "hello world"
print "\n", b.dummy

Is there a shorthand notation for adding attributes to an object?

For example one could think of the equivalent of adding an attribute to a
class, like

b.attr_accessor(:dummy) #illegal in ruby

Of course I could define a sort of public 'attr_accessor' for the class
'A' by myself, but I do NOT want to change/extend the class, only some objects.

any suggestions?

What you want to do is use attr_accessor -- but, as you say, not in
'A', but rather in the singleton class of 'b'. You would do this like
this:

    class << b
      attr_accessor :dummy
    end

(A singleton class is anonymous, so you have to use the "<< object"
notation to "fish" for it from the object whose singleton class it
is.)

In fact, when you do:

   def b.dummy ...

you are inserting a method into exactly the same class. Opening up
the class, however, makes it possible to use class methods, like
attr_accessor.

David

···

On Fri, 10 Dec 2004, Artur Merke wrote:

--
David A. Black
dblack@wobblini.net

[...]

Is there a shorthand notation for adding attributes to an object?

what about

class << a
      attr_accessor :b
end

?

Patrick

Thanx David, that's the solution, and even fits into one line
for several attributes :wink:

class << b; attr_accessor :dummy, :dummy2 end

Artur

···

On Fri, 10 Dec 2004, David A. Black wrote:

Hi --

On Fri, 10 Dec 2004, Artur Merke wrote:

> Hi
>
> class A
> attr_accessor :a
> end
>
> a= A.new
> b= A.new
>
> #this adds a new attribute to the object 'b'
> def b.dummy
> @dummy
> end
> def b.dummy=(val)
> @dummy= val
> end
>
> b.dummy= "hello world"
> print "\n", b.dummy
>
> Is there a shorthand notation for adding attributes to an object?
>
> For example one could think of the equivalent of adding an attribute to a
> class, like
>
> b.attr_accessor(:dummy) #illegal in ruby
>
> Of course I could define a sort of public 'attr_accessor' for the class
> 'A' by myself, but I do NOT want to change/extend the class, only some objects.
>
> any suggestions?

What you want to do is use attr_accessor -- but, as you say, not in
'A', but rather in the singleton class of 'b'. You would do this like
this:

    class << b
      attr_accessor :dummy
    end

(A singleton class is anonymous, so you have to use the "<< object"
notation to "fish" for it from the object whose singleton class it
is.)

In fact, when you do:

   def b.dummy ...

you are inserting a method into exactly the same class. Opening up
the class, however, makes it possible to use class methods, like
attr_accessor.

David

--
David A. Black
dblack@wobblini.net

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0412100332320.5050@wobblini...

What you want to do is use attr_accessor -- but, as you say, not in
'A', but rather in the singleton class of 'b'. You would do this like
this:

Alternatively Artur can use OpenStruct, where you can add attributes even
more dynamic:

:25:55 [source]: irbs -r ostruct

a = OpenStruct.new([:a])

=> <OpenStruct a=nil>

a.a

=> nil

a.dummy = "hello world"

=> "hello world"

a.dummy

=> "hello world"

You can even inherity OpenStruct, if you want to add additional methods:

class A < OpenStruct
def my_method() puts "buh!" end
end

=> nil

a=A.new

=> <A>

a=A.new [:a]

=> <A a=nil>

a.my_method

buh!
=> nil

a.dummy = "hello"

=> "hello"

a.dummy

=> "hello"

a

=> <A a=nil dummy="hello">

Or you use a hash - that might be appropriate in some circumstances, too.

Kind regards

    robert