--00151751146c2bed8504665e4791
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

All variables in Ruby (Local, Global, Instance (object), or Class) do not
need to be defined at the beginning of a method.

I believe what we have is a miscommunication on the terminology!

Programing in general, when one defines a variable, then you are telling the
compiler what type of variable it is: String, Integer, Float, etc...

When you set a variable, then you are giving that variable a value.

I will use C++ as an example:

#include <iostream>

int main()
{
   using std::cout;
   using std::endl;

//Notice how you must define the variable before you set it's value
  unsigned short int Width = 5;
  unsigned short int Length = 10;
  unsigned short int Area = (Width * Length)

  cout << "Width: " << Width << endl;
  cout << "Length: " << Length << endl;
  cout << "Area: " << Area << endl;

  return 0;
}

====
Width: 5
Length: 10
Area: 500
====

In Ruby you set variables, and then ruby auto defines that variable.

In Ruby, do you need to set that variable before calling it? Yes.

In Ruby, do you need to define that variable before setting it? No.


Here is a good way to use a Class variable:

class Square
   def initialize
       if defined?(@@number_of_squares)
          @@number_of_squares += 1
       else
          #No need to define the variable before it's value is set
          @@number_of_squares = 1
       end
   end

   def Square.count
       @@number_of_squares
   end
end

a = Square.new
puts Square.count
b = Square.new
puts Square.count
c = Square.new
puts Square.count

====
1
2
3
====

I hope that clarifies some confusion on the  terminology being used, and it
helps in clearing up the confusion on how to use a Class variable in Ruby ;)

Peace
JC

On Mon, Mar 30, 2009 at 6:30 PM, Iki Baz Castillo <ibc / aliax.net> wrote:

> El Martes 31 Marzo 2009, Joshua Collins escribi
> > [quote]This is not true when using class variables (@@). These need to be
> > "declared"
> > at the beginning of the class:[/quote]
> >
> > You are incorrect.
>
> Perhaps you could detail it a little more.
>
> Yes, surely there are other ways to use class variables without assigning
> them
> a value into the class and out of class methods, but what I mean is that
> class
> variables are not like instance variables since they must exist prior to
> being
> used. Am I wrong?
>
> --
> Iki Baz Castillo <ibc / aliax.net>
>
>

--00151751146c2bed8504665e4791--