Ok, let's simplicity your class to contain only sex:
class Person
def initialize sex
raise "wrong sex" unless ['m', 'f'].include?(sex)
@sex=sex
end
attr_reader :sex
end
tom = Person.new 'm' # ok
anna = Person.new 'f' # ok
drwho = Person.new # ArgumentError: wrong number of arguments (0 for 1)
wrong = Person.new 'cat' # RuntimeError: wrong sex
You cannot create Person
with sex other than m
or f
(as you said).
Can you change the sex
?
tom.sex='ff'
# NoMethodError: undefined method `sex=' for #<Person:0x3f32148 @sex="m">
Is there other way to change sex
?
tom.instance_variable_set '@sex', 'cat'
# => "cat"
tom.sex
# => "cat"
To sum up:
Can I Assume It's Female
It depends on 2 factors:
- Speed
- Correctness
If you care about speed, you can avoid it, as you can see I used hackish way to change sex
.
If you must be 100% sure, use that elsif
.