aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
Commit message (Collapse)AuthorAgeFilesLines
* `String#strip_heredoc` preserves frozennessJeremy Daer2018-02-171-1/+3
| | | | | | | | | | | | | | | | | | ```ruby "foo".freeze.strip_heredoc.frozen? # => true ``` Fixes the case where frozen string literals would inadvertently become unfrozen: ```ruby foo = <<-MSG.strip_heredoc la la la MSG foo.frozen? # => false !?? ```
* Rails 6 requires Ruby 2.4.1+Jeremy Daer2018-02-176-46/+12
| | | | | | Skipping over 2.4.0 to sidestep the `"symbol_from_string".to_sym.dup` bug. References #32028
* Rails 6 requires Ruby 2.3+Jeremy Daer2018-02-171-17/+8
|
* PERF: symbolize ivar, to reduce dupesSam2018-02-151-1/+1
| | | | | | | | | | | | I noticed this in my memory profiler report. ``` 153 "@default_url_options" 152 /home/sam/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.4/lib/active_support/core_ext/class/attribute.rb:84 ``` 152 copies of the string `@default_url_options` are retained on the heap in Discourse post boot. Since this is just used for ivar lookups there is no need to use a string.
* Enable autocorrect for `Lint/EndAlignment` copKoichi ITO2018-01-181-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary This PR changes .rubocop.yml. Regarding the code using `if ... else ... end`, I think the coding style that Rails expects is as follows. ```ruby var = if cond a else b end ``` However, the current .rubocop.yml setting does not offense for the following code. ```ruby var = if cond a else b end ``` I think that the above code expects offense to be warned. Moreover, the layout by autocorrect is unnatural. ```ruby var = if cond a else b end ``` This PR adds a setting to .rubocop.yml to make an offense warning and autocorrect as expected by the coding style. And this change also fixes `case ... when ... end` together. Also this PR itself is an example that arranges the layout using `rubocop -a`. ### Other Information Autocorrect of `Lint/EndAlignment` cop is `false` by default. https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443 This PR changes this value to `true`. Also this PR has changed it together as it is necessary to enable `Layout/ElseAlignment` cop to make this behavior.
* Fix "the the " [ci skip]Ryuta Kamizono2018-01-101-1/+1
|
* Merge pull request #29685 from ayanko/fix-slow-name-error-missing-nameRyuta Kamizono2018-01-101-0/+5
|\ | | | | Fix performance issue with NameError#missing_name on ruby >= v2.3.0.
| * Fix performance issue with NameError#missing_name on ruby >= v2.3.0.Andriy Yanko2017-07-101-0/+5
| | | | | | | | | | Since ruby v2.3.0 `did_you_mean` gem shipped and ENABLED by default. It patches NameError#message with spell corrections which are SLOW.
* | Merge pull request #31049 from gwincr11/cg-blankRyuta Kamizono2018-01-041-1/+10
|\ \ | | | | | | Add support for multiple encodings in String.blank?
| * | Add support for multiple encodings in String.blank?Cory Gwin @gwincr112017-11-171-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Motivation: - When strings are encoded with `.encode("UTF-16LE")` `.blank?` throws an `Encoding::CompatibilityError` exception. - We tested multiple implementation to see what the fastest implementation was, rescueing the execption seems to be the fastest option we could find. Related Issues: - #28953 Changes: - Add a rescue to catch the exception. - Added a `Concurrent::Map` to store a cache of encoded regex objects for requested encoding types. - Use the new `Concurrent::Map` cache to return the correct regex for the string being checked.
* | | Remove undefined `track_deletion` callback [ci skip]Ryuta Kamizono2018-01-031-3/+0
| | |
* | | Clarify docs for delegate :allow_nil optionJay Hayes2017-12-151-5/+2
| | |
* | | Suppress `warning: BigDecimal.new is deprecated`Yasuo Honda2017-12-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `BigDecimal.new` has been deprecated in BigDecimal 1.3.3 which will be a default for Ruby 2.5. Refer https://github.com/ruby/bigdecimal/commit/533737338db915b00dc7168c3602e4b462b23503 * This commit has been made as follows: ``` cd rails git grep -l BigDecimal.new | grep -v guides/source/5_0_release_notes.md | grep -v activesupport/test/xml_mini_test.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g" ``` - `activesupport/test/xml_mini_test.rb` Editmanually to remove `.new` and `::` - guides/source/5_0_release_notes.md This is a Rails 5.0 release notes.
* | | [ci skip] Make Todo classes inherit ApplicationRecordYoshiyuki Hirano2017-12-071-5/+5
| | | | | | | | | | | | | | | Example codes that use `has_many` or `before_create` in `Module::Concerning` look like active record models. So I've made them inherit `ApplicationRecord`.
* | | Mention about Ruby 2.4 Unicode case mappings in `mb_chars` example [ci skip] ↵Ryuta Kamizono2017-11-291-1/+3
| | | | | | | | | | | | | | | (#31275) https://www.ruby-lang.org/en/news/2016/09/08/ruby-2-4-0-preview2-released/
* | | [ci skip] Correct output for UpcaseDixit Patel2017-11-291-1/+1
| | |
* | | Refactor Date/Time next_occurring and prev_occurringT.J. Schuck2017-11-281-4/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | These methods were originally added in https://github.com/rails/rails/pull/26600 This includes a couple of refactors to make these methods behave more similarly to other Date/Time extensions added by Active Support: 1. Use `advance` instead of `since` and `ago` to time-travel — this is particularly important to keep the returned instance’s class matching `self`. Before this change: today = Date.today # => Tue, 28 Nov 2017 today.class # => Date today.next_occurring(:wednesday) # => Wed, 29 Nov 2017 00:00:00 UTC +00:00 today.next_occurring(:wednesday).class # => ActiveSupport::TimeWithZone After this change, a Date (or Time, or DateTime) instance is properly returned (just like is shown in the new docs). This is generally how everything else in DateAndTime::Calculations works. 2. Move the tests from the DateTime tests to the DateAndTimeBehavior tests. The latter location is mixed in to the core_ext tests for _all_ of Date, Time, and DateTime to test the behavior across all of the classes. The previous location is for testing core_ext functionality added specifically just to DateTime. 3. Better docs!
* | | Update incorrect backtick usage in RDoc to teletypeT.J. Schuck2017-11-225-7/+7
| | | | | | | | | [ci skip]
* | | Allow `Range#include?` on TWZ rangesAndrew White2017-11-083-1/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In #11474 we prevented TWZ ranges being iterated over which matched Ruby's handling of Time ranges and as a consequence `include?` stopped working with both Time ranges and TWZ ranges. However in ruby/ruby@b061634 support was added for `include?` to use `cover?` for 'linear' objects. Since we have no way of making Ruby consider TWZ instances as 'linear' we have to override `Range#include?`. Fixes #30799.
* | | Merge pull request #30620 from ↵Andrew White2017-11-061-16/+24
|\ \ \ | |/ / |/| | | | | | | | bogdanvlviv/method_signature_prev-next-day-month-year_for_time Mirror the API of Ruby stdlib for #prev_day, #next_day, #prev_month, #next_month, #prev_year, #next_year
| * | Allows pass argument for `Time#prev_year` and `Time#next_year`.bogdanvlviv2017-10-241-5/+9
| | |
| * | Allows pass argument for `Time#prev_month` and `Time#next_month`bogdanvlviv2017-10-241-5/+9
| | |
| * | Allows pass argument for `Time#prev_day` and `Time#next_day`bogdanvlviv2017-10-241-6/+6
| | |
* | | Enable `Style/RedundantReturn` rubocop rule, and fixed a couple moreRyuta Kamizono2017-11-011-7/+7
| | | | | | | | | | | | Follow up of #31004.
* | | Simplify API documentation of methods that return a Durationbogdanvlviv2017-10-242-33/+10
|/ / | | | | | | Related to #30972
* | Make clear that Time core extensions are split between Numeric and IntegerJoão Fernandes2017-10-242-6/+10
| | | | | | | | | | | | | | The documentation wrongly suggests that Time extensions to Numeric include methods months and years, when these belong to Integer. Update both classes and guides.
* | Merge pull request #30961 from q-centrix/performance-improvement-acts-likeRafael França2017-10-231-1/+10
|\ \ | | | | | | Performance improvements for acts_like? method
| * | Performance improvements for acts_like? method.Dillon Welch2017-10-231-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | activesupport/lib/active_support/core_ext/object/acts_like.rb acts_like? Add a case statement to use direct symbols instead of string interpolation for the three scenarios I found in the Rails codebase: time, date, and string. For time/date/string, this change prevents two string allocations for each time the method is called and speeds up the method by ~2.7x. For other arguments, there is no memory difference and performance difference is within margin of error. begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "rails", github: "rails/rails" gem "arel", github: "rails/arel" gem "benchmark-ips" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end class Object def fast_acts_like?(duck) case duck when :time respond_to? :acts_like_time? when :date respond_to? :acts_like_date? when :string respond_to? :acts_like_string? else respond_to? :"acts_like_#{duck}?" end end end puts puts " acts_like? ".center(80, '=') puts obj = ''.freeze %i(time date string super_hacka).each do |type| puts " #{type} ".center(80, '=') puts " Memory Usage ".center(80, "=") puts puts "value.acts_like?" puts allocate_count { 1000.times { obj.acts_like?(type) } } puts "value.fast_acts_like?" puts allocate_count { 1000.times { obj.fast_acts_like?(type) } } puts puts " Benchmark.ips ".center(80, "=") puts Benchmark.ips do |x| x.report("acts_like?") { obj.acts_like?(type) } x.report("fast_acts_like?") { obj.fast_acts_like?(type) } x.compare! end end ================================== acts_like? ================================== ===================================== time ===================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-1983, :T_STRING=>2052, :T_IMEMO=>1} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 104.281k i/100ms fast_acts_like? 155.523k i/100ms Calculating ------------------------------------- acts_like? 1.688M (±10.7%) i/s - 8.342M in 5.003804s fast_acts_like? 4.596M (±12.1%) i/s - 22.551M in 5.000124s Comparison: fast_acts_like?: 4596162.4 i/s acts_like?: 1688163.8 i/s - 2.72x slower ===================================== date ===================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2001, :T_STRING=>2000} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 85.372k i/100ms fast_acts_like? 166.097k i/100ms Calculating ------------------------------------- acts_like? 1.720M (± 8.3%) i/s - 8.537M in 5.001003s fast_acts_like? 4.695M (±10.1%) i/s - 23.254M in 5.010734s Comparison: fast_acts_like?: 4695493.1 i/s acts_like?: 1719637.9 i/s - 2.73x slower ==================================== string ==================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2001, :T_STRING=>2000} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 100.221k i/100ms fast_acts_like? 182.841k i/100ms Calculating ------------------------------------- acts_like? 1.706M (± 7.3%) i/s - 8.519M in 5.022331s fast_acts_like? 3.968M (±22.8%) i/s - 18.650M in 5.006762s Comparison: fast_acts_like?: 3967972.9 i/s acts_like?: 1705773.7 i/s - 2.33x slower ================================= super_hacka ================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2004, :T_STRING=>2002, :T_SYMBOL=>1} value.fast_acts_like? {:FREE=>-2003, :T_STRING=>2001, :T_SYMBOL=>1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 100.344k i/100ms fast_acts_like? 101.690k i/100ms Calculating ------------------------------------- acts_like? 1.617M (± 7.5%) i/s - 8.128M in 5.055285s fast_acts_like? 1.534M (±10.1%) i/s - 7.627M in 5.031052s Comparison: acts_like?: 1617390.7 i/s fast_acts_like?: 1533897.3 i/s - same-ish: difference falls within error
* | | Fix #to_json for unreadable IO objects, fixes #26132Paul Kuruvilla2017-10-231-0/+6
| | |
* | | [Active Support] require_relative => requireAkira Matsuda2017-10-2159-188/+188
| | | | | | | | | | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* | | Let Hash#slice return a HashAkira Matsuda2017-10-211-1/+1
| | | | | | | | | | | | | | | | | | | | | In order to keep this method compatible with the Ruby 2.5 version of Hash#slice. This bahavior is actually slightly incompatibile with previous versions of Active Support but it might not cause a real problem, since HWIA, the biggest use case of Hash subclassing here, already overrides `slice` to return another HWIA.
* | | Move HWIA specific logic for slice and slice! to HWIA classAkira Matsuda2017-10-211-2/+0
| | |
* | | Hash#slice is in Ruby 2.5+Akira Matsuda2017-10-211-1/+1
| | | | | | | | | | | | since r60229
* | | Remove obsolete documentation [ci skip]Max Felsher2017-10-191-3/+0
|/ / | | | | | | | | Instructions to use `h` or `html_escape` in ERB templates were added to `actionpack/lib/action_view/template_handlers/erb.rb` in a1b0349 (Rails 2.1), but ERB has automatically escaped values since Rails 3.
* | Fix `to_s(:db)` for range comprising of alphabets.Aditya Kapoor2017-10-161-1/+7
| |
* | Fix formatting of `Time.use_zone` [ci skip]yuuji.yaginuma2017-10-041-4/+4
| |
* | Remove redundant require_relative "module/anonymous" and "module/reachable"bogdanvlviv2017-09-171-3/+0
| |
* | Deprecate `Module#reachable?` methodbogdanvlviv2017-09-171-0/+1
| |
* | Hash#transform_keys is in Ruby 2.5+Akira Matsuda2017-09-011-2/+2
| | | | | | | | since r59328
* | Self-alias doesn't suppress the warning on Ruby 2.2Matthew Draper2017-09-011-8/+17
| |
* | Clarify intentions around method redefinitionsMatthew Draper2017-09-019-54/+69
| | | | | | | | | | | | | | | | | | Don't use remove_method or remove_possible_method just before a new definition: at best the purpose is unclear, and at worst it creates a race condition. Instead, prefer redefine_method when practical, and silence_redefinition_of_method otherwise.
* | Update links to use https instead of http [ci skip]Yoshiyuki Hirano2017-08-221-1/+1
| |
* | faster implementation of Hash#deep_mergeM. Simon Borg2017-08-161-12/+6
| | | | | | | | | | | | | | | | | | | | | | | | add missing newline call #deep_merge instead of #dup.deep_merge! make variable and parameter naming more consistent change `_key` to `key` faster implementation of Hash#deep_merge
* | Test for the new exception of delegate_missing_to (#30191)Anton Khamets2017-08-121-3/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Add test for the new exception of delegate_missing_to * Add a changelog entry * Only check for nil if NoMethodError was raised * Make method private * Have to pass both target name and value * Inline the re-raise [Rafael Mendonça França + Anton Khamets]
* | Extend image_tag to accept ActiveStorage Attachments and Variants (#30084)Anton Khamets2017-08-071-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Extend image_tag to accept ActiveStorage's Attachments and Variants * Flip resolve_image_source around * Add tests for the new use-cases of image_tag * Remove the higher-level test * Update image_tag documentation * Add error states into the test suite * Re-raise polymorhic_url's NoMethodError as ArgumentError * delegate_missing_to will raise DelegationError instead of NoMethodError
* | Remove unused `LoadError::REGEXPS` constantyuuji.yaginuma2017-08-041-7/+0
| | | | | | | | since 4ad1a52, `LoadError::REGEXPS` is no longer needed.
* | Update String#camelize to provide feedback when wrong option is passedRicardo Díaz2017-08-021-0/+2
| | | | | | | | | | | | | | | | | | String#camelize was returning nil without any feedback when an invalid option was passed as parameter. This update makes the method to raises an ArgumentError when the option passed is invalid, similar to what Ruby does for String#downcase (and others) in 2.4.1. https://ruby-doc.org/core-2.4.1/String.html#method-i-downcase
* | Merge pull request #29758 from glaucocustodio/patch-1 [ci skip]Sean Griffin2017-07-171-0/+10
|\ \ | | | | | | Add documentation for class_attribute default option
| * | Add documentation for class_attribute optionsGlauco Custódio2017-07-171-0/+10
| | |
* | | Merge branch 'master' into make-reverse-merge-bang-order-consistentSean Griffin2017-07-17114-311/+633
|\ \ \