+ 3

Why this code only give one answer?

in p I have entered 4 things.. but it only replies programming.. how can I get 4 things as output https://code.sololearn.com/cZD8qxlLZJ88/?ref=app

14th May 2018, 5:22 PM
Fahim UL Haque
Fahim UL Haque - avatar
3 Respostas
+ 3
It works like this. But I don't know Ruby well enough to say it a good solution. class Person def initialize(name,age,favcolor,hobby) @age=age @favcolor=favcolor @hobby=hobby @name = name end def get_name @name end def get_favcolor @favcolor end def get_age @age end def get_hobby @hobby end end p= Person.new("Fahim",20,"Blue","Programing") puts p.get_name puts p.get_hobby
14th May 2018, 5:34 PM
Paul
Paul - avatar
+ 2
in ruby, every method returns the evaluated result of the last line that is executed that's why you get only one answer you can try this: class Person attr_reader :age,:favcolor,:name,:hobby def initialize(name,age,favcolor,hobby) @age = age @favcolor = favcolor @hobby = hobby @name = name end end per = Person.new("Fahim",20,"Blue","Programing") puts per.age puts per.favcolor puts per.hobby puts per.name
14th May 2018, 5:55 PM
MO ELomari
MO ELomari - avatar
0
why using per not person??
14th May 2018, 5:59 PM
Fahim UL Haque
Fahim UL Haque - avatar