From: dblack / rubypal.com [mailto:dblack / rubypal.com] On Behalf Of
dblack / wobblini.net
Sent: Thursday, November 23, 2006 3:31 AM
>On Thu, 23 Nov 2006, Victor "Zverok" Shepelev wrote:
>
>> Hi all.
>>
>> I have some huge class with aspects of functionality spreaded in several
>> source files.
>>
>> And in some of those aspects there is the same picture:
>> ---
>> class MyClass
>>  def push_something(obj)
>>    @something_list ||= []
>>    @something_list << obj
>>  end
>>
>>  def use_something(i)
>>    (@something_list ||= [])[i]
>>  end
>> end
>> ---
>>
>> Then I note (through profiler) various push_XXX spend too much time in
>> checking is @XXX_list initialized. Then I change it:
>>
>> ---
>> class MyClass
>>  alias :initialize_without_something :initialize
>>
>>  def initialize(*arg)
>>    initialize_without_something(*arg)
>>    @something_list = []
>>  end
>>
>>  def push_something(obj)
>>    @something_list << obj
>>  end
>>
>>  def use_something(i)
>>    @something_list[i]
>>  end
>> end
>> ---
>>
>> Now push_XXX and use_XXX work cleaner and faster, but all those
>initialize
>> aliases (now I have 5 or 6) don't seem to be very elegant.
>>
>> Is there better solution?
>>
>> (to be clear, all those push_XXX are NOT similar - some of them push
>object
>> to hashes, others to arrays, params count differ and so on - so, they
>can't
>> be generated at once through metaprogramming)
>
>In your second example, what purpose does initialize_without_something
>serve?  I'm wondering why you don't just do:
>
>   def initialize
>     @something_list = []
>   end
>
>and then the other methods.

Because it would hide initialization code defined in other files for the
same class.

class A
 def initialize; p 1 end
end

class A
 def initialize; p 2 end
end

class A
 def initialize; p 3 end
end

A.new # => 3

V.