Skip to content

Instantly share code, notes, and snippets.

@l15n
Created April 1, 2009 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l15n/88570 to your computer and use it in GitHub Desktop.
Save l15n/88570 to your computer and use it in GitHub Desktop.
# Set YAML output of String values in hashes to be double quoted
# Only modifies hashes that are about to run to_yaml
require 'yaml'
class Hash
def to_yaml_with_quoted_strings(*args)
class << self
unless method_defined?(:each_with_quoted_strings)
def each_with_quoted_strings
each_with_normal_strings do |k,v|
if String === v && !v.frozen?
def v.to_yaml_style; return :quote2; end
end
yield k, v
end
end
alias_method :each_with_normal_strings, :each
alias_method :each, :each_with_quoted_strings
end
end
return to_yaml_with_normal_strings(*args)
end
alias_method :to_yaml_with_normal_strings, :to_yaml
alias_method :to_yaml, :to_yaml_with_quoted_strings
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment