Class: Nucop::OrderedHash

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/nucop/cops/ordered_hash.rb

Overview

This cop looks for usages of ‘ActiveSupport::OrderedHash`

Hashes in Ruby (since 1.9) enumerate their keys in the order they are inserted:

“Hashes enumerate their values in the order that the corresponding keys were inserted.” ruby-doc.org/core-2.1.6/Hash.html

Examples:


# bad

hash = ActiveSupport::OrderedHash.new

# good

hash = {}

Constant Summary collapse

MSG =
"Ruby hashes after 1.9 enumerate keys in order of insertion"

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



36
37
38
39
40
# File 'lib/nucop/cops/ordered_hash.rb', line 36

def autocorrect(node)
  ->(corrector) do
    corrector.replace(node.source_range, "{}")
  end
end

#on_send(node) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/nucop/cops/ordered_hash.rb', line 26

def on_send(node)
  ordered_hash_usage(node) do
    add_offense(
      node,
      location: :expression,
      message: MSG
    )
  end
end