adding a method to built-in class in rails app

后端 未结 3 768
死守一世寂寞
死守一世寂寞 2020-12-24 07:51

I want to add a method to the Array class in a rails app. Where should I put this method?

EDIT to be clearer, obviously I put it in a file somewhere, but how do I t

3条回答
  •  渐次进展
    2020-12-24 08:06

    One way to do this is to create a file at lib/rails_extensions.rb. Then, add your extensions like so:

    class Array
      def bring_me_food
        # ...
      end
    
      def make_tea
        # ...
      end
    end
    
    class Hash
      def rub_my_shoulders
        # ...
      end
    end
    

    Then in config/environment.rb, add this:

    require 'rails_extensions'
    

    Your mileage with subservient objects may vary.

提交回复
热议问题