Hi --

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.


David

-- 
                   David A. Black | dblack / wobblini.net
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org