+ 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
3 Antworten
+ 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
+ 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
0
why using per not person??