# What is the scope of this object

**URL:** <https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585>\
**Category:** Ruby\
**Created:** [July 30, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585 "2006-07-30T16:18:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 30, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/1 "2006-07-30T16:18:25Z")

</div>

Hi all,

irb(main):001:0\>  
irb(main):002:0\* class A  
irb(main):003:1\> def secret;“you found me”;end  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> class BlackHole  
irb(main):006:1\> a=A.new  
irb(main):007:1\> end  
=\> #\<A:0x40224e50\>  
irb(main):008:0\> ObjectSpace.each\_object(){|obj| $found=obj if  
obj.respond\_to? :secret }  
=\> 5252  
irb(main):009:0\> $found  
=\> #\<A:0x40224e50\>  
irb(main):010:0\> $found.secret  
=\> “you found me”  
irb(main):011:0\>

seemingly a is not dead inside BlackHole  
is there a civilized way to get to it? (not through ObjectSpace)

I know Ruby has @@ and @ for variables, everything else are methods  
if defined inside a class like in example above

but where do object like a go if defined inside a class?

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 30, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/2 "2006-07-30T17:08:14Z")

</div>

Robert K. wrote:

> It’s a local variable that goes out of scope when the class body is  
> closed. The fact that you can access it via ObjectSpace does not  
> proove much. The instance is likely not yet garbage collected when  
> you start ObjectSpace.each\_object. Also note, that it’s dangerous to  
> rely on IRB when it comes to local variables and such as IRB behaves  
> slightly different than the Ruby interpreter due to the line by line  
> mode.  
> First I thought the same. But running the following (even without IRB)  
> still finds the object:

class A  
def secret;“you found me”;end  
end

class BlackHole  
A.new  
nil  
end

GC.start  
ObjectSpace.each\_object(){|obj| $found=obj if obj.respond\_to? :secret }  
p $found # =\> #\<A:0xb7b1af74\>  
p $found.secret # =\> “you found me”

v = [RUBY\_PLATFORM, RUBY\_VERSION, RUBY\_RELEASE\_DATE] \* ’ ’  
p v # =\> “i686-linux 1.8.4 2005-12-24”

Is this a bug?

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 30, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/3 "2006-07-30T16:21:59Z")

</div>

Schüle Daniel wrote:

> =\> #\<A:0x40224e50\>  
> is there a civilized way to get to it? (not through ObjectSpace)
> 
> I know Ruby has @@ and @ for variables, everything else are methods  
> if defined inside a class like in example above
> 
> but where do object like a go if defined inside a class?
> 
> Regards, Daniel

It’s a local variable that goes out of scope when the class body is  
closed. The fact that you can access it via ObjectSpace does not proove  
much. The instance is likely not yet garbage collected when you start  
ObjectSpace.each\_object. Also note, that it’s dangerous to rely on IRB  
when it comes to local variables and such as IRB behaves slightly  
different than the Ruby interpreter due to the line by line mode.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 30, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/4 "2006-07-30T17:11:50Z")

</div>

> > > > > “F” == Florian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

F\> Is this a bug?

no, for ruby it’s still on the stack.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 31, 2006, 12:51am UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/5 "2006-07-31T00:51:41Z")

</div>

Hello,

[…]

> It’s a local variable that goes out of scope when the class body is  
> closed. The fact that you can access it via ObjectSpace does not proove  
> much. The instance is likely not yet garbage collected when you start  
> ObjectSpace.each\_object.

ok, I understand. They are used in the same way as any local variable.  
right now I don’t see any possible not trivial purposes it could be  
useful for me. Maybe something like

class Q  
f=File.read(“data”)  
eval f  
end

By the way, what if I create

class Q  
@x=1  
end

to whom does @x belong now?  
Does it make sense? (Usually one wants to create with @@ class variables  
at this scope)

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 30, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/6 "2006-07-30T17:24:04Z")

</div>

ts wrote:

> F\> Is this a bug?
> 
> no, for ruby it’s still on the stack.

Ah, I see. If this happens in a required file, it’s garbage collected  
after the file has been evaluated. One should keep this in mind, after  
creating larger objects in “class … end”.

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [July 31, 2006, 1:15am UTC](https://www.ruby-forum.com/t/what-is-the-scope-of-this-object/100585/7 "2006-07-31T01:15:57Z")

</div>

SchÃ¼le Daniel wrote:

> class Q  
> @x=1  
> end  
> to whom does @x belong now?  
> Q.instance\_variable\_get :@x # =\> 1  
> Does it make sense? (Usually one wants to create with @@ class  
> variables at this scope)  
> @@variables are shared across the whole class hierarchy. Often that is  
> not what is wanted. IMHO they are rather useless.

Your @x from above is a class instance variable, a variable of the class  
object Q.

It’s a good idea to use accessor methods to set/get its value.

class Q  
class \<\< self  
attr\_accessor :x  
end  
self.x = 0  
end

Q.x # =\> 0  
Q.x = 1  
Q.x # =\> 1
