+ 12
[Solved] Instance variable outside of instance method
#what does line 2 do? class Person @age = 0 def initialize(name, age) @name = name puts @age end end ob = Person.new("Jafca", 42) https://code.sololearn.com/cj3L4nsnZsy4/?ref=app
2 odpowiedzi
+ 11
Finally found something:
Line 2 sets the default value of @age to 0.
Line 2 @age is a "class instance variable".
Line 4 @age is an "instance variable".
These two are separate variables, even though they have the same names!
The class instance variable is accessible in class methods e.g.
def self.c
...
end
The instance variable is accessible in instance methods e.g.
def initialize
...
end
def m
...
end
Source: http://stackoverflow.com/a/27649945
+ 4
This is the ruby program