summaryrefslogtreecommitdiff
path: root/ext/psych/lib/psych
diff options
context:
space:
mode:
Diffstat (limited to 'ext/psych/lib/psych')
-rw-r--r--ext/psych/lib/psych/nodes/node.rb4
-rw-r--r--ext/psych/lib/psych/visitors/to_ruby.rb14
2 files changed, 12 insertions, 6 deletions
diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb
index f59fb8916b..7e414e771f 100644
--- a/ext/psych/lib/psych/nodes/node.rb
+++ b/ext/psych/lib/psych/nodes/node.rb
@@ -46,8 +46,8 @@ module Psych
# Convert this node to Ruby.
#
# See also Psych::Visitors::ToRuby
- def to_ruby
- Visitors::ToRuby.create.accept(self)
+ def to_ruby(symbolize_names: false)
+ Visitors::ToRuby.create(symbolize_names: symbolize_names).accept(self)
end
alias :transform :to_ruby
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb
index b72fb4a1dc..3021aa7eda 100644
--- a/ext/psych/lib/psych/visitors/to_ruby.rb
+++ b/ext/psych/lib/psych/visitors/to_ruby.rb
@@ -12,20 +12,21 @@ module Psych
###
# This class walks a YAML AST, converting each node to Ruby
class ToRuby < Psych::Visitors::Visitor
- def self.create
+ def self.create(symbolize_names: false)
class_loader = ClassLoader.new
scanner = ScalarScanner.new class_loader
- new(scanner, class_loader)
+ new(scanner, class_loader, symbolize_names: symbolize_names)
end
attr_reader :class_loader
- def initialize ss, class_loader
+ def initialize ss, class_loader, symbolize_names: false
super()
@st = {}
@ss = ss
@domain_types = Psych.domain_types
@class_loader = class_loader
+ @symbolize_names = symbolize_names
end
def accept target
@@ -336,7 +337,12 @@ module Psych
SHOVEL = '<<'
def revive_hash hash, o
o.children.each_slice(2) { |k,v|
- key = deduplicate(accept(k))
+ key = accept(k)
+ if @symbolize_names
+ key = key.to_sym
+ else
+ key = deduplicate(key)
+ end
val = accept(v)
if key == SHOVEL && k.tag != "tag:yaml.org,2002:str"