1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/abstract_controller
Viktar Basharymau 453cd7b617 Relpace =~ Regexp.new str with .include? str in AC::Base#_valid_action_name?
Because it is more natural way to test substring inclusion. Also, in
this particular case it is much faster.

In general, using `Regexp.new str` for such kind of things is dangerous.
The string must be escaped, unless you know what you're doing. Example:

    Regexp.new "\\" # HELLO WINDOWS
    # RegexpError: too short escape sequence: /\/

The right way to do this is escape the string

    Regexp.new Regexp.escape "\\"
    # => /\\/

Here is the benchmark showing how faster `include?` call is.

```
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('include?') { !"index".to_s.include? File::SEPARATOR }
  x.report('   !~   ') { "index" !~ Regexp.new(File::SEPARATOR) }
end

__END__
Calculating -------------------------------------
            include?     75754 i/100ms
               !~        21089 i/100ms
-------------------------------------------------
            include?  3172882.3 (±4.5%) i/s -   15832586 in   5.000659s
               !~      322918.8 (±8.6%) i/s -    1602764 in   4.999509s
```

Extra `.to_s` call is needed to handle the case when `action_name` is
`nil`. If it is omitted, some tests fail.
2014-06-19 18:39:58 +03:00
..
railties
asset_paths.rb 🔥 Rails asset id support 2012-10-15 09:47:16 -05:00
base.rb Relpace =~ Regexp.new str with .include? str in AC::Base#_valid_action_name? 2014-06-19 18:39:58 +03:00
callbacks.rb Partially revert deprecation of *_filter 2014-06-03 19:53:28 -03:00
collector.rb Improve a couple exception messages related to variants and mime types 2013-12-03 22:23:12 -02:00
helpers.rb move MissingHelperError out of the ClassMethods module. 2013-07-12 15:34:29 +02:00
logger.rb avoid empty api pages 2012-04-08 01:56:41 +05:30
rendering.rb Check if the request variable isn't nil when calling render_to_string 2014-02-20 20:53:47 +02:00
translation.rb change useless gsub to tr 2013-03-05 09:33:42 -05:00
url_for.rb fixed a typo [ci skip] 2014-05-17 00:29:09 +09:00