Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::HashSyntax

This cop checks hash literal syntax.

It can enforce either the use of the class hash rocket syntax or the use of the newer Ruby 1.9 syntax (when applicable).

A separate offense is registered for each problematic pair.

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/hash_syntax.rb, line 35
def autocorrect(node)
  key = node.children.first.loc.expression
  op = node.loc.operator

  @corrections << lambda do |corrector|
    if style == :ruby19
      range = Parser::Source::Range.new(key.source_buffer,
                                        key.begin_pos, op.end_pos)
      range = range_with_surrounding_space(range, :right)
      corrector.replace(range,
                        range.source.sub(/^:(.*\S)\s*=>\s*$/, '\1: '))
    else
      corrector.insert_after(key, ' => ')
      corrector.insert_before(key, ':')
      corrector.remove(range_with_surrounding_space(op))
    end
  end
end
hash_rockets_check(node) click to toggle source
# File lib/rubocop/cop/style/hash_syntax.rb, line 29
def hash_rockets_check(node)
  pairs = *node

  check(pairs, ':', MSG_HASH_ROCKETS)
end
on_hash(node) click to toggle source
# File lib/rubocop/cop/style/hash_syntax.rb, line 17
def on_hash(node)
  style == :ruby19 ? ruby19_check(node) : hash_rockets_check(node)
end
ruby19_check(node) click to toggle source
# File lib/rubocop/cop/style/hash_syntax.rb, line 21
def ruby19_check(node)
  pairs = *node

  sym_indices = pairs.all? { |p| word_symbol_pair?(p) }

  check(pairs, '=>', MSG_19) if sym_indices
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.