Ruby 中是否可以像 PHP 中那样自动初始化多维哈希数组?

发布于 2024-09-07 19:32:34 字数 1565 浏览 1 评论 0原文

我已经习惯了在 PHP 中使用多维数组,我可以通过

unset($a); // just to show that there is no variable $a
$a['settings']['system']['memory'] = '1 Gb';
$a['settings']['system']['disk space'] = '100 Gb';

Is there a way to do those in Ruby? 来分配和初始化哈希?或者我需要先初始化所有维度,然后再赋值。是否可以定义一个高级哈希来执行我需要的操作?你会怎么做?


更新

除了Douglas提出的解决方案(见下文)之外,我还找到了关于该主题的线程,其中 Brian Schröäer 提出了 Hash 类的扩展:

class AutoHash < Hash
  def initialize(*args)
    super()
    @update, @update_index = args[0][:update], args[0][:update_key] unless args.empty?
  end

  def [](k)
    if self.has_key?k
      super(k)
    else
      AutoHash.new(:update => self, :update_key => k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end
end

它可以解决问题当仅请求项值时意外创建丢失的哈希项时,例如a['key']


一些其他参考

  1. ruby 哈希自动生存(facets)
  2. http://trevoke.net/blog/2009/11/06 /auto-vivifying-hashes-in-ruby/
  3. http://www.eecs.harvard.edu/~cduan/technical/ruby/ycombinator.shtml

I am so used to work in PHP with multi-dimensional arrays, where I can assign and initialize a hash by

unset($a); // just to show that there is no variable $a
$a['settings']['system']['memory'] = '1 Gb';
$a['settings']['system']['disk space'] = '100 Gb';

Is there a way to do similar thing in Ruby? Or I need to initialize all dimensions first, and then to assign values. Is it possible to define an advanced Hash which will allow to do what I need? How would you do that?


Update

In addition to the solution proposed by Douglas (see below), I have found a thread on the subject, in which Brian Schröäer has proposed an extension for the Hash class:

class AutoHash < Hash
  def initialize(*args)
    super()
    @update, @update_index = args[0][:update], args[0][:update_key] unless args.empty?
  end

  def [](k)
    if self.has_key?k
      super(k)
    else
      AutoHash.new(:update => self, :update_key => k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end
end

It allows to solve the problem when a missing hash item is undesirably created when the item value was only requested, e.g. a['key'].


Some additional references

  1. ruby hash autovivification (facets)
  2. http://trevoke.net/blog/2009/11/06/auto-vivifying-hashes-in-ruby/
  3. http://www.eecs.harvard.edu/~cduan/technical/ruby/ycombinator.shtml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-09-14 19:32:34

试试这个:

def hash_with_default_hash
    Hash.new { |hash, key| hash[key] = hash_with_default_hash }
end

a = hash_with_default_hash

如果键不存在,则块的结果将用作默认值。在这种情况下,默认值也是一个散列,它使用散列作为其默认值。

Try this:

def hash_with_default_hash
    Hash.new { |hash, key| hash[key] = hash_with_default_hash }
end

a = hash_with_default_hash

If the key doesn't exist, then the result of the block will be used as the default value. In this case, the default value is also a hash which uses hashes as its default values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文