Class: Object

Inherits:
BasicObject
Defined in:
lib/leap_cli/core_ext/yaml.rb

Instance Method Summary collapse

Instance Method Details

#sorted_ya2yaml(options = {}) ⇒ Object

ya2yaml will output hash keys in sorted order, but it outputs arrays in natural order. This new method, sorted_ya2yaml(), is the same as ya2yaml but ensures that arrays are sorted.

This is important so that the .yaml files don’t change each time you recompile.

see github.com/afunai/ya2yaml/blob/master/lib/ya2yaml.rb



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/leap_cli/core_ext/yaml.rb', line 11

def sorted_ya2yaml(options = {})
  # modify array
  Array.class_eval do
    alias_method :collect_without_sort, :collect
    def collect(&block)
      sorted = sort {|a,b| a.to_s <=> b.to_s}
      sorted.collect_without_sort(&block)
    end
  end

  # generate yaml
  yaml_str = self.ya2yaml(options)

  # restore array
  Array.class_eval {alias_method :collect, :collect_without_sort}

  return yaml_str
end