Method: Parser::Source::Map#to_hash

Defined in:
lib/parser/source/map.rb

#to_hashHash<Symbol, Parser::Source::Range>

Converts this source map to a hash with keys corresponding to ranges. For example, if called on an instance of Collection, which adds the ‘begin` and `end` ranges, the resulting hash will contain keys `:expression`, `:begin` and `:end`.

Examples:

require 'parser/current'

p Parser::CurrentRuby.parse('[1, 2]').loc.to_hash
# => {
#   :begin => #<Source::Range (string) 0...1>,
#   :end => #<Source::Range (string) 5...6>,
#   :expression => #<Source::Range (string) 0...6>
# }

Returns:


166
167
168
169
170
171
172
# File 'lib/parser/source/map.rb', line 166

def to_hash
  instance_variables.inject({}) do |hash, ivar|
    next hash if ivar.to_sym == :@node
    hash[ivar[1..-1].to_sym] = instance_variable_get(ivar)
    hash
  end
end