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-forum.com/.