"stephan.zimmer" <stephan.zimmer / googlemail.com> writes: > I would like to represent certain data by a list; to this end I let > class SomeData inherit from Array: > > class SomeData < Array > def get_field > self[2] > end > end > > (I want to keep it simple, therefore, the example might look silly.) > Since the "get_field" method is not universal I don't want to reopen > class "Array". If I write > > SomeData.new([1,2,3]).get_field > > everything is fine. If I, however, try to do > > [1,2,3].get_field > > I get an exception "NoMethodError", which, of course, is not > surprising. > > My question is: is there a way around this, that is, to simply write > [1,2,3] to denote a constant of type "SomeData" instead of always > writing "SomeData.new([1,2,3])" (without reopening Array)? Do I understand you correctly that you want a function that works on instances of SomeData and Array similarly without re-opening Array? Rather than: SomeData.new([1,2,3]).get_field [1,2,3].get_field # doesn't work How about the following? get_field(SomeData.new([1,2,3])) get_field([1,2,3]) In other words, maybe you want a function that knows how to "get_field" given *any* indexable sequence. 1 class SomeData < Array 2 def self.get_field list 3 list[2] 4 end 5 end 6 7 x = SomeData.new([1,2,3]) 8 y = [4,5,6] 9 10 puts SomeData.get_field(x) 11 puts SomeData.get_field(y) Ruby doesn't support generic functions, so there's a limit to this approach, but depending on how simple your real class is, it might be a possibility. > Thanks a lot, > > Stephan -- Brian Adkins http://www.lojic.com/ http://lojic.com/blog/