mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use native Array#append
, Array#prepend
, Hash#transform_keys
, and Hash#transform_keys!
Since Rails 6 requires Ruby 2.5. https://github.com/ruby/ruby/blob/ruby_2_5/NEWS Follow up #34754.
This commit is contained in:
parent
3e50a1bcd4
commit
d5197d59a1
11 changed files with 6 additions and 181 deletions
|
@ -1,7 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/hash/keys"
|
||||
|
||||
module ActionController
|
||||
module ConditionalGet
|
||||
extend ActiveSupport::Concern
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/hash/keys"
|
||||
|
||||
module ActionController
|
||||
# ActionController::Renderer allows you to render arbitrary templates
|
||||
# without requirement of being in controller actions.
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/class/subclasses"
|
||||
require "active_support/core_ext/hash/keys"
|
||||
|
||||
module ActiveJob
|
||||
# Provides helper methods for testing Active Job
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/array/extract_options"
|
||||
require "active_support/core_ext/hash/keys"
|
||||
require "active_support/core_ext/hash/except"
|
||||
|
||||
module ActiveModel
|
||||
# == Active \Model \Validations
|
||||
|
|
|
@ -6,5 +6,4 @@ require "active_support/core_ext/array/conversions"
|
|||
require "active_support/core_ext/array/extract"
|
||||
require "active_support/core_ext/array/extract_options"
|
||||
require "active_support/core_ext/array/grouping"
|
||||
require "active_support/core_ext/array/prepend_and_append"
|
||||
require "active_support/core_ext/array/inquiry"
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Array
|
||||
# The human way of thinking about adding stuff to the end of a list is with append.
|
||||
alias_method :append, :push unless [].respond_to?(:append)
|
||||
require "active_support/deprecation"
|
||||
|
||||
# The human way of thinking about adding stuff to the beginning of a list is with prepend.
|
||||
alias_method :prepend, :unshift unless [].respond_to?(:prepend)
|
||||
end
|
||||
ActiveSupport::Deprecation.warn "Ruby 2.5+ (required by Rails 6) provides Array#append and Array#prepend natively, so requiring active_support/core_ext/array/prepend_and_append is no longer necessary. Requiring it will raise LoadError in Rails 6.1."
|
||||
|
|
|
@ -1,35 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Hash
|
||||
# Returns a new hash with all keys converted using the +block+ operation.
|
||||
#
|
||||
# hash = { name: 'Rob', age: '28' }
|
||||
#
|
||||
# hash.transform_keys { |key| key.to_s.upcase } # => {"NAME"=>"Rob", "AGE"=>"28"}
|
||||
#
|
||||
# If you do not provide a +block+, it will return an Enumerator
|
||||
# for chaining with other methods:
|
||||
#
|
||||
# hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
|
||||
def transform_keys
|
||||
return enum_for(:transform_keys) { size } unless block_given?
|
||||
result = {}
|
||||
each_key do |key|
|
||||
result[yield(key)] = self[key]
|
||||
end
|
||||
result
|
||||
end unless method_defined? :transform_keys
|
||||
|
||||
# Destructively converts all keys using the +block+ operations.
|
||||
# Same as +transform_keys+ but modifies +self+.
|
||||
def transform_keys!
|
||||
return enum_for(:transform_keys!) { size } unless block_given?
|
||||
keys.each do |key|
|
||||
self[yield(key)] = delete(key)
|
||||
end
|
||||
self
|
||||
end unless method_defined? :transform_keys!
|
||||
|
||||
# Returns a new hash with all keys converted to strings.
|
||||
#
|
||||
# hash = { name: 'Rob', age: '28' }
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "concurrent/map"
|
||||
require "active_support/core_ext/array/prepend_and_append"
|
||||
require "active_support/i18n"
|
||||
require "active_support/deprecation"
|
||||
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_unit"
|
||||
require "active_support/core_ext/array"
|
||||
|
||||
class PrependAppendTest < ActiveSupport::TestCase
|
||||
def test_append
|
||||
assert_equal [1, 2], [1].append(2)
|
||||
def test_requiring_prepend_and_append_is_deprecated
|
||||
assert_deprecated do
|
||||
require "active_support/core_ext/array/prepend_and_append"
|
||||
end
|
||||
|
||||
def test_prepend
|
||||
assert_equal [2, 1], [1].prepend(2)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_unit"
|
||||
require "active_support/core_ext/hash/keys"
|
||||
|
||||
class TransformKeysTest < ActiveSupport::TestCase
|
||||
test "transform_keys returns a new hash with the keys computed from the block" do
|
||||
original = { a: "a", b: "b" }
|
||||
mapped = original.transform_keys { |k| "#{k}!".to_sym }
|
||||
|
||||
assert_equal({ a: "a", b: "b" }, original)
|
||||
assert_equal({ a!: "a", b!: "b" }, mapped)
|
||||
end
|
||||
|
||||
test "transform_keys! modifies the keys of the original" do
|
||||
original = { a: "a", b: "b" }
|
||||
mapped = original.transform_keys! { |k| "#{k}!".to_sym }
|
||||
|
||||
assert_equal({ a!: "a", b!: "b" }, original)
|
||||
assert_same original, mapped
|
||||
end
|
||||
|
||||
test "transform_keys returns a sized Enumerator if no block is given" do
|
||||
original = { a: "a", b: "b" }
|
||||
enumerator = original.transform_keys
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
test "transform_keys! returns a sized Enumerator if no block is given" do
|
||||
original = { a: "a", b: "b" }
|
||||
enumerator = original.transform_keys!
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
test "transform_keys is chainable with Enumerable methods" do
|
||||
original = { a: "a", b: "b" }
|
||||
mapped = original.transform_keys.with_index { |k, i| [k, i].join.to_sym }
|
||||
assert_equal({ a0: "a", b1: "b" }, mapped)
|
||||
end
|
||||
|
||||
test "transform_keys! is chainable with Enumerable methods" do
|
||||
original = { a: "a", b: "b" }
|
||||
original.transform_keys!.with_index { |k, i| [k, i].join.to_sym }
|
||||
assert_equal({ a0: "a", b1: "b" }, original)
|
||||
end
|
||||
|
||||
test "transform_keys returns a Hash instance when self is inherited from Hash" do
|
||||
class HashDescendant < ::Hash
|
||||
def initialize(elements = nil)
|
||||
super(elements)
|
||||
(elements || {}).each_pair { |key, value| self[key] = value }
|
||||
end
|
||||
end
|
||||
|
||||
original = HashDescendant.new(a: "a", b: "b")
|
||||
mapped = original.transform_keys { |k| "#{k}!".to_sym }
|
||||
|
||||
assert_equal({ a: "a", b: "b" }, original)
|
||||
assert_equal({ a!: "a", b!: "b" }, mapped)
|
||||
assert_equal(::Hash, mapped.class)
|
||||
end
|
||||
end
|
|
@ -2132,30 +2132,6 @@ The methods `second`, `third`, `fourth`, and `fifth` return the corresponding el
|
|||
|
||||
NOTE: Defined in `active_support/core_ext/array/access.rb`.
|
||||
|
||||
### Adding Elements
|
||||
|
||||
#### `prepend`
|
||||
|
||||
This method is an alias of `Array#unshift`.
|
||||
|
||||
```ruby
|
||||
%w(a b c d).prepend('e') # => ["e", "a", "b", "c", "d"]
|
||||
[].prepend(10) # => [10]
|
||||
```
|
||||
|
||||
NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
|
||||
|
||||
#### `append`
|
||||
|
||||
This method is an alias of `Array#<<`.
|
||||
|
||||
```ruby
|
||||
%w(a b c d).append('e') # => ["a", "b", "c", "d", "e"]
|
||||
[].append([1,2]) # => [[1, 2]]
|
||||
```
|
||||
|
||||
NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
|
||||
|
||||
### Extracting
|
||||
|
||||
The method `extract!` removes and returns the elements for which the block returns a true value.
|
||||
|
@ -2646,48 +2622,6 @@ There's also the bang variant `except!` that removes keys in the very receiver.
|
|||
|
||||
NOTE: Defined in `active_support/core_ext/hash/except.rb`.
|
||||
|
||||
#### `transform_keys` and `transform_keys!`
|
||||
|
||||
The method `transform_keys` accepts a block and returns a hash that has applied the block operations to each of the keys in the receiver:
|
||||
|
||||
```ruby
|
||||
{nil => nil, 1 => 1, a: :a}.transform_keys { |key| key.to_s.upcase }
|
||||
# => {"" => nil, "1" => 1, "A" => :a}
|
||||
```
|
||||
|
||||
In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash:
|
||||
|
||||
```ruby
|
||||
{"a" => 1, a: 2}.transform_keys { |key| key.to_s.upcase }
|
||||
# The result could either be
|
||||
# => {"A"=>2}
|
||||
# or
|
||||
# => {"A"=>1}
|
||||
```
|
||||
|
||||
This method may be useful for example to build specialized conversions. For instance `stringify_keys` and `symbolize_keys` use `transform_keys` to perform their key conversions:
|
||||
|
||||
```ruby
|
||||
def stringify_keys
|
||||
transform_keys { |key| key.to_s }
|
||||
end
|
||||
...
|
||||
def symbolize_keys
|
||||
transform_keys { |key| key.to_sym rescue key }
|
||||
end
|
||||
```
|
||||
|
||||
There's also the bang variant `transform_keys!` that applies the block operations to keys in the very receiver.
|
||||
|
||||
Besides that, one can use `deep_transform_keys` and `deep_transform_keys!` to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
|
||||
|
||||
```ruby
|
||||
{nil => nil, 1 => 1, nested: {a: 3, 5 => 5}}.deep_transform_keys { |key| key.to_s.upcase }
|
||||
# => {""=>nil, "1"=>1, "NESTED"=>{"A"=>3, "5"=>5}}
|
||||
```
|
||||
|
||||
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
|
||||
|
||||
#### `stringify_keys` and `stringify_keys!`
|
||||
|
||||
The method `stringify_keys` returns a hash that has a stringified version of the keys in the receiver. It does so by sending `to_s` to them:
|
||||
|
|
Loading…
Reference in a new issue