| Previous | Reductive Labs, LLC | Next |
Why You'd Use the Indexing MethodsSure, you could subclass Hash, but it's not always appropriate: class Yayness attr_accessor :name @instances = {} def Yayness.[](instname) @instances[instname] end def Yayness.[]=(instname, object) @instances[instname] = object end def initialize(myname) self.class[myname] = self @name = myname end end first = Yayness.new(:first) second = Yayness.new(:second) puts Yayness[:first].name
Produces:
|
| Previous | Luke Kanies luke@reductivelabs.com | Index | Next |