在rails app中为内置类添加方法

时间:2022-06-13 11:00:35

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

我想在rails应用程序中向Array类添加一个方法。我应该把这个方法放在哪里?

EDIT to be clearer, obviously I put it in a file somewhere, but how do I tell the rails app about where to find it?

编辑更清楚,显然我把它放在某个地方的文件中,但是如何告诉rails应用程序在哪里找到它?

3 个解决方案

#1


37  

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

一种方法是在lib / rails_extensions.rb上创建一个文件。然后,像这样添加您的扩展程序:

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:

然后在config / environment.rb中添加以下内容:

require 'rails_extensions'

Your mileage with subservient objects may vary.

您对辅助对象的里程可能会有所不同。

#2


6  

By default, when you call "require", Rails will look in (from the Rails edge source):

默认情况下,当您调用“require”时,Rails将查找(来自Rails边缘源):

app app/metal app/models app/controllers app/helpers app/services lib vendor

app app / metal app / models app / controllers app / helpers app / services lib vendor

For simplicity's sake, put the file in lib/, and require it by name in your config/environment.rb, or you can put it in config/initializers/array_extension.rb, and it'll be automatically loaded.

为简单起见,将文件放在lib /中,并在config / environment.rb中按名称输入,或者将其放在config / initializers / array_extension.rb中,它将自动加载。

Where I work, we've put all of our extensions to the core Ruby library into a plugin, and stored it in (Rails.root/)vendor/plugins/utilities/lib/core_ext, and then we require the individual extensions in the plugin's init.rb.

在我工作的地方,我们将所有扩展到核心Ruby库的插件,并将其存储在(Rails.root /)vendor / plugins / utilities / lib / core_ext中,然后我们需要在插件的init.rb.

Another way to skin this cat: if you say, want to store your core extensions in Rails.root/core_ext, then you can add that path as a load path in your configuration block in environment.rb:

另一种修饰这只猫的方法:如果你说,想要将你的核心扩展存储在Rails.root / core_ext中,那么你可以在environment.rb的配置块中将该路径添加为加载路径:

Rails::Initializer.run do |config|
  config.load_paths << 'core_ext'
end

Then you can call "require 'array_extension'" from anywhere, and it'll load.

然后你可以从任何地方调用“require'array_extension'”,它会加载。

#3


0  

Just put it in a new file, e.g. array_extended.rb

只需将其放入新文件中,例如array_extended.rb

class Array

  def my_new_method()
    ...
  end

end

After that you may include this file with require "array_extended.rb". Be sure you don't override already existing methods as this may break other functionality.

之后,您可以将此文件包含在require“array_extended.rb”中。请确保您不会覆盖现有方法,因为这可能会破坏其他功能。

#1


37  

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

一种方法是在lib / rails_extensions.rb上创建一个文件。然后,像这样添加您的扩展程序:

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:

然后在config / environment.rb中添加以下内容:

require 'rails_extensions'

Your mileage with subservient objects may vary.

您对辅助对象的里程可能会有所不同。

#2


6  

By default, when you call "require", Rails will look in (from the Rails edge source):

默认情况下,当您调用“require”时,Rails将查找(来自Rails边缘源):

app app/metal app/models app/controllers app/helpers app/services lib vendor

app app / metal app / models app / controllers app / helpers app / services lib vendor

For simplicity's sake, put the file in lib/, and require it by name in your config/environment.rb, or you can put it in config/initializers/array_extension.rb, and it'll be automatically loaded.

为简单起见,将文件放在lib /中,并在config / environment.rb中按名称输入,或者将其放在config / initializers / array_extension.rb中,它将自动加载。

Where I work, we've put all of our extensions to the core Ruby library into a plugin, and stored it in (Rails.root/)vendor/plugins/utilities/lib/core_ext, and then we require the individual extensions in the plugin's init.rb.

在我工作的地方,我们将所有扩展到核心Ruby库的插件,并将其存储在(Rails.root /)vendor / plugins / utilities / lib / core_ext中,然后我们需要在插件的init.rb.

Another way to skin this cat: if you say, want to store your core extensions in Rails.root/core_ext, then you can add that path as a load path in your configuration block in environment.rb:

另一种修饰这只猫的方法:如果你说,想要将你的核心扩展存储在Rails.root / core_ext中,那么你可以在environment.rb的配置块中将该路径添加为加载路径:

Rails::Initializer.run do |config|
  config.load_paths << 'core_ext'
end

Then you can call "require 'array_extension'" from anywhere, and it'll load.

然后你可以从任何地方调用“require'array_extension'”,它会加载。

#3


0  

Just put it in a new file, e.g. array_extended.rb

只需将其放入新文件中,例如array_extended.rb

class Array

  def my_new_method()
    ...
  end

end

After that you may include this file with require "array_extended.rb". Be sure you don't override already existing methods as this may break other functionality.

之后,您可以将此文件包含在require“array_extended.rb”中。请确保您不会覆盖现有方法,因为这可能会破坏其他功能。