When to use instance variables @


#1

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file - I do
    understand the accessors are automatic but I’m not sure if I can just
    pull a @product in the middle of a function

  2. do i need to use the @ to refer to them in the class. Would it work
    without the @, and if so, how does it differentiate them from local
    vars?

  3. I’ve seen code where just below the class declaration, objects are
    instantiated like product = Product.new - I don’t see a @ sign, does
    that mean it’s a local var? How can a local var even exist at the class
    level, outside a function?

Many thanks!


#2

On Mar 30, 1:52 pm, Steve D. removed_email_address@domain.invalid wrote:

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file - I do
    understand the accessors are automatic but I’m not sure if I can just
    pull a @product in the middle of a function

No. You can ask for any instance variable at any time - if no value
has ever been set, you will get nil back. You can set any instance
variable at any time. A common idiom is:
@foo ||= 42
which is the same as “@foo = @foo || 42” which technically means “Set
@foo to 42 if is it currently false or nil”; when you’re not dealing
with an instance variable that deals in boolean values, however, it
basically means “Set @foo to 42 unless I already set it to some
value.”

  1. do i need to use the @ to refer to them in the class. Would it work
    without the @, and if so, how does it differentiate them from local
    vars?

It does not work without the @. You must use it everywhere, even when
using something like:
variable_name = “foo”
instance_variable_set( :"@#{variable_name}", 42 )

  1. I’ve seen code where just below the class declaration, objects are
    instantiated like product = Product.new - I don’t see a @ sign, does
    that mean it’s a local var? How can a local var even exist at the class
    level, outside a function?

Class ‘declarations’ aren’t quite what you think they are. They’re
actually code that is executed. Try this on for size:

msg = “outside”
puts “#{msg} class”
class Foo
msg = “inside”
puts “#{msg} class”
end
puts “#{msg} class”

Note in particular the last message; the output “outside class” lets
you know that there are two local variables with the same name, in
their own scopes.


#3

On Mar 30, 2:05 pm, Phrogz removed_email_address@domain.invalid wrote:

Class ‘declarations’ aren’t quite what you think they are. They’re
actually code that is executed. Try this on for size:

Here’s a more real example of why you might want to write arbitrary
code while creating (or modifying) a class:

class Person
%w| age weight sex |.each do |param|
variable_name = “@#{param}”.to_sym
define_method( param ) do
puts “You just accessed #{param}”
instance_variable_get( variable_name )
end
define_method( “#{param}=” ) do |new_value|
puts “Now setting #{param} to #{new_value}”
instance_variable_set( variable_name, new_value )
end
end
end

me = Person.new

me.age = 42
#=> “Now setting age to 42”

puts me.age
#=> “You just accessed age”
#=> 42


#4

Steve D. wrote:

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file

In Ruby you declare nothing. (That’s unlike languages like Java, C,
etc., where everything has to be declated before use.)

So, in Ruby you don’t declare variables. In fact, you don’t even declare
functions and classes. (You “create” them.)


#5

Steve D. wrote:

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file - I do
    understand the accessors are automatic but I’m not sure if I can just
    pull a @product in the middle of a function

No, you don’t need to declare them at the top of your file, just before
you use them. It’s good practice to initialise each of your class
variables in your constructor, but this is not always possible.
in the middle of a member function, you can just pull a ‘@product =
something’ even if you haven’t already defined it, but bugs are harder
to trace if variables start popping up all over the place.

  1. do i need to use the @ to refer to them in the class. Would it work
    without the @, and if so, how does it differentiate them from local
    vars?

Yes, always with the @ that’s exactly how their scope is known to the
interpreter.

  1. I’ve seen code where just below the class declaration, objects are
    instantiated like product = Product.new - I don’t see a @ sign, does
    that mean it’s a local var? How can a local var even exist at the class
    level, outside a function?

a local var existing at the class level outside a function? That to me
means something like this:

class Klass
product = Product.new

def some_function
nil
end
end

That doesn’t make sense to me and has no accessible means (I could be
wrong on this one though). It’s valid syntax, but to my understanding,
it will be instantiated and then immediately set for garbage
collection. A subtle note is that if the variable name starts with a
Capital letter, then it is a constant that belongs to that class and has
java-like public scope, so can be referred to by Klass::Constant

Also, you might see some people write code that looks procedural, but
uses @variables and so forth. This is because you are by default in the
Object class’s scope. Hmmm, this makes me think that local variables at
the class level should be treated identically as the procedural-like
code at the Object scope. I think I have just confused myself, can
anyone clarify this? (Of course, I’m not actually going to use this
feature, even if it exists, but it would be nice to understand)

Many thanks!

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.


#6

El Martes 31 Marzo 2009, Albert S. escribió:

In Ruby you declare nothing. (That’s unlike languages like Java, C,
etc., where everything has to be declated before use.)

This is not true when using class variables (@@). These need to be
“declared”
at the beginning of the class:

class KK

@@koko   # <-- WRONG since no assignement is done.
         #     @@koko = #SOMETHING#   is required.

def show_var
  puts "koko = #{@@koko}"
end

def set_var(v)
  @@koko = v
end

end

irb(main):022:0> k=KK.new

irb(main):023:0> k.show_var
NameError: uninitialized class variable @@koko in KK

irb(main):024:0> k.set 123
irb(main):025:0> k.show_var
koko = 123


#7

[quote]This is not true when using class variables (@@). These need to
be
“declared”
at the beginning of the class:[/quote]

You are incorrect.


#8

El Martes 31 Marzo 2009, Joshua C. 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?


#9

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

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 :wink:

Peace
JC


#10

El Martes 31 Marzo 2009, Joshua C. escribió:

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

I didn’t say “at the beginning of a method”, but “at the beginning of a
class”
(anyhow I was also wrong XD).

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

Well, this is different for class variables and instance variables:


irb> @non_existing_instance_variable
nil

irb> @@non_existing_class_variable
NameError: uninitialized class variable @@non_existing_class_variable in
Object
from (irb):2
from /usr/bin/irb:12:in `’

What I mean is that class variables MUST be defined (not declared, of
course)
before reading them, while an instance variable returns nil if it’s
not set
yet.

      #No need to define the variable before it's value is set
      @@number_of_squares = 1

Yes, please note that I said:

This is not true when using class variables (@@). These need to be
“declared”
at the beginning of the class

(note the “declared” between “” XD)

Regards.


#11

Ah right.

I totally forgot about instance auto set to nil :wink:

I see where you were coming from now.


#12

El Martes 31 Marzo 2009, Joshua C. escribió:

Ah right.

I totally forgot about instance auto set to nil :wink:

Yes, I just wanted to point that, but I chose a wrong and prohibited
word in
Ruby (“declare”) XDDD

Regards.


#13

Steve D. removed_email_address@domain.invalid wrote:

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file - I do
    understand the accessors are automatic but I’m not sure if I can just
    pull a @product in the middle of a function

No, nor can you! Code that looks like it is declaring them isn’t. A
common beginner mistake is illustrated by this experimented:

class C
@greeting = “howdy” # doesn’t do what you might suppose
def greet
puts @greeting
end
end
C.new.greet #=> nil # surprise!

The @greeting that looks like it’s being declared in the second line is
not, in fact, the instance variable mentioned inside the “greet” method.
(It is something else entirely - a “class instance variable”.) m.


#14

7stud – wrote:

  1. I’ve seen code where just below the class declaration, objects are
    instantiated like product = Product.new - I don’t see a @ sign, does
    that mean it’s a local var? How can a local var even exist at the class
    level, outside a function?

class Product
attr_accessor :product

def initialize
@product = “Shoes”
end
end

class Person

PersonProduct = Product.new

def initialize(name, age, product)
@name = name
@age = age
@product = product
end

def show
puts “We all have #{PersonProduct.product}”
puts “And I have #{@product}”
end

end

p = Person.new(“Steve”, 25, “Sandals”)
p.show

–output:–
We all have Shoes
And I have Sandals


#15

Steve D. wrote:

Hi, I have a couple questions about instance variables in Ruby.

  1. do i need to declare them at the top of my class file - I do
    understand the accessors are automatic

Then you’re understanding is faulty:

class Person

def initialize(name, age)
@name = name
@age = age
end

end

p = Person.new(“Steve”, 25)
puts p.name

–output:–
undefined method `name’ for #<Person:0x2529c @age=25, @name=“Steve”>
(NoMethodError)

but I’m not sure if I can just
pull a @product in the middle of a function

They are called “methods” in ruby.

class Person

def initialize(name, age)
@name = name
@age = age
end

def somefunc(x)
@product = x
end

def show
puts “I have #{@product}”
end

end

p = Person.new(“Steve”, 25)
p.somefunc(“Shoes”)
p.show

–output:–
I have Shoes

  1. do i need to use the @ to refer to them in the class. Would it work
    without the @,

class Person
def initialize(name, age)
@name = name
@age = age
end

def somefunc(x)
@product = x
end

def show
puts “I have #{product}” #<-- no @
end

end

p = Person.new(“Steve”, 25)
p.somefunc(“Shoes”)
p.show

–output:–
in show undefined local variable or method `product’ for
#<Person:0x24ef0 @age=25, @product=“Shoes”, @name=“Steve”> (NameError)

and if so, how does it differentiate them from local
vars?

class Person

def initialize(name, age)
@name = name
@age = age
end

def somefunc(x)
@product = x
end

def show
product = “Sandals”
puts “I have #{product}”
puts “I have #{@product}”
end

end

p = Person.new(“Steve”, 25)
p.somefunc(“Shoes”)
p.show

–output:–
I have Sandals
I have Shoes

  1. I’ve seen code where just below the class declaration, objects are
    instantiated like product = Product.new - I don’t see a @ sign, does
    that mean it’s a local var? How can a local var even exist at the class
    level, outside a function?

class Product
def initialize
@product = “Shoes”
end
end

class Person
product = Product.new

def initialize(name, age)
@name = name
@age = age
end

def show
#puts product
#puts @product
#puts @@product
puts Person.product
end

end

p = Person.new(“Steve”, 25)
p.show

–output:–
in show': undefined methodproduct’ for Person:Class (NoMethodError)
from r3test.rb:24


#16

7stud – wrote:

puts Person::PersonProduct.product

–output:–
Shoes


#17

Thank you to everyone who answered - yes it makes a lot more sense now!

It made me think though - when should you use self? I know you can use
self to declare class methods but why do I see some people prefix just
about anything with self. ? Isn’t ‘self’ implied?

Cheers!


#18

Steve D. wrote:

Thank you to everyone who answered - yes it makes a lot more sense now!

It made me think though - when should you use self? I know you can use
self to declare class methods but why do I see some people prefix just
about anything with self. ? Isn’t ‘self’ implied?

Cheers!

I for one sometimes prefix methods with self to help visually
distinguish them from local variables. I did this a lot when I was less
experienced in ruby, and I still do it occasionally. You must specify
the self receiver if you’re calling an assignment method, or the
interpreter will think you are trying to assign to a local variable.
It’s also mandatory if you have a method with the same name as a local
variable, or whose name begins with a capital letter (although frankly,
you probably should just change the method name or the variable name in
these cases):

hello = 1

def hello
2
end

hello # => 1
self.hello # => 2

def Hello
3
end

Hello # => NameError: uninitialized constant Hello
self.Hello # => 3

I’ve often thought that the ruby would be just a tad bit cleaner if
local variables and implicit self method calls were distinguishable from
each other syntactically. Something like giving implicit self method
calls a bare dot prefix. I’m sure there are a lot of people who would
disagree, though, and it’s unlikely to happen at this point regardless.
Ah, well.


#19

Adam G. wrote:

It’s also mandatory if you have a method with the same name as a local
variable, or whose name begins with a capital letter (although frankly,
you probably should just change the method name or the variable name in
these cases)

In that case you can just add parens instead of prefixing self:
def x
42
end
x=23
[x, x(), self.x] #=> [23, 42, 42]


#20

Adam G. removed_email_address@domain.invalid writes:

I’ve often thought that the ruby would be just a tad bit cleaner if
local variables and implicit self method calls were distinguishable from
each other syntactically. Something like giving implicit self method
calls a bare dot prefix. I’m sure there are a lot of people who would
disagree, though, and it’s unlikely to happen at this point regardless.
Ah, well.

You mean like that:

C/USER[55]> hello
1
C/USER[56]> (hello)
2

irb(main):260:0> hello
hello
1
irb(main):261:0> (hello)
(hello)
1

Ah, but for that to work, you would need to use a whole lisp, not a
matzacred lisp.

Have a look at http://clisp.cons.org/
http://cliki.net/
http://www.gigamonkeys.com/book/