I would personally do it as something like :

  def initialize_with_defaults(attributes = nil, &block)
    initialize_without_defaults(attributes) do
      self.some_attribute = 'whatever' # Setting default value
      yield self if block_given?
    end
  end
  alias_method_chain :initialize, :defaults

Looks like it's working quite well.

Thanks,
Pratik

On Jul 24, 8:28 am, Ben Munat <[EMAIL PROTECTED]> wrote:
> Piers Cawley wrote:
> > On 24/07/07, Ben Munat <[EMAIL PROTECTED]> wrote:
> >> Anyway, I think I'll definitely check out Jonathan's plugin on my next 
> >> app... looks all nice and
> >> "DSL-ish". :-)
>
> > The problem with the plugin approach here is that it's monkeypatching
> > a pretty fundamental method, and undocumented, method. And that makes
> > it fragile; if the implementation of initialize ever changes (and
> > there's no reason that it shouldn't), the plugin breaks.
>
> > Still, if it's the only game in town...
>
> Hmm, yeah that's a good point... always the risk with monkeypatching.
>
> So, Piers, you're the one that started this whole thing... if it really is 
> safe to do:
>
> class MyModel < ActiveRecord::Base
>    def initialize(attrs = {}, &block)
>      super
>      # all my initialization stuff here
>    end
> end
>
> and this is documented in the AR::Base docs, then this does kind of seem like 
> enough. I mean, it's
> what I did all the time in my java life.
>
> I'd just swear that someone told me the holy beasts of doom would swarm down 
> upon me if I ever tried
> to override AR::Base#initialize.
>
> Ben


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to