Forum: Ruby Question on Class Variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-02 16:25
---------------------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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-02 22:01
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.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-03 05:55
Joel Pearson 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?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-03 09:53
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.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-03 11:12
Joel Pearson 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?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-03 12:09
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.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-03 18:44
Joel Pearson 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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-03 18:49
set_name is undefined, so it will error.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-04 03:58
Joel Pearson 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)?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-04 12:43
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.
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-09-05 13:04
Joel Pearson 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?
This topic is locked and can not be replied to.