[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Scope of an @variable

William (Bill) Froelich

3/14/2006 10:42:00 PM

I'm also a newbie so others feel free to correct me as well ;-)

Your understanding is correct in that @name applies to the specific
instance of the class and the @@name applies to all instances of the
class.

Hope this example helps.



class Person
@@lastname = "Doe"
@name=""

def setLastName(newName)
@@lastname = newName
end

def changeName(newName)
@name = newName
end

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

def fullName()
"My full name is " + @name + " " + @@lastname
end
end

puts "---Defining John---\n"
p1 = Person.new()
p1.changeName('John')
puts p1.sayName()
puts p1.fullName()

puts "\n"

puts "---Defining Jane---\n"
p2 = Person.new()
p2.changeName('Jane')
puts p2.sayName()
puts p2.fullName()

puts "\n"

puts "---Changing Last Name---\n"
p1.setLastName('Smith')

puts "John's full name -> " + p1.fullName()
puts "Jane's full name -> " + p2.fullName()

-----Original Message-----
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of Nathan Olberding
Sent: Tuesday, March 14, 2006 4:18 PM
To: ruby-talk ML
Subject: Re: Scope of an @variable

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-....



5 Answers

Brad Tilley

3/15/2006 12:11:00 AM

0

William (Bill) Froelich wrote:
> I'm also a newbie so others feel free to correct me as well ;-)
>
> Your understanding is correct in that @name applies to the specific
> instance of the class and the @@name applies to all instances of the
> class.

This may be wrong, but I like to think of @@ as a class variable and @
as a method variable. @@ is accessible by any method in the class and @
is accessible by the method only.

Jeff

3/15/2006 12:55:00 AM

0

rtilley wrote:
> This may be wrong, but I like to think of @@ as a class variable and @
> as a method variable. @@ is accessible by any method in the class and @
> is accessible by the method only.

You're close. @ are instance variables. Any instance method in your
class can
access them.

(Maybe you're thinking of local variables, which are defined within a
method and are accessible by that method only.)

Jeff
www.softiesonrails.com
www.jeffcohenonline.com

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


Brad Tilley

3/15/2006 1:00:00 AM

0

Jeff Cohen wrote:
> rtilley wrote:
>> This may be wrong, but I like to think of @@ as a class variable and @
>> as a method variable. @@ is accessible by any method in the class and @
>> is accessible by the method only.
>
> You're close. @ are instance variables. Any instance method in your
> class can
> access them.
>
> (Maybe you're thinking of local variables, which are defined within a
> method and are accessible by that method only.)

Thanks Jeff! That makes sense. I was confusing @ vars with local vars.

benjohn

3/17/2006 12:05:00 AM

0


On 15 Mar 2006, at 00:54, Jeff Cohen wrote:

> rtilley wrote:
>> This may be wrong, but I like to think of @@ as a class variable
>> and @
>> as a method variable. @@ is accessible by any method in the class
>> and @
>> is accessible by the method only.
>
> You're close. @ are instance variables. Any instance method in your
> class can
> access them.

You can also access @ variables from a class method - ie
ClassName.function; @a_variable; end. They are local to each class
within a hierarchy, rather than @@ which would be shared among
classes in the hierarchy.

So:

class X
def X.var;
@var
end

def X.var=(v)
@var=v
end
end

class Y<X;end

X.var=10
Y.var=20
X.var => 10
Y.var => 20

Cheers,
Benjohn


dblack

3/17/2006 1:13:00 AM

0