[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to access variables from within class definition

eggman2001

5/27/2009 3:07:00 AM

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?

4 Answers

Marnen Laibow-Koser

5/27/2009 4:06:00 AM

0

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@marnen.org
--
Posted via http://www.ruby-....

Heesob Park

5/27/2009 4:44:00 AM

0

2009/5/27 eggman2001 <sodani@gmail.com>:
> I've got something like:
>
> @var =3D 'brown'
>
> class Foo < ActiveRecord::Base
> =C2=A0set_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?
>
You can do it using eval like this:

@var =3D 'brown'
class Foo
p eval('@var',TOPLEVEL_BINDING)
end

Regards,
Park Heesob

Brian Candler

5/27/2009 8:42:00 AM

0

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.
--
Posted via http://www.ruby-....

eggman2001

5/28/2009 1:17:00 AM

0

This helps a lot. Guess I should brush up on my variable types :-)

On May 27, 4:42=A0am, Brian Candler <b.cand...@pobox.com> wrote:
> eggman2001 wrote:
> > I've got something like:
>
> > @var =3D 'brown'
>
> This is an instance variable of the top-level (main) object.
>
> > class Foo < ActiveRecord::Base
> > =A0 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
> =A0 set_table_name eval("@var", TOPLEVEL_BINDING)
> end
>
> (2) Much better to use a local variable:
>
> var =3D 'brown'
> Foo.set_table_name var
>
> That should work unless set_table_name is a private method. If it is,
> then:
>
> (3)
> var =3D '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.
> --
> Posted viahttp://www.ruby-....