繁体   English   中英

是否有在自定义类中启用哈希样式查找的方法?

[英]Are there methods for enabling Hash-style look-ups in custom classes?

我很确定我可以在我的类中实现一些方法来使这样的查找成为可能:

foo = MyClass.new
foo["bar"] = 123

但他们是什么?

您可以随时定义自己的[][]=方法(谷歌:“运算符重载”),例如:

class MyClass
  def [](key)
    # ...
  end

  def []=(key, value)
    # ... 
  end
end

这里有些例子:

http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-operators-and-their-methods/

https://www.ruby-forum.com/topic/4067570

如何覆盖ruby中的[]括号?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM