Class: HashFormatter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hash_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ HashFormatter

Returns a new instance of HashFormatter.



7
8
9
10
11
# File 'lib/hash_formatter.rb', line 7

def initialize(string)
  @parser = RubyParser.new
  @r2r    = Ruby2Ruby.new
  @hash   = parse(string)
end

Instance Method Details

#eachObject



25
26
27
28
# File 'lib/hash_formatter.rb', line 25

def each
  return to_enum unless block_given?
  @hash.map {|a| yield a.map(&method(:to_ruby))}
end

#to_18Object



17
18
19
# File 'lib/hash_formatter.rb', line 17

def to_18
  "{%s}" % map {|a| a.join(' => ')}.join(', ')
end

#to_19Object



21
22
23
# File 'lib/hash_formatter.rb', line 21

def to_19
  "{%s}" % map {|a| format_pair(*a)}.join(', ')
end

#to_multilineObject



13
14
15
# File 'lib/hash_formatter.rb', line 13

def to_multiline
  "{\n%s\n}" % map {|(k, v)| "  #{pad(k)} => #{v}"}.join(",\n")
end