I have a method that returns a hash, or nil:
def person_of_age(age)
some_hash = @array_of_hashes.select { |h| h.age == age }.last
return some_hash
end
I want to use this hash like so:
my_height = 170
my_age = 30
if my_height < self.person_of_age(my_age)['height']
puts "You are shorter than another person I know of the same age!"
end
Now, if the hash returns nil, ruby doesn't like me using ['height']:
undefined method `[]' for nil:NilClass (NoMethodError)
Fair enough, but how can I use ||= to avoid this problem? If the method does return nil, let's just say I want the 'height' to be 0.
I have tried things along the lines of, to no avail:
if my_height < self.person_of_age(age)||={ 'height' => 0 }['height']
#...
if my_height < self.person_of_age(age)['height'] ||= 0
Clearly my example is running a little thin, and there are other ways around this problem, but if ||= can be used I'd love to know how.
Thank you!