[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Scope of an @variable

Nathan O.

3/14/2006 10:03:00 PM

I've got a class. I want some methods of this class to be able to edit
some data that's "global" within any given instance of this class. For
example:

class Person

@name

def changeName(newName)
@name = newName
end

def sayName()
puts "My name is " + @name
end
end

It seems that @name reverts back once I leave the scope of any method
that manipulates it.

--
Posted via http://www.ruby-....


6 Answers

Adam Shelly

3/14/2006 10:06:00 PM

0

you need to use @@name.

On 3/14/06, Nathan Olberding <nathan.olberding@gmail.com> wrote:
> I've got a class. I want some methods of this class to be able to edit
> some data that's "global" within any given instance of this class. For
> example:
>
> class Person
>
> @name
>
> def changeName(newName)
> @name = newName
> end
>
> def sayName()
> puts "My name is " + @name
> end
> end
>
> It seems that @name reverts back once I leave the scope of any method
> that manipulates it.
>
> --
> Posted via http://www.ruby-....
>
>


Nathan O.

3/14/2006 10:18:00 PM

0

Adam Shelly wrote:
> you need to use @@name.

I was under the impression (newbie alert) that @name was for instances
and @@name was for classes as a whole (ie, @@ variables change that
value in all instances of Class). Is there a way to have variables that
apply to all instances of a Class?

--
Posted via http://www.ruby-....


Daniel Harple

3/14/2006 10:36:00 PM

0


On Mar 14, 2006, at 11:18 PM, Nathan Olberding wrote:

> I was under the impression (newbie alert) that @name was for instances
> and @@name was for classes as a whole (ie, @@ variables change that
> value in all instances of Class). Is there a way to have variables
> that
> apply to all instances of a Class?

Use an initialize method:

class Person
def initialize(name="Anonymous")
@name = name
end
attr_accessor :name
def to_s
"My name is #{@name}"
end
end

person = Person.new
puts person # -> My name is Anonymous
person.name = "Fred"
puts person # -> My name is Fred

This should help you: http://ruby-doc.org/docs/Progr...

-- Daniel


dblack

3/15/2006 2:13:00 AM

0

Stephen Waits

3/15/2006 2:18:00 AM

0

dblack@wobblini.net wrote:
>
> Note that self changes between a class definition and an instance
> method definition:

Light bulbs floating above list readers' heads around the world just
illuminated.

Thanks for the great explanation!

--Steve



Adam Shelly

3/15/2006 7:51:00 PM

0

On 3/14/06, Nathan Olberding <nathan.olberding@gmail.com> wrote:
> Adam Shelly wrote:
> > you need to use @@name.
>
> I was under the impression (newbie alert) that @name was for instances
> and @@name was for classes as a whole (ie, @@ variables change that
> value in all instances of Class). Is there a way to have variables that
> apply to all instances of a Class?

Oops, I read the question too quickly. Sorry. David Black has the
right explanation.
-Adam