Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Ruby 中是否有诸如 C# 中的索引器方法的概念?
是的,一个名为[]采用单个参数的方法:
[]
>> class Foo >> def [](idx) >> idx * 5 >> end >> end => nil >> ?> f = Foo.new => #<Foo:0x101098d80> >> f[8] => 40 >> f[1] => 5
如果需要在索引处设置[]=值,请将方法命名为。
[]=