嵌套哈希已定义?()
确定是否定义了 @hash[:key1][:key2]
的最简洁方法是什么,如果 @hash
或 @hash 则不会引发错误[:key1]
为零?
如果@hash[:key1]
存在,define?(@hash[:key1][:key2])
返回True(它不判断:key2< /code> 已定义)
What's the most concise way to determine if @hash[:key1][:key2]
is defined, that does not throw an error if @hash
or @hash[:key1]
are nil?
defined?(@hash[:key1][:key2])
returns True if @hash[:key1]
exists (it does not determine whether :key2
is defined)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当使用 ActiveSupport (Rails) 或 Backports 时,您可以使用
try
:您甚至可以将
@hash
处理为nil
:如果您想要
@hash
始终返回丢失键的哈希值:您也可以定义此递归:
但要回答您的问题:
When using ActiveSupport (Rails) or Backports, you can use
try
:You could even handle
@hash
beingnil
:If you want
@hash
to always return a hash for a missing key:You could also define this recursive:
But to answer your question:
也许我遗漏了一些东西,但如果您关心的只是简洁......为什么不呢:
或者如果您想保存一些字符,
如果其中任何部分失败,它会返回
nil
否则它会返回与:key2
或true
关联的值。即使
:key2
不存在,define?
也会返回 true,因为它只是检查您引用的对象是否存在,在这种情况下就是方法[]
是方法fetch
的别名,该方法确实存在于哈希@hash[:key1]
上,但如果返回 nil,则有nil
上没有 fetch 方法,它将返回nil
。话虽这么说,如果您必须n
深入到嵌入的哈希中,在某些时候调用会变得更有效:Perhaps I am missing something, but if all you care about is concise...why not:
or if you want to save a few characters
if any part of this fails, it returns
nil
otherwise it returns the value associated with:key2
ortrue
.The reason the
defined?
returns true even if:key2
is not there is because it just checks whether the object you are referencing exists, which in that case is the method[]
which is an alias for the methodfetch
which does exist on the hash@hash[:key1]
but if that were to return nil, there is no fetch method onnil
and it would returnnil
. That being said, if you had to gon
deep into an embedded hash, at some point it would become more efficient to call:使用 Hash#fetch
您可以使用 Hash#fetch 方法,默认值为
{}
,这样即使第一级密钥不存在,也可以安全地调用has_key?
。例如Alternative
或者,您可以使用条件运算符,例如
,如果
hash
没有密钥key1
则返回false
无需寻找第二级钥匙。如果确实有key1
,则返回检查key1
值是否有key2
的结果。另外,如果您想在调用之前检查
hash[key1]'s
值是否具有has_key?
方法:Using Hash#fetch
You can use the Hash#fetch method with a default of
{}
so that it is safe to callhas_key?
even if the first level key doesn't exist. e.g.Alternative
Alternatively you can use the conditional operator e.g.
i.e. if
hash
doesn't have keykey1
then just returnfalse
without looking for the second level key. If it does havekey1
then return the result of checkingkey1's
value forkey2
.Also, if you want to check that
hash[key1]'s
value has ahas_key?
method before calling it:如果您不关心区分不存在的
@hash[:key1][:key2]
(在 3 个级别中的任意一个)和@hash[:key1][:key2] == nil
,这非常干净,适用于任何深度:如果您希望将
nil
视为现有的,请使用以下代码:If you don't care about distinguishing nonexistent
@hash[:key1][:key2]
(at any of 3 levels) from@hash[:key1][:key2] == nil
, this is quite clean and works for any depth:If you want
nil
to be treated as existing, use this instead:我刚刚发现的另一种选择是使用
seek
方法扩展 Hash。技术来自 科里·奥丹尼尔。将其放入初始值设定项中:
然后只需调用:
您将获得该值,如果不存在则返回 nil 。
Another option, one that I just discovered, is to extend Hash with a
seek
method. Technique comes from Corey O'Daniel.Stick this in an initializer:
Then just call:
You'll get the value, or nil if it doesn't exist.