Question on Class Variables

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

Thanks

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.

Joel P. wrote in post #1184933:

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.

Joel P. wrote in post #1184944:

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.

set_name is undefined, so it will error.

Joel P. wrote in post #1184946:

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

def initialize(name)
set_name(name)
end

AND

def initialize(name)
@set_name = name
end

One is storing the name in two variables, @first_name and @last_name,
and you can re-use the methods to set the name in three different ways.

The other is storing the name in one variable, and can only set it when
the class is initialised.

Joel P. wrote in post #1184954:

One is storing the name in two variables, @first_name and @last_name,
and you can re-use the methods to set the name in three different ways.

The other is storing the name in one variable, and can only set it when
the class is initialised.

Ok I get it. So whenever e.g you set_name(name) you can split the method
and reuse it right?

Joel P. wrote in post #1184950:

set_name is undefined, so it will error.

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs