Skip to content

Commit

Permalink
Merge pull request #34761 from kamipo/require-ruby-2.5
Browse files Browse the repository at this point in the history
Use native `Array#append`, `Array#prepend`, `Hash#transform_keys`, and `Hash#transform_keys!`
  • Loading branch information
kamipo committed Dec 20, 2018
2 parents 3e50a1b + d5197d5 commit 9c87474
Show file tree
Hide file tree
Showing 11 changed files with 6 additions and 181 deletions.
2 changes: 0 additions & 2 deletions actionpack/lib/action_controller/metal/conditional_get.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "active_support/core_ext/hash/keys"

module ActionController
module ConditionalGet
extend ActiveSupport::Concern
Expand Down
2 changes: 0 additions & 2 deletions actionpack/lib/action_controller/renderer.rb
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion activejob/lib/active_job/test_helper.rb
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions activemodel/lib/active_model/validations.rb
@@ -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
Expand Down
1 change: 0 additions & 1 deletion activesupport/lib/active_support/core_ext/array.rb
Expand Up @@ -6,5 +6,4 @@
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."
29 changes: 0 additions & 29 deletions activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -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' }
Expand Down
1 change: 0 additions & 1 deletion activesupport/lib/active_support/inflector/inflections.rb
@@ -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"

Expand Down
11 changes: 4 additions & 7 deletions activesupport/test/core_ext/array/prepend_append_test.rb
@@ -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)
end

def test_prepend
assert_equal [2, 1], [1].prepend(2)
def test_requiring_prepend_and_append_is_deprecated
assert_deprecated do
require "active_support/core_ext/array/prepend_and_append"
end
end
end
64 changes: 0 additions & 64 deletions activesupport/test/core_ext/hash/transform_keys_test.rb

This file was deleted.

66 changes: 0 additions & 66 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9c87474

Please sign in to comment.