Unfortunately it will never work the way you want it to.
Place this code into an initializer file (under the initializers
folder) or place it under the "lib" folder and do an explicit require.
Also, if all you want to do is to add a method to a class defined
elsewhere, do it like this:
Jason::MyClass.class_eval do
def another_method
# do whatever you want to do here.
end
end
-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
On Wed, Apr 8, 2009 at 5:27 PM, Jason Fox
<[email protected]> wrote:
>
> I have class in a plugin that I want to crack open and add some
> functionality to for a particular application. So, I created a file by
> the same name as the class in my app/models folder and added some
> methods to the class, but, I can't seem to get Rails (2.2.2) to pick-up
> the extended definition.
>
> I did find while trying to debug the problem that if I paste the
> extended definition into, say, environment.rb it works fine. Also, if I
> require_dependency "#{RAILS_ROOT}/app/models/my_class.rb" in the
> plugin's version of the class it also works fine (obviously not
> desirable).
>
> All of this leads me to believe that my problem has something to do with
> the class loader, but, I can't figure it out. I've laid out an example
> below that illustrates my problem. Any help would be much appreciated.
>
> Thanks,
> Jason
>
>
> # vendor/plugins/my_plugin/init.rb
> require 'my_plugin'
>
> # vendor/plugins/my_plugin/lib/my_plugin.rb
> module Jason
> class MyClass
> def a_method
> # ...
> end
> end
> end
>
> # app/models/my_class.rb
> module Jason
> class MyClass
> def another_method
> # ...
> end
> end
> end
>
> $ script/console
>>> Jason::MyClass.new.another_method
> NoMethodError: undefined method `another_method' for
> #<Jason::MyClass:0x408b444>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" 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-talk?hl=en
-~----------~----~----~----~------~----~------~--~---