方法从哈希从红宝石按键阵列获得价值 [英] Method to get value from a hash from array of keys in Ruby

查看:122
本文介绍了方法从哈希从红宝石按键阵列获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写它返回一个哈希键的值的函数,当按键阵列提供的(和'零'如果该键不存在)。

考虑散列:

  my_hash = {
  FONT_SIZE:10,
  FONT_FAMILY:宋体,
  嘘:{
    名称:'胡说'
  }
}

该方法的存根可能是:

 高清的get_value(哈希,array_of_keys)
  ...
结束

原因是,我可以在实际上可能不存在的散列访问不同的密钥。因此,例如,我想'嗒嗒',通常我会叫 my_hash [:嘘] [:名字] 但是它可能不存在,也可能是非常深的。我想这样做是有我的功能,我可以用的get_value(my_hash,[:嘘,:名字])调用,这样我可以测试每个键的存在(所以我如果其中任何一个不存在没有得到例外,哪里都无所谓深的价值怎么可能)。

在我的情况下,它的不可行,检查我要求每个值的存在(我可能需要测试连续10个键的存在),我只需要我可以说,我需要它的价值和得到,如果它的值的函数存在,零如果没有(那么一个异常没有抛出试图检索它)。

我给它一个镜头,试图用一个递归函数的'流行每次循环的时间是第一个元素数组中,但我不能锻炼如何回报我的价值。

 高清的get_value(VAL,ARR)
  如果arr.count> 1
    arr.each做| A |
      的get_value VAL [α],arr.pop
    结束
  其他
    VAL [一]
  结束
结束
的get_value S,[:嘘,:名字]

这是我的企图(这并不明显工作) - 谁能帮我解决这个问题,或者有一个替代的解决方案,可能是更优雅?有几点:


  • 的按键不能保证存在,我想返回nil在
    这种情况下,和

  • 的哈希可能是非常深的,因此需要处理的散列值和阵列中的任何数字键的(这就是为什么我认为递归可能是答案,但现在我不那么肯定)。


解决方案

  [:FONT_SIZE] .inject(my_hash){| H,K | h.to_h [k]的}#=> 10
[:嘘,:名字] .inject(my_hash){| H,K | h.to_h [k]的}#=> 嗒嗒
[:嘘,:美孚] .inject(my_hash){| H,K | h.to_h [k]的}#=>零

I am trying to write a function which returns the value of a hash key, when provided with an array of keys (and 'nil' if the key doesn't exist).

Consider the hash:

my_hash = { 
  font_size: 10, 
  font_family: "Arial", 
  boo: { 
    name: 'blah' 
  } 
}

A stub of the method might be:

def get_value(hash, array_of_keys)
  ...
end

The reason being that I can access different keys in the hash which may not actually exist. So for example, I want 'blah', normally I would call my_hash[:boo][:name] however it may not already exist or it may be very deep. What I would like to do is have my function, which I could call with get_value(my_hash, [:boo, :name]) so that I can test that each key exists (so I don't get exceptions if any of them do not exist, and where it doesn't matter how 'deep' the value might be).

In my case, its not feasible to check the existence of each value I require (I might need to test existence of 10 consecutive keys) I simply need a function that I can say which value I need and get the value if it exists, and nil if it doesn't (so an exception isn't thrown trying to retrieve it).

I gave it a shot, trying to use a recursive function that 'pop's the first element in the array each time it loops but i couldn't workout how to return my value.

def get_value(val, arr)
  if arr.count > 1
    arr.each do |a|
      get_value val[a], arr.pop
    end
  else
    val[a]
  end
end


get_value s, [:boo, :name]

This is my attempt (which doesn't work obviously) - can anyone help me solve this problem, or have an alternate solution that might be more elegant? A couple of points:

  • The keys are not guaranteed to exist, and I'd like to return nil in those cases, and
  • The hash could potentially be very deep so it needs to handle any hash and any number of keys in the array (which is why I thought recursion might be the answer, but now I'm not so sure).

解决方案

[:font_size].inject(my_hash){|h, k| h.to_h[k]} #=> 10
[:boo, :name].inject(my_hash){|h, k| h.to_h[k]} #=> "blah"
[:boo, :foo].inject(my_hash){|h, k| h.to_h[k]} #=> nil

这篇关于方法从哈希从红宝石按键阵列获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆