adding object attributes

A

Artur Merke

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
 
D

David A. Black

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
 
A

Artur Merke

Thanx David, that's the solution, and even fits into one line
for several attributes ;-)

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

Artur
 
R

Robert Klemme

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])
=> said:
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
=> said:
a=A.new [:a]
=> said:
a.my_method
buh!
=> nil=> <A a=nil dummy="hello">

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

Kind regards

robert
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top