tags:

views:

218

answers:

3

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!

+2  A: 

You can do it like this:

if my_height < self.person_of_age(age).nil? ? 0 : self.person_of_age(age)['height']

Or you can rewrite person_of_age method:

def person_of_age(age)
  some_hash = @array_of_hashes.select { |h| h.age == age }.last || {:height => 0}
  return some_hash
end

or simpler

def person_of_age(age)
  @array_of_hashes.select { |h| h.age == age }.last || {:height => 0}      
end
klew
+3  A: 

obvious:

(self.person_of_my_age(age) || {'height' => 0})['height']

but somehow this does not feel so right

roman
I like your answer. Why it doesn't feel so right?
klew
Or `(self.person_of_my_age(age) || {})['height'] || 0`, or `(self.person_of_mY_age(age) || {}).fetch('height', 0)`
Wayne Conrad
A: 

I'm not sure if this helps in your circumstance, but one capability Hash has is

hash.fetch(key) { block_for_code_evaluated_if_key_absent }
Andrew Grimm