Recently started learning ruby and I’ve created a class for family members that contains name, age, sex, marital status, and traits. I am trying to write a method that will determine if a family member is a parent and if whether or not its the mother or the father.
so the code for the method is as follows:
def is_father?(age, sex)
if age > 30
puts "is parent"
if sex == "Male"
then puts "is father"
else puts "not father"
end
end
end
And a family member might look like this:
fm1=Family.new("John", "Male", 54, "Married", "Annoying")
after being initialized like this:
class Family
def initialize(name, sex, age, status, trait)
@fam_name=name
@fam_sex=sex
@fam_age=age
@fam_stat=status
@fam_trait=trait
end
end
If a person contains the previously mentioned characteristics, how do I just pass age + sex into this method? Thanks in advance for your help
You must store your data during initialization in attributes. later you can use them without using parameters of the method.
Example:
Remarks:
is_father?– methods ending on?normally returns a boolean value.to_s.to_sis called if you print your object withputs.putsinside your methods. Most of the times, it is better to return an answer string and make theputswhen you call the method.Perhaps I misunderstand your request.
If
is_father?is no method of Family and you need access to the attributes, then you must define a getter method: