B
Brian Candler
a = Array.new { [] }
p a #=> []
p a[3] #=> nil
Just checking: you can't have an Array whose elements spring into life when
needed (as you can with a Hash)?
Maybe a good thing - you might not want a single access to a[10000] to
create that many objects - but it could be useful on occasions.
No need to post other solutions - I can subclass/delegate such that [], []=
are overridden appropriately, although usually I just end up writing
a[x] ||= []
a[x][y] = 'whatever'
Regards,
Brian.
p a #=> []
p a[3] #=> nil
Just checking: you can't have an Array whose elements spring into life when
needed (as you can with a Hash)?
Maybe a good thing - you might not want a single access to a[10000] to
create that many objects - but it could be useful on occasions.
No need to post other solutions - I can subclass/delegate such that [], []=
are overridden appropriately, although usually I just end up writing
a[x] ||= []
a[x][y] = 'whatever'
Regards,
Brian.