为什么我无法从ruby中的选定键创建新的散列? [英] Why can't I make a new hash from selected keys in ruby?

查看:85
本文介绍了为什么我无法从ruby中的选定键创建新的散列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直在困扰我一段时间。这不是一件困难的事情,但我不知道为什么没有简单的方法来做到这一点,我敢打赌,有,我没有看到它。



我只是想采取一个哈希,像这样:

  cars = {:bob => 'Pontiac',:fred => '克莱斯勒',
:lisa => 'Cadillac',:mary => '美洲虎'}

并做类似的事情

  cars [:bob,:lisa] 

并得到

  {:bob => 'Pontiac',:lisa => '凯迪拉克'} 

我做到了这一点,它工作的很好:

  class Hash 
def pick(* keys)
Hash [select {| k,v | keys.include?(k)}]
end
end

ruby​​-1.8.7-p249:008> cars.pick(:bob,:lisa)
=> {:bob =>Pontiac,:lisa =>Cadillac}

一个简单的方法来做到这一点,但我想知道是否有内置的东西,我错过了,或者一个不明显的原因,这不是一个标准的和正常的事情?如果没有它,我会使用类似的东西:

  chosen_cars = {:bob =>汽车[:bob],:lisa =>汽车[:丽莎]} 

这不是世界末日,但它不是很漂亮。看起来这应该是常规词汇的一部分。我在这里错过了什么?



(相关问题,包括:红宝石哈希白名单过滤器
(这篇博文与我的结果完全相同,但是为什么不是内置的? http://matthewbass.com/2008/06/26/picking-values-from-ruby-hashes/


update:

我使用Rails,它具有ActiveSupport :: CoreExtensions ::哈希::切片,它的工作原理与我想要的一样,所以问题解决了,但仍然...也许别人会在这里找到他们的答案:)

我一直认为这是一个奇怪的遗漏,但实际上没有简单的标准方法。



您的示例以上可能会不必要的慢,因为它遍历所有哈希条目,无论我们是否需要它们,以及n重复搜索键参数数组。这段代码应该快一点(假设这会有所影响 - 我还没有试图对它进行基准测试)。

  class Hash 
def pick(* keys)
values = values_at(* keys)
Hash [keys.zip(values)]
end
end


This has been bugging me for a while. It's not a difficult thing, but I don't know why there's no easy way to do it already, and I bet there is and I don't see it.

I just want to take a hash, like this:

cars = {:bob => 'Pontiac', :fred => 'Chrysler', 
        :lisa => 'Cadillac', :mary => 'Jaguar'}

and do something like

cars[:bob, :lisa]

and get

{:bob => 'Pontiac', :lisa => 'Cadillac'}

I did this, which works fine:

class Hash
  def pick(*keys)
    Hash[select { |k, v| keys.include?(k) }]
  end
end

ruby-1.8.7-p249 :008 > cars.pick(:bob, :lisa)
=> {:bob=>"Pontiac", :lisa=>"Cadillac"} 

There's obviously a zillion easy ways to do this, but I'm wondering if there's something built in I've missed, or a good an un-obvious reason it's not a standard and normal thing? Without it, I wind up using something like:

chosen_cars = {:bob => cars[:bob], :lisa => cars[:lisa]}

which isn't the end of the world, but it's not very pretty. It seems like this should be part of the regular vocabulary. What am I missing here?

(related questions, include this: Ruby Hash Whitelist Filter) (this blog post has precisely the same result as me, but again, why isn't this built in? http://matthewbass.com/2008/06/26/picking-values-from-ruby-hashes/ )

update:

I'm using Rails, which has ActiveSupport::CoreExtensions::Hash::Slice, which works exactly as I want it to, so problem solved, but still... maybe someone else will find their answer here :)

解决方案

I've always thought that was a weird omission as well, but there really is no simple, standard method for that.

Your example above might be unnecessarily slow because it iterates over all the hash entries whether we need them or not, and then repeatedly searches through the keys parameter array. This code should be a bit faster (assuming that would ever matter -- and I haven't tried to benchmark it).

class Hash
  def pick(*keys)
    values = values_at(*keys)
    Hash[keys.zip(values)]
  end
end

这篇关于为什么我无法从ruby中的选定键创建新的散列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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