aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
Commit message (Collapse)AuthorAgeFilesLines
...
| * | 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
|\ \ \
| * | | [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-11114-0/+114
| |/ /
| * | Merge pull request #29728 from kirs/frozen-activesupportMatthew Draper2017-07-09114-0/+114
| |\ \ | | | | | | | | Use frozen-string-literal in ActiveSupport
| | * | Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-09114-0/+114
| | |/
| * / Use `map` in `delegate` so that actual prefixed method names are returned, ↵Krzysztof Zych2017-07-051-1/+1
| |/ | | | | | | if using prefix version.
| * [Active Support] require => require_relativeAkira Matsuda2017-07-0159-187/+187
| |
| * Use mattr_accessor default: option throughout the projectGenadi Samokovarov2017-06-031-1/+1
| |
| * Implement mattr_acessor :default optionGenadi Samokovarov2017-06-031-24/+19
| |
| * Add next occur and previous occurred day of week API (#26600)Shota Iguchi2017-05-301-0/+16
| |
| * Add option for class_attribute default (#29270)David Heinemeier Hansson2017-05-291-2/+11
| | | | | | | | | | | | | | | | | | | | | | | | * Allow a default value to be declared for class_attribute * Convert to using class_attribute default rather than explicit setter * Removed instance_accessor option by mistake * False is a valid default value * Documentation
| * Assorted delegate_missing_to doc fixesT.J. Schuck2017-05-041-22/+17
| | | | | | | | | | | | | | | | | | | | | | | | * Fix rdoc code formatting — `tt`, not backticks * Fix/simplify sentence grammar — should at least just be “and the like”, not “likes”, but this is just general tightening up. * Add note that delegated methods must be public. Tested here: https://github.com/rails/rails/blob/7ff5ccae94ce2aff76b5f8a31a28e305a047d642/activesupport/test/core_ext/module_test.rb#L359-L365 * Simplify example code for delegate_missing_to. The example had complexity that wasn’t necessary for demonstrating `delegate_missing_to`. This gets rid of a bunch of cruft so the example is more obvious about what’s going on regarding the feature itself. [ci skip]
| * Add missing require for `remove_possible_method`Eugene Kenny2017-04-281-0/+1
| | | | | | | | | | | | https://github.com/rails/rails/commit/505537082849d912e8e29819655b80a573e93c0c added a call to `remove_possible_method`, but didn't require the file that defines it.
| * Add commaJon Moss2017-04-201-1/+1
| | | | | | | | [ci skip]
| * Fix Enumerable#sum redefined warningFumiaki MATSUSHIMA2017-04-181-23/+45
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If we require 'active_support/core_ext/enumerable' on Ruby 2.4, we'll see following warning because `Enumerable#sum` and `Array#sum` are added in Ruby 2.4. ``` rails/rails/activesupport/lib/active_support/core_ext/enumerable.rb:20: warning: method redefined; discarding old sum ``` The minimal way to fix the warning is `alias sum sum`. ``` $ ruby -v ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux] $ ruby -w -e "def a; end; def a; end" -e:1: warning: method redefined; discarding old a -e:1: warning: previous definition of a was here $ ruby -w -e "def a; end; alias a a; def a; end" ``` But this behavior is not intended. (@amatsuda was told by @ko1) So we should use `alias` as a meaningful way. Ruby 2.4's `sum` (`orig_sum`) assumes an `identity` is `0` when we omit `identity` so we can delegate to `orig_sum` with explicit `identity` only. In a strict sense, we can detect `identity` by check instance's class but we don't care at this time about that because calling `Enumerable#sum` is rare. In many cases, we will call `Array#sum`.
| * Add missing periodsJon Moss2017-04-171-2/+2
| | | | | | | | [ci skip]
| * Add (more) documentation to to_timeKen Mayer2017-04-171-0/+3
| | | | | | | | | | There's a difference in the results between `Date#to_time(:local)` and `Date#in_time_zone` but it is subtle and can confuse users (like me :-).
| * Merge pull request #28638 from bogdanvlviv/prepend_and_append_in_rubyKasper Timm Hansen2017-04-151-2/+2
| |\ | | | | | | Prevent aliases Array#append and Array#prepend
| | * Prevent aliases Array#append and Array#prependbogdanvlviv2017-04-021-2/+2
| | | | | | | | | | | | https://github.com/ruby/ruby/commit/f57d515d69b7a35477b9ba5d08fe117df1f1e275
| * | Add additional options to time `change` methodsAndrew White2017-04-141-15/+21
| | | | | | | | | | | | | | | | | | | | | Support `:offset` in `Time#change` and `:zone` or `:offset` in `ActiveSupport::TimeWithZone#change`. Fixes #28723.
| * | delegate_missing_to should fall back to superMatthew Draper2017-04-091-1/+1
| | |
| * | delegate_to_missing doesn't delegate private methodsMatthew Draper2017-04-091-1/+4
| |/ | | | | | | So we shouldn't claim they're there, even when asked explicitly.