Class: HashFormatter
- Inherits:
-
Object
- Object
- HashFormatter
- Includes:
- Enumerable
- Defined in:
- lib/hash_formatter.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(string) ⇒ HashFormatter
constructor
A new instance of HashFormatter.
- #to_18 ⇒ Object
- #to_19 ⇒ Object
- #to_multiline ⇒ Object
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
#each ⇒ Object
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_18 ⇒ Object
17 18 19 |
# File 'lib/hash_formatter.rb', line 17 def to_18 "{%s}" % map {|a| a.join(' => ')}.join(', ') end |
#to_19 ⇒ Object
21 22 23 |
# File 'lib/hash_formatter.rb', line 21 def to_19 "{%s}" % map {|a| format_pair(*a)}.join(', ') end |
#to_multiline ⇒ Object
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 |