# How to access variables from within class definition

**URL:** <https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891>\
**Category:** Ruby\
**Created:** [May 27, 2009, 5:08am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891 "2009-05-27T05:08:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 27, 2009, 5:08am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/1 "2009-05-27T05:08:24Z")

</div>

I’ve got something like:

@var = ‘brown’

class Foo \< ActiveRecord::Base  
set\_table\_name @var  
end

However, as the code is right now, I’m not able to access @var from  
within the class definition. Can someone tell me how I can accomplish  
this?

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 27, 2009, 5:24am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/2 "2009-05-27T05:24:11Z")

</div>

Maybe you want to use global instead instance variable here:

- eggman2001 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-05-27 12:07:18 +0900]:

> I’ve got something like:
> 
> @var = ‘brown’  
> $var = ‘brown’

> class Foo \< ActiveRecord::Base  
> set\_table\_name @var  
> set\_table\_name $var

-Jan

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 27, 2009, 6:05am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/3 "2009-05-27T06:05:35Z")

</div>

eggman2001 wrote:

> I’ve got something like:
> 
> @var = ‘brown’
> 
> class Foo \< ActiveRecord::Base  
> set\_table\_name @var  
> end
> 
> However, as the code is right now, I’m not able to access @var from  
> within the class definition. Can someone tell me how I can accomplish  
> this?

Of course you can’t see @var from within the class definition if you set  
it outside. Remember that @variables are instance variables, and are  
scoped to the class definition.

If you can explain where you’re trying to set @var, perhaps we can come  
up with an alternative approach.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 27, 2009, 10:42am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/4 "2009-05-27T10:42:02Z")

</div>

eggman2001 wrote:

> I’ve got something like:
> 
> @var = ‘brown’

This is an instance variable of the top-level (main) object.

> class Foo \< ActiveRecord::Base  
> set\_table\_name @var  
> end

Now you’re inside class Foo, @var is an instance variable of the Foo  
class object (sometimes called a “class instance variable”, but it’s  
just an instance variable like any other)

> However, as the code is right now, I’m not able to access @var from  
> within the class definition. Can someone tell me how I can accomplish  
> this?

(1) Horrible:

class Foo \< ActiveRecord::Base  
set\_table\_name eval("@var", TOPLEVEL\_BINDING)  
end

(2) Much better to use a local variable:

var = ‘brown’  
Foo.set\_table\_name var

That should work unless set\_table\_name is a private method. If it is,  
then:

(3)  
var = ‘brown’  
Foo.class\_eval { set\_table\_name var }

You see that local variables propagate happily into blocks. It’s only  
‘class’ and ‘def’ which start with a clean slate as far as local  
variables are concerned.

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 27, 2009, 6:44am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/5 "2009-05-27T06:44:56Z")

</div>

2009/5/27 eggman2001 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> this?

You can do it using eval like this:

@var = ‘brown’  
class Foo  
p eval(‘@var’,TOPLEVEL\_BINDING)  
end

Regards,  
Park H.

---

<div class="post-metadata">

**Author:** ![eggman2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@eggman2001](https://www.ruby-forum.com/u/eggman2001)\
**Post date:** [May 28, 2009, 3:18am UTC](https://www.ruby-forum.com/t/how-to-access-variables-from-within-class-definition/168891/6 "2009-05-28T03:18:17Z")

</div>

This helps a lot. Guess I should brush up on my variable types 🙂
