开发者

Using ruby 'or equals' ||= on methods that return a hash or nil

开发者 https://www.devze.com 2022-12-20 05:55 出处:网络
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

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 aro开发者_如何学编程und this problem, but if ||= can be used I'd love to know how.

Thank you!


obvious:

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

but somehow this does not feel so right


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


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 }
0

精彩评论

暂无评论...
验证码 换一张
取 消