Concept of instance variable in ruby

@x = ‘Hello world’

def printMe
puts @x
end

int the above code section when a call printMe method it behaves as
expected. However if I remove the ‘@’ symbol making x as a local
variable it throws an undefined name error. which is also as expected.
But can anyone explain me in the previous case, what is the class for
@x.

Please explain me in details

Manav G. wrote in post #1035910:

@x = ‘Hello world’

def printMe
puts @x
end

int the above code section when a call printMe method it behaves as
expected. However if I remove the ‘@’ symbol making x as a local
variable it throws an undefined name error. which is also as expected.
But can anyone explain me in the previous case, what is the class for
@x.

Please explain me in details

You can find the current object by calling ‘self’. If you open irb:

1.9.3p0 :001 > self
=> main
1.9.3p0 :002 > self.class
=> Object
1.9.3p0 :003 > self.instance_variables
=> [:@prompt]
1.9.3p0 :004 > @x = “hello”
=> “hello”
1.9.3p0 :005 > self.instance_variables
=> [:@prompt, :@x]

You can see @x becomes an instance variable of ‘self’, which is an
instance of the class Object.

To expand on the nice diagnostic suggestion in the foregoing post, x and
@x are not the same variable, and they do not have the same scope.

If your program consists entirely of this code:

@x = ‘hello’
def printMe
puts x
puts @x
end

@x is an instance variable of self (the Object instance that is your
program). It is visible to all methods of self.

printMe() is a method you’ve added to self. It can see the value of @x.

x is a local variable whose scope is limited to printMe(), and is
undefined.

(That, at any rate, is how I understand it.)

On 09.12.11 13:01, Manav G. wrote:

@x.

Please explain me in details

Why does it (again) sound like a school assignment?

http://ruby.runpaint.org/methods#global

kaspar

On Friday, December 9, 2011 7:01:24 AM UTC-5, Manav G. wrote:

But can anyone explain me in the previous case, what is the class for
@x.

Please explain me in details

This can be a bit tricky b/c the toplevel namespace doesn’t behave like
this code would if it were defined within a class...end, which is more
typical.

In this case @x belongs to main which is a special object that also
proxy’s certain calls to Object class.

In the more typical case:

class X
@x = ‘Hello World’

def printMe
  puts @x
end

end

The @x in the class scope is not the same as the @x in the method
(instance
scope). Rather:

class X
@x = ‘Hello World’

def self.printMe
  puts @x   #=> 'Hello World'
end

end

@x is visible at the class level (the self. makes this a class method
instead of an instance method).

La Wi wrote in post #1036024:

If your program consists entirely of this code:

@x = ‘hello’
def printMe
puts x
puts @x
end

x is a local variable whose scope is limited to printMe(), and is
undefined.

No, that’s not quite right. x here is a method call, not a local
variable, because there has been no assignment to x earlier in the scope
(in this case the body of method ‘printMe’). That is:

puts x

is interpreted as if you had written

puts self.x

or puts x()

However, if you had written instead:

def printMe
  if false
    x = 123
  end
  puts x
end

then x would indeed be a local variable, and its value is ‘nil’ (because
the assignment which occurs earlier in the scope is not actually
executed)

Regards,

Brian.