# Object singleton problem

**URL:** <https://www.ruby-forum.com/t/object-singleton-problem/143356>\
**Category:** Rails\
**Created:** [July 17, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356 "2008-07-17T17:04:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 17, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/1 "2008-07-17T17:04:31Z")

</div>

Hi All

I’m trying to add an instance variable and a method to a object  
The CODE:

#! /usr/bin/ruby

class Xyz  
def initialize  
p “initializing”  
end  
end

my\_inst = Xyz.new

class \<\<my\_inst  
@new\_var = “Hello world”

def get\_new\_var  
p “instance method”  
@new\_var  
end  
end

p my\_inst.get\_new\_var

# =========

prints:  
instance method  
nil

Any suggestions why the variable is ‘nil’ ??

Thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 17, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/2 "2008-07-17T17:18:23Z")

</div>

@new\_var is a Class attribute only accessible in class methods.

check this out:

> **[Ruby Class Variables, Attributes and Constants](http://ghouston.blogspot.com/2006/06/ruby-class-variables-attributes-and.html)**
>
> Ruby Class Variables, Attributes and Constants (For a related post, see Use Class Instance Variables Not Class Variables ) Writing a little ...

On Jul 17, 11:04 am, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 19, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/3 "2008-07-19T12:10:33Z")

</div>

I thought I opened the object so I could add instance variables to it,  
but I guess this should be done like

my\_inst.instance\_variable\_set(…)

or can this be done like I’m trying ?

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 19, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/4 "2008-07-19T22:19:11Z")

</div>

thnx, but how would I access the @x variable which belongs to the class  
object C ?

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/5 "2008-07-20T13:45:17Z")

</div>

Hi –

On Sat, 19 Jul 2008, Luca S. wrote:

> thnx, but how would I access the @x variable which belongs to the class  
> object C ?

Like any object, C can expose its instance variable through accessor  
methods:

class \<\< C  
attr\_accessor :x  
end

C.x = 1 # etc.

You can also use instance\_eval or instance\_variable\_get, but those are  
“impolite” ways of making an object show you its data. (Useful, but  
impolite 🙂

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 19, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/6 "2008-07-19T14:41:15Z")

</div>

Hi –

On Sat, 19 Jul 2008, Luca S. wrote:

> I thought I opened the object so I could add instance variables to it,  
> but I guess this should be done like
> 
> my\_inst.instance\_variable\_set(…)
> 
> or can this be done like I’m trying ?

There’s a very simple rule: every time you see this:

```
@var

```

you are seeing an instance variable that belongs to ‘self’ – that is,  
whatever ‘self’ happens to be at the time.

Your example looks like this:

class C  
end

c = C.new

class \<\< c  
@x = 1  
end

If you check for self at that point in execution, you’ll find that  
self is the class you’ve just opened up – namely, the singleton class  
of the object c:

class \<\< c  
p self # #\<Class:#\<C:0x7aeb8\>\>  
end

So the instance variable defined at that point belongs to the class.

It’s not specific to singleton classes; it’s about self.

class C  
@x = 1 # belongs to the class object C

```
 def instance_x
   @x # belongs to whatever instance of C is 'self'
 end

```

end

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/7 "2008-07-20T17:13:54Z")

</div>

Hi –

On Sun, 20 Jul 2008, Luca S. wrote:

> my examples uses an object not the class, so if I would do
> 
> obj = Xyz.new
> 
> class \<\< obj  
> attr\_accessor :x  
> end
> 
> how would I assign a value to x and how would I retrieve it ?

obj.x = 1  
puts obj.x

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 20, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/object-singleton-problem/143356/8 "2008-07-20T15:52:12Z")

</div>

my examples uses an object not the class, so if I would do

obj = Xyz.new

class \<\< obj  
attr\_accessor :x  
end

how would I assign a value to x and how would I retrieve it ?

thnx a lot
