---------------------Start of
Code---------------------------------------
class Square
def initialize
if defined?(@@number_of_squares)
@@number_of_squares += 1
else
@@number_of_squares = 1
end
end
def self.count
@@number_of_squares
end
end
a = Square.new
b = Square.new
c = Square.new
puts Square.count
-------------------End of
Code-------------------------------------------
Anyone can explain under the “else” statement
if i define @@number_of_squares = 2
when i puts my statements it returns 4.
A class variable exists across all instances of the class. By defining
the first instance of the variable as 2, and then creating another two
classes which each increment it by 1, you get 4.
A class variable exists across all instances of the class. By defining
the first instance of the variable as 2, and then creating another two
classes which each increment it by 1, you get 4.
But isn’t it weird when you create a new object for square a it starts
with number 1? there’s a if defined?(@@number_squares), so if a is
created it should start from 1? why did the code goes to else statement
instead?
Because a class variable exists across all instances of the class. If
you want a variable that only exists inside one instance, use an
instance variable.
Because a class variable exists across all instances of the class. If
you want a variable that only exists inside one instance, use an
instance variable.
Ok i get what you meant. Basically class variable is just 1 method
within the class itself right? object variable/instance variable is
only on the method itself? correct?
No, a class variable is shared between every instance of that class.
An instance variable is individual to each instance of that class.
if class Person has the variables @@a and @a, then @@a can be see by
person1, person2, person3, etc. @@a has the exact same value for each
instance. @a can have a different value for each instance, so person1
could have @a == 1, person 2 could have @a == 5, etc.
No, a class variable is shared between every instance of that class.
An instance variable is individual to each instance of that class.
if class Person has the variables @@a and @a, then @@a can be see by
person1, person2, person3, etc. @@a has the exact same value for each
instance. @a can have a different value for each instance, so person1
could have @a == 1, person 2 could have @a == 5, etc.
ok thx. I want to ask you another questions
what’s the difference between set_name(name) and @set_name = name
class Person
def initialize(name)
set_name(name)
end
def name
@first_name + ' ' + @last_name
end
def set_name(name)
first_name,last_name = name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name(name)
if name.length < 4
puts "Please ensure that your first name is more than 4
characters"
else @first_name = name
end
end
def set_last_name(name)
@last_name = name
end
end
This is my code above. it’s a working code.
but i want to clarify on the first method
def initialize(name)
set_name(name)
end
AND
def initialize(name) @set_name = name
the 2nd one i know. the name assigns whatever name you supplied to it to
the @set_name. but what’s the difference on the first method
set_name(name)?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.