# Dynamic methods / accessors

**URL:** <https://rubytalk.org/t/dynamic-methods-accessors/20451>\
**Category:** ruby-talk\
**Created:** [28 August 2005 15:09 UTC](https://rubytalk.org/t/dynamic-methods-accessors/20451 "2005-08-28T15:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frodo\_Larik](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@Frodo\_Larik](https://rubytalk.org/u/Frodo_Larik)\
**Post date:** [28 August 2005 15:09 UTC](https://rubytalk.org/t/dynamic-methods-accessors/20451/1 "2005-08-28T15:09:14Z")

</div>

Hi all,

I'm trying to setup some kind of dynamic method / accessor assignment to objects. But I don't want the other objects from the same class to know about "personal" methods. I'm just learning Ruby, so it could be I'm missing some crucial knowledge.

For example:

class MyThing  
&nbsp;&nbsp;&nbsp;&nbsp;def create\_method(method)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I found two things on internet which can acchieve this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.class\_eval { attr\_accessor method }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# AND  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.send(:define\_method, method, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but I'm not sure which is the best one to use, but I think  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I prefer the attr\_accessor, because that's the functionality  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I need  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

# Create first object  
a = MyThing.new  
a.create\_method('box') # Adds a method box to a  
a.box = 'something inside'

# The nicest thing would be if I could do something like this:  
a.create\_method('box','some\_value'), so a.box is populated with 'some value'

# Create second object  
b = MyThing.new

# I don't want the following to happen  
b.box = 'something other'

I don't want the method 'box' be available to b, only to a

How can I achieve this?

I saw it was possible to achieve this with something like this:

class YourThing \< MyThing  
&nbsp;&nbsp;&nbsp;&nbsp;# A placeholder class for the dynamic methods  
end

but I don't know the name of the class on forehand, so if something like this is possible:

class #{i\_want\_this\_class} \< MyThing  
&nbsp;&nbsp;&nbsp;&nbsp;# A placeholder class for the dynamic methods  
end

That would be nice.

Sincerely,

Frodo Larik

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 August 2005 15:16 UTC](https://rubytalk.org/t/dynamic-methods-accessors/20451/2 "2005-08-28T15:16:22Z")

</div>

You need so called singleton methods. These are present in just a single instance. You define them via the singleton class. (Disclaimer: there has been some debate about the naming of these because "singleton" can be misleading here, but AFAIK this is still the officiel term.) You access the singleton class (a class that is associated with a single instance only) like this

class \<\<some\_instance  
end

So you can do:

class MyThing  
end  
a = MyThing.new  
class \<\<a  
&nbsp;&nbsp;attr\_accessor :box  
end  
a.box = 'something inside'

If you want to have it a bit more convenient you can pack this into a method like this:

class MyThing  
&nbsp;&nbsp;def add\_attribute(name, val=nil)  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\<self;self;end.send( :attr\_accessor, name )  
&nbsp;&nbsp;&nbsp;&nbsp;self.send("#{name}=", val)  
&nbsp;&nbsp;end  
end  
a = MyThing.new  
a.add\_attribute :foo, "hello"  
a.foo # returns "hello"

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Frodo Larik \<lists@elasto.nl\> wrote:
> 
> > Hi all,
> > 
> > I'm trying to setup some kind of dynamic method / accessor assignment  
> > to objects. But I don't want the other objects from the same class to  
> > know about "personal" methods. I'm just learning Ruby, so it could be  
> > I'm missing some crucial knowledge.
> > 
> > For example:
> > 
> > class MyThing  
> > &nbsp;&nbsp;&nbsp;def create\_method(method)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I found two things on internet which can acchieve this  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.class\_eval { attr\_accessor method }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# AND  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.send(:define\_method, method, &block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but I'm not sure which is the best one to use, but I think  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I prefer the attr\_accessor, because that's the functionality  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I need  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > # Create first object  
> > a = MyThing.new  
> > a.create\_method('box') # Adds a method box to a  
> > a.box = 'something inside'
> > 
> > # The nicest thing would be if I could do something like this:  
> > a.create\_method('box','some\_value'), so a.box is populated with 'some  
> > value'
> > 
> > # Create second object  
> > b = MyThing.new
> > 
> > # I don't want the following to happen  
> > b.box = 'something other'
> > 
> > I don't want the method 'box' be available to b, only to a
> > 
> > How can I achieve this?

---

<div class="post-metadata">

**Author:** ![Frodo\_Larik](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@Frodo\_Larik](https://rubytalk.org/u/Frodo_Larik)\
**Post date:** [28 August 2005 15:36 UTC](https://rubytalk.org/t/dynamic-methods-accessors/20451/3 "2005-08-28T15:36:21Z")

</div>

Thanks guys!

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 August 2005 15:21 UTC](https://rubytalk.org/t/dynamic-methods-accessors/20451/4 "2005-08-28T15:21:22Z")

</div>

PS: There is also OpenStruct which adds methods on the fly. So you can directly do

o = OpenStruct.new  
o.bar = 10

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > Frodo Larik \<lists@elasto.nl\> wrote:
> > 
> > > Hi all,
> > > 
> > > I'm trying to setup some kind of dynamic method / accessor assignment  
> > > to objects. But I don't want the other objects from the same class to  
> > > know about "personal" methods. I'm just learning Ruby, so it could be  
> > > I'm missing some crucial knowledge.
> > > 
> > > For example:
> > > 
> > > class MyThing  
> > > &nbsp;&nbsp;&nbsp;def create\_method(method)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I found two things on internet which can acchieve this  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.class\_eval { attr\_accessor method }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# AND  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.class.send(:define\_method, method, &block)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but I'm not sure which is the best one to use, but I think  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I prefer the attr\_accessor, because that's the functionality  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I need  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > > 
> > > # Create first object  
> > > a = MyThing.new  
> > > a.create\_method('box') # Adds a method box to a  
> > > a.box = 'something inside'
> > > 
> > > # The nicest thing would be if I could do something like this:  
> > > a.create\_method('box','some\_value'), so a.box is populated with 'some  
> > > value'
> > > 
> > > # Create second object  
> > > b = MyThing.new
> > > 
> > > # I don't want the following to happen  
> > > b.box = 'something other'
> > > 
> > > I don't want the method 'box' be available to b, only to a
> > > 
> > > How can I achieve this?
> > 
> > You need so called singleton methods. These are present in just a  
> > single instance. You define them via the singleton class.  
> > (Disclaimer: there has been some debate about the naming of these  
> > because "singleton" can be misleading here, but AFAIK this is still  
> > the officiel term.) You access the singleton class (a class that is  
> > associated with a single instance only) like this
> > 
> > class \<\<some\_instance  
> > end
> > 
> > So you can do:
> > 
> > class MyThing  
> > end  
> > a = MyThing.new  
> > class \<\<a  
> > attr\_accessor :box  
> > end  
> > a.box = 'something inside'
> > 
> > If you want to have it a bit more convenient you can pack this into a  
> > method like this:
> > 
> > class MyThing  
> > def add\_attribute(name, val=nil)  
> > &nbsp;&nbsp;&nbsp;class \<\<self;self;end.send( :attr\_accessor, name )  
> > &nbsp;&nbsp;&nbsp;self.send("#{name}=", val)  
> > end  
> > end  
> > a = MyThing.new  
> > a.add\_attribute :foo, "hello"  
> > a.foo # returns "hello"
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;robert
