String class - where does this happen in Rails?

I try the following tutorial: http://henrik.nyh.se/2007/03/ruby-wordwrap-method

It has a block that needs to be added somewhere that I think outside of the helper:

class String # Replace the second of three capture groups with the given block. def midsub(regexp, &block) self.gsub(regexp) { $1 + yield($2) + $3 } end end 

Where should this be located in the rails 3 app? Thanks

UPDATE

Added by /lib/midsub.rb

 # Needed for html_format try 3 class String # Replace the second of three capture groups with the given block. def midsub(regexp, &block) self.gsub(regexp) { $1 + yield($2) + $3 } end end 

application.rb

 config.autoload_paths += %W(#{config.root}/lib) 
+4
source share
3 answers

you can add this code inside the file, say 'my_string.rb' in the / config / initializers folder

+3
source

These things are usually in the RAILS_ROOT/lib directory. Everything in this directory is automatically loaded when Rails is loaded, although NB, unlike the files in the app directory, these libraries are not re-evaluated for each request, even in development mode, so if you are going to test the changes on the fly, you It's best to test your libraries yourself and wait for them to be polished before putting the final version into your lib .

The Rails Guides describe the actual file upload order, although you are completely free to ignore the order and “jump” gun, require with the file earlier.

0
source

You can put this in lib/ . Then edit config/application.rb so lib is loaded automatically:

 config.autoload_paths += %W(#{config.root}/lib) 
0
source

Source: https://habr.com/ru/post/1340665/


All Articles