gpt4 book ai didi

ruby - 是否有在自定义类中启用散列式查找的方法?

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:38 25 4
gpt4 key购买 nike

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

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

how to override [] brackets in ruby?

关于ruby - 是否有在自定义类中启用散列式查找的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19080808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com