aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
Commit message (Collapse)AuthorAgeFilesLines
* Performance improvement for `String#to`Ryuta Kamizono2019-07-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby class String def to1(position) position = [position + length, -1].max if position < 0 self[0, position + 1] end def to2(position) position += size if position < 0 self[0, position + 1] || +"" end end Benchmark.ips do |x| x.report("'foo'.to(1)") { 'foo'.to(1) } x.report("'foo'.to1(1)") { 'foo'.to1(1) } x.report("'foo'.to2(1)") { 'foo'.to2(1) } x.report("'foo'.to(-1)") { 'foo'.to(-1) } x.report("'foo'.to1(-1)") { 'foo'.to1(-1) } x.report("'foo'.to2(-1)") { 'foo'.to2(-1) } x.report("'foo'.to(-10)") { 'foo'.to(-10) } x.report("'foo'.to1(-10)") { 'foo'.to1(-10) } x.report("'foo'.to2(-10)") { 'foo'.to2(-10) } end ``` Result: ``` Warming up -------------------------------------- 'foo'.to(1) 199.859k i/100ms 'foo'.to1(1) 220.293k i/100ms 'foo'.to2(1) 221.522k i/100ms 'foo'.to(-1) 205.032k i/100ms 'foo'.to1(-1) 195.837k i/100ms 'foo'.to2(-1) 214.975k i/100ms 'foo'.to(-10) 214.331k i/100ms 'foo'.to1(-10) 182.666k i/100ms 'foo'.to2(-10) 224.696k i/100ms Calculating ------------------------------------- 'foo'.to(1) 4.685M (± 4.2%) i/s - 23.583M in 5.042568s 'foo'.to1(1) 5.233M (± 5.8%) i/s - 26.215M in 5.026778s 'foo'.to2(1) 5.180M (± 5.7%) i/s - 25.918M in 5.020735s 'foo'.to(-1) 4.253M (± 7.0%) i/s - 21.323M in 5.043133s 'foo'.to1(-1) 4.438M (±11.2%) i/s - 21.934M in 5.025751s 'foo'.to2(-1) 4.716M (± 9.8%) i/s - 23.432M in 5.028088s 'foo'.to(-10) 4.678M (± 9.5%) i/s - 23.148M in 5.007379s 'foo'.to1(-10) 4.428M (± 5.1%) i/s - 22.103M in 5.005155s 'foo'.to2(-10) 5.243M (± 4.6%) i/s - 26.289M in 5.024695s ```
* Merge pull request #36185 from jonathanhefner/optimize-string-first-and-lastRafael França2019-07-281-22/+2
|\ | | | | Improve String#first and #last performance
| * Improve String#first and #last performanceJonathan Hefner2019-05-051-22/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Removes unnecessary conditional and method call for significant performance improvement. As a side effect, this fixes an unexpected behavior where passing a limit of 0 would return a frozen string. This also implements the Rails 6.1 intended behavior with regards to negative limits, and removes the previous deprecation warnings. String#first Comparison: new: 3056515.0 i/s old: 1943310.2 i/s - 1.57x slower String#last Comparison: new: 2691919.0 i/s old: 1924256.6 i/s - 1.40x slower (Note: "old" benchmarks have deprecation warnings commented out, for a more fair comparison.)
* | Use match? where we don't need MatchDataAkira Matsuda2019-07-271-1/+1
| | | | | | | | We're already running Performance/RegexpMatch cop, but it seems like the cop is not always =~ justice
* | Remove tough to grasp -1 + 1 = 0 from String#toKasper Timm Hansen2019-07-261-2/+2
| | | | | | | | | | | | | | | | | | | | | | In case a negative position is provided that exceeds the size of the string, we're relying on -1 returned from max to get 0 length by + 1 and let [] with a 0 length returning "" for us. E.g. "hello".to(-7), where -7 + 5 size = -2. That's lower than -1, so we use -1 instead and + 1 would turn it into 0. Instead allow outer bounds access and always return "".
* | Merge pull request #36180 from jonathanhefner/optimize-string-fromRafael França2019-07-251-2/+3
|\ \ | | | | | | Avoid extra allocation in String#from and #to
| * | Avoid extra allocation in String#from and #toJonathan Hefner2019-05-051-2/+3
| |/ | | | | | | | | | | | | | | | | | | | | | | Removes unnecessary Range object allocations for a significant speed-up. String#from Comparison: new: 3378594.0 i/s old: 2380129.8 i/s - 1.42x slower String#to Comparison: new: 2866175.7 i/s old: 2304406.4 i/s - 1.24x slower
* | Merge pull request #36412 from robotdana/compact_blankRafael Mendonça França2019-07-251-0/+46
|\ \ | | | | | | | | | Add compact_blank shortcut for reject(&:blank?)
| * | Add compact_blank shortcut for reject(&:blank?)Dana Sherson2019-06-051-0/+46
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I frequently find myself having to .compact but for blank. which means on an array reject(&:blank?) (this is fine), or, on a hash `.reject { |_k, v| v.blank? }` which is slightly more frustrating and i usually write it as .reject(&:blank?) first and am confused when it's trying to check if the keys are blank. I've added the analagous .compact_blank! where there's a reject! to build on (there's also a reject! in Set, but there's no other core_ext touching Set so i've left that alone)
* | | Omit marshal_dump & _dump from delegate_missing_toAaron Lipman2019-07-171-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Exclude missing marshal_dump and _dump methods from being delegated to an object's delegation target via the delegate_missing_to extension. This avoids unintentionally adding instance variables to an object during marshallization, should the delegation target be a method which would otherwise add them. In current versions of Ruby, a bug exists in the way objects are marshalled, allowing for instance variables to be added or removed during marshallization (see https://bugs.ruby-lang.org/issues/15968). This results in a corrupted serialized byte stream, causing an object's instance variables to "leak" into subsequent serialized objects during demarshallization. In Rails, this behavior may be triggered when marshalling an object that uses the delegate_missing_to extension, if the delegation target is a method which adds or removes instance variables to an object being marshalled - when calling Marshal.dump(object), Ruby's built in behavior will check whether the object responds to :marshal_dump or :_dump, which in turn triggers the delegation target method in the responds_to_missing? function defined in activesupport/lib/active_support/core_ext/module/delegation.rb While future versions of Ruby will resolve this bug by raising a RuntimeError, the underlying cause of this error may not be readily apparent when encountered by Rails developers. By excluding marshal_dump and _dump from being delegated to an object's target, this commit eliminates a potential cause of unexpected behavior and/or RuntimeErrors. Fixes #36522
* | | Implement UnboundMethod#duplicable?Jean Boussier2019-07-121-1/+10
| | |
* | | Remove dead code in duplicable.rbJean Boussier2019-07-111-120/+1
| | |
* | | Indentation >>Akira Matsuda2019-06-221-6/+6
| | |
* | | Delete `DateAndTime` method definition in rails that is compatible with ruby ↵soartec-lab2019-06-162-30/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | definition Tests are also only on the `Time` class Update doc forgetting to erase when moved Update guide `Date` class to `Time` class and defined file Update guide correction omission
* | | Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-134-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* | | Add missing file to require digest/uuid on active_support core extensionsLucas Arantes2019-06-121-0/+3
|/ /
* | Change commentsEdu Depetris2019-05-251-2/+2
| |
* | Address 639d7be. Readd changelog line; remove needless explicit return.Kasper Timm Hansen2019-05-241-1/+1
| |
* | Add :allow_nil option to delegate_missing_to; use in ActiveStorageMatt Tanous2019-05-231-3/+8
|/ | | | attachment
* Improve error message of ActiveSupport delegateokuramasafumi2019-04-291-1/+1
| | | | | | | | | ActiveSupport `delegate` has `to` option, but it's not a option hash anymore and now it's a keyword argument. When `to` argument is not given, it raises an ArgumentError but the message suggests supplying "options hash", which is now wrong. Now it's fixed to provide correct suggestion to supply a keyword argument.
* Frozen truncate (#36109)Jordan Thomas2019-04-261-1/+1
| | | | | | | | | | | | | | * Add test asserting truncate returns unfrozen string * Ensure strings returned from truncate are not frozen This fixes an issue where strings too short to be truncated were returned unfrozen, where as long-enough strings were returned frozen. Now retuned strings will not be frozen whether or not the string returned was shortened. * Update changelog w/ new truncate behavior description [Jordan Thomas + Rafael Mendonça França]
* Preserve html_safe? status on ActiveSupport::SafeBuffer#*r7kamura2019-04-191-0/+6
|
* Merge pull request #35771 from timoschilling/hash-speed-improvementsRafael França2019-04-021-1/+1
|\ | | | | Hash / HashWithIndifferentAccess speed improvements
| * Speed improvement for Hash#exceptTimo Schilling2019-03-291-1/+1
| |
* | Tweaks CHANGELOGs and docs [ci skip]Ryuta Kamizono2019-03-311-4/+4
|/ | | | | | | * add leading `#` before `=>` since hash rocket is valid Ruby code * add backticks * remove trailing spaces * and more
* Merge pull request #34405 from shugo/safe_buffer_backref_fixMatthew Draper2019-03-281-2/+37
|\ | | | | sub, sub!, gsub, and gsub! should set back references
| * Eliminate a thread local variable as suggested by nobuShugo Maeda2019-02-141-8/+1
| |
| * Remove trailing spaceShugo Maeda2018-11-081-1/+1
| |
| * Add a commented code example of what will be producedShugo Maeda2018-11-081-22/+22
| |
| * sub, sub!, gsub, and gsub! should set back referencesShugo Maeda2018-11-081-2/+44
| |
* | Fix bug in Range comparisons when comparing to excluded-end RangeOwen Stephens2019-03-281-12/+21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ```ruby (1..10).cover?(1...11) => false ``` After: ```ruby (1..10).cover?(1...11) => true ``` See https://git.io/fjTtz for the commit against Ruby core that added support for Range arguments, with similar handling of this case.
* | Fix Time#advance to work with dates before 1001-03-07Andrew White2019-03-181-2/+1
| | | | | | | | | | | | | | | | | | In #10634 the behavior of Time#advance was changed to maintain a proleptic gregorian calendar for dates before calendar reform. However it didn't full address dates a long time before calendar reform and they gradually drift away from the proleptic calendar the further you go back in time. Fix this by always converting the date to gregorian before calling advance which sets the reform date to -infinity.
* | Update docs for 'parameterize()' [ci skip]Sharang Dashputre2019-03-161-1/+1
| |
* | support slice assignment on SafeBufferRichard Monette2019-03-131-2/+6
| |
* | Change wording of some instances of 'opt out' [ci skip]Sharang Dashputre2019-03-122-10/+10
| |
* | Add locale option to parameterizeKaan Ozkan2019-03-111-2/+7
| | | | | | | | | | | | Parameterize is triggering I18n#transliterate. This method already accepts a locale. It would be cleaner if similar to other string inflection methods #parameterize also accepted 'locale' as a parameter.
* | Fix including/excluding flatteningGabriel Sobrinho2019-03-062-7/+7
| |
* | Added Array#including, Array#excluding, Enumerable#including, ↵David Heinemeier Hansson2019-03-052-8/+40
| | | | | | | | Enumerable#excluding
* | activesupport: Simplify class_attribute implementation (#35454)Dylan Thacker-Smith2019-03-031-15/+10
| | | | | | | | | | | | | | | | * activesupport(class_attribute): Use redefine_singleton_method * activesupport(class_attribute): Use keyword arguments * activesupport(class_attribute): Avoid unnecessary redefinition for default
* | Remove redundant returning `object`Ryuta Kamizono2019-02-091-1/+0
| | | | | | | | `object.transform_values!` returns `object` itself.
* | Use Ruby 2.4+ native transform_values(!)Kasper Timm Hansen2019-02-081-7/+2
| |
* | Add 'Hash#deep_transform_values', and 'Hash#deep_transform_values!'Guillermo Iguaran2019-02-082-0/+53
| |
* | Remove the Kernel#` override that turns ENOENT into nilAkinori MUSHA2019-01-312-14/+0
| | | | | | | | | | | | | | | | | | | | | | | | ActiveSupport overrides `` Kernel#` `` so that it would not raise `Errno::ENOENT` but return `nil` instead (due to the last statement `STDERR.puts` returning nil) if a given command were not found. Because of this, you cannot safely say somthing like `` `command`.chomp `` when ActiveSupport is loaded. It turns out that this is an outdated monkey patch for Windows platforms to emulate Unix behavior on an ancient version of Ruby, and it should be removed by now.
* | No dup nor delete from optionsKrzysztof Rybka2019-01-181-5/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Memory comparison: Options: {:years=>1, :months=>1, :weeks=>1, :days=>1} Calculating ------------------------------------- master 576.000 memsize ( 0.000 retained) 5.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) advance_no_dup 288.000 memsize ( 0.000 retained) 4.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) Comparison: advance_no_dup: 288 allocated master: 576 allocated - 2.00x more Options: {:years=>1} Calculating ------------------------------------- master 264.000 memsize ( 0.000 retained) 2.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) advance_no_dup 72.000 memsize ( 0.000 retained) 1.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) Comparison: advance_no_dup: 72 allocated master: 264 allocated - 3.67x more Options: {:weeks=>1} Calculating ------------------------------------- master 264.000 memsize ( 0.000 retained) 2.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) advance_no_dup 72.000 memsize ( 0.000 retained) 1.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) Comparison: advance_no_dup: 72 allocated master: 264 allocated - 3.67x more Peformance comparison: Options: {:years=>1, :months=>1, :weeks=>1, :days=>1} Warming up -------------------------------------- master 27.740k i/100ms advance_no_dup 37.705k i/100ms Calculating ------------------------------------- master 338.511k (± 5.9%) i/s - 1.692M in 5.020333s advance_no_dup 572.980k (± 3.7%) i/s - 2.866M in 5.008680s Comparison: advance_no_dup: 572979.7 i/s master: 338510.9 i/s - 1.69x slower Options: {:years=>1} Warming up -------------------------------------- master 53.313k i/100ms advance_no_dup 115.016k i/100ms Calculating ------------------------------------- master 639.715k (± 1.7%) i/s - 3.199M in 5.001851s advance_no_dup 1.579M (± 6.4%) i/s - 7.936M in 5.053876s Comparison: advance_no_dup: 1579251.7 i/s master: 639714.8 i/s - 2.47x slower Options: {:weeks=>1} Warming up -------------------------------------- master 57.353k i/100ms advance_no_dup 129.141k i/100ms Calculating ------------------------------------- master 674.113k (± 3.4%) i/s - 3.384M in 5.025973s advance_no_dup 1.911M (± 2.5%) i/s - 9.556M in 5.004496s Comparison: advance_no_dup: 1910739.3 i/s master: 674112.6 i/s - 2.83x slower
* | Remove deprecated `Module#reachable?` methodRafael Mendonça França2019-01-172-7/+1
| |
* | Refactor calculating beginning_of_quarter and end_of_quarter (#34927)Krzysztof Rybka2019-01-141-2/+2
| | | | | | | | | | | | | | * Calculate first month of quarter instead of finding * Calculate last month of quarter instead of finding [Krzysztof Rybka + Rafael Mendonça França]
* | Clarify `delegate_missing_to` [ci skip]bogdanvlviv2019-01-041-1/+1
| | | | | | | | | | | | | | | | Since #34864 removed explicit receiver to clarify the purpose of `delegate_missing_to`, I think it will be better to do the same a few lines above to easier figure out that `delegate_missing_to` defines `method_missing`, `respond_to_missing?` when comparing these examples.
* | Clarify benefit of `delegate_missing_to`Michael Gee2019-01-041-1/+1
| | | | | | Removing the explicit receiver clarifies the purpose of `delegate_missing_to`.
* | Make Active Storage blob keys lowercaseJulik Tarkhanov2018-12-301-3/+23
| | | | | | Accommodate case-insensitive filesystems and database collations.
* | Clarify the :to parameter of delegateEddie Lebow2018-12-201-1/+1
| |