aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/enumerable.rb
Commit message (Collapse)AuthorAgeFilesLines
* 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)
* Fix including/excluding flatteningGabriel Sobrinho2019-03-061-1/+1
|
* Added Array#including, Array#excluding, Enumerable#including, ↵David Heinemeier Hansson2019-03-051-4/+24
| | | | Enumerable#excluding
* Don't expose `Enumerable#_original_sum_with_required_identity` which is ↵yuuji.yaginuma2018-06-041-1/+5
| | | | | | | | | | internal API [ci skip] `:nodoc:` is specified, but unfortunately, it is exposed in the API doc. http://edgeapi.rubyonrails.org/classes/Enumerable.html#method-i-_original_sum_with_required_identity If the method of the `alias` destination is public, the specification of `:nodoc:` does not seem to work.
* Add Enumerable#index_with.Kasper Timm Hansen2018-05-211-1/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In the app I'm working on I've wished that index_by had a buddy that would assign the hash value instead of the key multiple times. Enter index_with. Useful when building a hash from a static list of symbols. Before you'd do: ```ruby POST_ATTRIBUTES.map { |attr_name| [ attr_name, public_send(attr_name) ] }.to_h ``` But now that's a little clearer and faster with: ````ruby POST_ATTRIBUTES.index_with { |attr_name| public_send(attr_name) } ``` It's also useful when you have an enumerable that should be converted to a hash, but you don't want to muddle the code up with the overhead that it takes to create that hash. So before, that's: ```ruby WEEKDAYS.each_with_object(Hash.new) do |day, intervals| intervals[day] = [ Interval.all_day ] end ``` And now it's just: ```ruby WEEKDAYS.index_with([ Interval.all_day ]) ``` It's also nice to quickly get a hash with either nil, [], or {} as the value.
* Don't doc _original_sum_with_required_identityChris Arcand2018-04-121-1/+1
| | | | It's not public API so don't document it.
* Remove native `Array#sum` and `Enumerable#sum` detection (#32102)Ryuta Kamizono2018-02-261-63/+44
| | | Since #32034, Rails 6 requires Ruby 2.4.1+.
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* 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 comment out [ci skip]yuuji.yaginuma2017-01-201-7/+7
|
* Refining Array#sum monkey-patch using RefinementsAkira Matsuda2016-12-151-2/+7
| | | | Because we don't want to see our ugly orig_sum method outside of this file
* Perfomance fix for Enumerable#index_bylvl0nax2016-05-141-1/+3
| | | | | | | | | | | | Calculating ------------------------------------- before 34.731k i/100ms after 48.206k i/100ms ------------------------------------------------- before 508.451k (± 1.2%) i/s - 2.570M after 720.068k (± 0.9%) i/s - 3.615M Comparison: after: 720067.6 i/s before: 508451.1 i/s - 1.42x slower
* Use original `Array#sum` to speed up calculatingyui-knk2016-05-071-4/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Use original `Array#sum` when calculating Numeric sum. This commit is related #24804 issue. Issue #24804 reports `Array#sum` becomes much slower when ActiveSupport is included. This commit tries to use original method as far as possible. ```shell $ cat array_sum.rb class Array alias core_sum sum end require 'benchmark/ips' require 'active_support/core_ext/enumerable' ary = [1.0] * 1_000_000 Benchmark.ips do |x| x.report("core sum") { ary.core_sum } x.report("AS's sum") { ary.sum } x.compare! end $ bundle exec ruby -v -I lib array_sum.rb ruby 2.4.0dev (2016-05-01 master 54867) [x86_64-darwin14] Calculating ------------------------------------- core sum 4.000 i/100ms AS's sum 5.000 i/100ms ------------------------------------------------- core sum 50.492 (± 7.9%) i/s - 252.000 AS's sum 50.116 (± 6.0%) i/s - 250.000 Comparison: core sum: 50.5 i/s AS's sum: 50.1 i/s - 1.01x slower ``` Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
* Fix initial value effects for sum along to ruby 2.4Kenta Murata2016-04-301-5/+7
| | | | Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
* Remove `Array#sum` method before override ityui-knk2016-04-241-0/+2
| | | | | | | To suppress warning ('warning: method redefined; discarding old sum') remove the method before override it. Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
* Ruby 2.4 Array#sum: fix non-numeric #sum feature detectionJeremy Daer2016-04-191-1/+1
|
* Ruby 2.4: compat with new Array#sumJeremy Daer2016-04-181-0/+14
| | | | | | | | | | | | | | | | | | | | | Ruby 2.4 introduces `Array#sum`, but it only supports numeric elements, breaking our `Enumerable#sum` which supports arbitrary `Object#+`. To fix, override `Array#sum` with our compatible implementation. Native Ruby 2.4: %w[ a b ].sum # => TypeError: String can't be coerced into Fixnum With `Enumerable#sum` shim: %w[ a b ].sum # => 'ab' We tried shimming the fast path and falling back to the compatible path if it fails, but that ends up slower even in simple causes due to the cost of exception handling. Our only choice is to override the native `Array#sum` with our `Enumerable#sum`.
* Change Enumerable#sum to use inject(:sym) specificationT.J. Schuck2015-11-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Not only does this make for simpler, more obvious code, it's also more performant: require 'benchmark/ips' module Enumerable def old_sum(identity = 0, &block) if block_given? map(&block).old_sum(identity) else inject { |sum, element| sum + element } || identity end end def new_sum(identity = 0, &block) if block_given? map(&block).new_sum(identity) else inject(:+) || identity end end end summable = (1..100).to_a # sum is 5050 Benchmark.ips do |x| x.report("old_sum") { summable.old_sum } x.report("new_sum") { summable.new_sum } x.compare! end # Calculating ------------------------------------- # old_sum 10.674k i/100ms # new_sum 14.542k i/100ms # ------------------------------------------------- # old_sum 117.350k (± 7.1%) i/s - 587.070k # new_sum 154.712k (± 3.8%) i/s - 785.268k # # Comparison: # new_sum: 154712.1 i/s # old_sum: 117350.0 i/s - 1.32x slower More benchmarks [here](https://gist.github.com/tjschuck/b3fe4e8c812712376648), including summing strings and passing blocks. The performance gains are less for those, but this version still always wins.
* Allow Enumerable#pluck to take a splat.Kevin Deisz2015-05-291-2/+9
| | | | | | This allows easier integration with ActiveRecord, such that AR#pluck will now use Enumerable#pluck if the relation is loaded, without needing to hit the database.
* Add Enumerable#pluck.Kevin Deisz2015-05-281-0/+8
| | | | Allows fetching the same values from arrays as from ActiveRecord associations.
* Use include? instead of in? for Enumerable#without.Juanito Fatas2015-03-021-1/+1
| | | | [egilburg]
* Add Enumerable#withoutTodd Bealmear2015-03-011-0/+11
|
* Return sized enumerator from Enumerable#index_byMarc-Andre Lafortune2014-02-051-1/+1
|
* Return sized enumerator from Batches#find_eachMarc-Andre Lafortune2014-02-051-1/+1
|
* update AS/core_ext docs [ci skip]Francesco Rodriguez2012-09-121-5/+6
|
* Fix Range#sum optimized versionAlexey Vakhov2012-05-251-2/+6
| | | | | | | | | | | | | | | | | | | | | | At 1bd4d1c67459a91415ee73a8f55d2309c0d62a87 was added Range#sum optimized version for arithmetic progressions. This improvment injected a defect with not integer range boundaries. The defect was fixed by e0adfa82c05f9c975005f102b4bcaebfcd17d241. The second commit really disabled optimization at all because in Ruby integer-valued numbers are instances of Fixnum and Bignum classes. We should #use is_a? (#kind_of?) method instead #instance_of? to check if value is numerical: 1.class # => Fixnum 1.instance_of?(Integer) # => false 1.is_a?(Integer) # => true -100_000_000_000.class # => Bignum -100_000_000_000.instance_of?(Integer) # => false -100_000_000_000.is_a?(Integer) # => true Moreover original implementation of Range#sum has a defect with reverse range boundaries. If the first boundary is less than the second range is empty. Current commit fixes and tests this case too.
* removing unnecessary 'examples' noise from activesupportFrancesco Rodriguez2012-05-131-2/+2
|
* String quotes and trailing spacesAlexey Gaziev2012-04-291-1/+1
|
* AS core_ext refactoring pt.2Alexey Gaziev2012-04-291-3/+6
|
* AS core_ext refactoringAlexey Gaziev2012-04-291-3/+6
|
* remove Enumerable#each_with_object againSergey Nartimov2011-12-231-21/+0
| | | | it come back occasionally in 367741ef
* remove conflict markerVijay Dev2011-12-231-1/+0
|
* Revert "Added Enumerable#pluck to wrap the common pattern of ↵Aaron Patterson2011-12-221-5/+20
| | | | | | | | | | | | collect(&:method) *DHH*" This reverts commit 4d20de8a50d889a09e6f5642984775fe796ca943. Conflicts: activesupport/CHANGELOG.md activesupport/lib/active_support/core_ext/enumerable.rb activesupport/test/core_ext/enumerable_test.rb
* remove Enumerable#each_with_object from core_ext as it is present in ruby 1.9lest2011-12-211-21/+0
|
* Initial pass at removing dead 1.8.x code from Active Support.José Valim2011-12-201-37/+0
| | | | | | There are a bunch of other implicit branches that adds 1.8.x specific code that still needs to be removed. Pull requests for those cases are welcome.
* Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*David Heinemeier Hansson2011-12-021-0/+7
|
* docs formatting changesVijay Dev2011-09-011-2/+2
|
* Trivial optimization for Enumerable#each_with_objectMarc-Andre Lafortune2011-07-231-2/+2
|
* Insure that Enumerable#index_by, group_by, ... return EnumeratorsMarc-Andre Lafortune2011-07-231-0/+3
|
* Make Enumerable#many? iterate only over what is necessaryMarc-Andre Lafortune2011-07-231-3/+10
|
* Make Enumerable#many? not rely on #sizeMarc-Andre Lafortune2011-07-231-2/+2
|
* #many? uses count instead of select - a bit fasterJulius Markūnas2011-07-111-1/+1
|
* Clarify comment by removing french reference ('a la'). Should improve ↵Ben Orenstein2011-03-051-1/+1
| | | | readability for non-native english speakers.
* speed up index_by by removing a lolinjectAaron Patterson2010-10-011-4/+1
|
* Deletes trailing whitespaces (over text files only find * -type f -exec sed ↵Santiago Pastorino2010-08-141-4/+4
| | | | 's/[ \t]*$//' -i {} \;)
* edit pass to apply API guideline wrt the use of "# =>" in example codeXavier Noria2010-07-301-1/+2
|