+ 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

24th Apr 2017, 1:13 PM
Jafca
Jafca - avatar
2 Respuestas
+ 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
24th Apr 2017, 3:01 PM
Jafca
Jafca - avatar
+ 4
This is the ruby program
25th Apr 2017, 11:08 AM
ramya
ramya - avatar