在Ruby中使用新的运算符对自动生成哈希进行多重初始化 [英] Multiple initialization of auto-vivifying hashes using a new operator in Ruby

查看:138
本文介绍了在Ruby中使用新的运算符对自动生成哈希进行多重初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化几个自动生成哈希。到目前为止,我对 AutoHash 对象有一个额外的方法:

  class AutoHash<哈希
...
def few(n = 0)
Array.new(n){AutoHash.new}
end



允许我做以下操作

  a ,b,c = AutoHash.new.few 3 

然而,我觉得可以做以下可能通过定义一个新的操作符:=

  a:= b := c = AutoHash.new 

您能帮我实施吗?






我必须使用 superators

  require'superators'

class AutoHash<哈希
...
superator:=do | operand |
如果operand.kind_of? Hash
...
else
...
end
end






更新:现在我发现运营商需要在课堂外定义。是否有可能定义这样的对象克隆操作符?






Update2 更明确的方法定义少数,感谢Joshua






参考资料


  1. http://www.linux-mag.com/cache/7432/1.html

  2. Ruby是否支持像PHP这样的var引用?

  3. http://ruby.about.com/od/advancedruby/a/deepcopy.htm


解决方案

在哪里要求 a:= b:= c: = AutoHash.new.few 3 我想(不知道我理解你的愿望),你真的想要 a,b,c = Autohash.new.few 3


为什么很少有var当你只使用第一个参数时,这些参数是否可信?



我也发现你创建的返回值会令人困惑,也许试试吧。 c $ c> def few(n = 0)
Array.new(n){AutoHash.new}
end






除此之外,似乎少数应该是一个类方法。 a,b,c = AutoHash.few 3 如果您在课堂上定义了很少的话,这将起作用:

<$ p $ (n = 0)
Array.new(n){AutoHash.new}
结束






如果 a,b,c = AutoHash.few 3 不是你正在寻找的东西,而你真的想要实现你自己的操作符,然后检查 Hacking parse.y ,这是在RubyConf上给出的一个讨论您可以在观看演示文稿http://rubyconf2009.confreaks.com/19-nov-2009-17-15-hacking-parsey-tatsuhiro-ujihisa.html ,你可以在 http://www.slideshare.net/ujihisa/hacking-parsey-rubyconf-2009


I would like to initialize several auto-vivifying hashes by one-line expression. So far I came to an extra method for the AutoHash object:

class AutoHash < Hash
  ...
  def few(n=0)
    Array.new(n) { AutoHash.new }
  end

which allows me to do the following

a, b, c = AutoHash.new.few 3

However, I feel that one can make the following sentence possible by defining a new operator :=

a := b := c = AutoHash.new

Could you help me to implement this?


Do I have to use superators?

require 'superators'

class AutoHash < Hash
  ...
  superator ":=" do |operand|
    if operand.kind_of? Hash
      ...
    else
      ...
    end
  end


Update: Now I see that the operator needs to be defined outside the class. Is it possible to define such object clone operator?


Update2 More clear definition of method few, thanks to Joshua


References

  1. http://www.linux-mag.com/cache/7432/1.html
  2. Does Ruby support var references like PHP?
  3. http://ruby.about.com/od/advancedruby/a/deepcopy.htm

解决方案

Where you ask for a := b := c := AutoHash.new.few 3 I think (not sure I understand your desire) that you really want a,b,c=Autohash.new.few 3


Why does few take variable args, when you only ever use the first?

I also find your creation of the return value to be confusing, maybe try

def few(n=0) 
  Array.new(n) { AutoHash.new } 
end 


Beyond that, it seems like few should be a class method. a,b,c=AutoHash.few 3 which will work if you defined few on the class:

def AutoHash.few(n=0)
  Array.new(n) { AutoHash.new }
end


If a,b,c=AutoHash.few 3 isn't what you're looking for, and you really want to implement your own operator, then check out Hacking parse.y, which was a talk given at RubyConf 2009. You can watch the presentation at http://rubyconf2009.confreaks.com/19-nov-2009-17-15-hacking-parsey-tatsuhiro-ujihisa.html and you can see the slides at http://www.slideshare.net/ujihisa/hacking-parsey-rubyconf-2009

这篇关于在Ruby中使用新的运算符对自动生成哈希进行多重初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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