Ticket #26 (new defect)

Opened 22 months ago

Syntax Error reading in Hash w/ keys of custom classes or of the Range class from YAML

Reported by: rampion Owned by: somebody
Priority: major Milestone:
Component: component1 Version:
Keywords: Cc:

Description

(Using ruby 1.8.5)

Using custom ruby classes as Hash keys causes a syntax error when trying to load the YAML generated from the Hash.

require 'yaml'

class Example_1; end

ex1 = Example_1.new
hash1 = {ex1 => true}

[ [:ex1, ex1], [:hash1, hash1]].each do |name, obj|
	puts "#{name} := #{obj.inspect}"
	puts "#{name}.to_yaml :=<<EOY"
	puts obj.to_yaml
	puts "EOY"
	begin
		obj_copy = YAML::load(obj.to_yaml)
		puts "#{name}_copy = #{obj_copy.inspect}"
	rescue Exception => e
		puts "Exception: #{e.inspect}"
	end
	puts
end
ex1 := #<Example_1:0x1da7b8>
ex1.to_yaml :=<<EOY
--- !ruby/object:Example_1 {}

EOY
ex1_copy = #<Example_1:0x1d7b80>

hash1 := {#<Example_1:0x1da7b8>=>true}
hash1.to_yaml :=<<EOY
--- 
!ruby/object:Example_1 ? {}
: true

EOY
Exception: #<ArgumentError: syntax error on line 2, col -1: `: true

'>

The same error also happens when trying to use a Range as a Hash key.

require 'yaml'

ex2 = Range.new(0,0)
hash2 = {ex2 => true}

[ [:ex2, ex2], [:hash2, hash2]].each do |name, obj|
	puts "#{name} := #{obj.inspect}"
	puts "#{name}.to_yaml :=<<EOY"
	puts obj.to_yaml
	puts "EOY"
	begin
		obj_copy = YAML::load(obj.to_yaml)
		puts "#{name}_copy = #{obj_copy.inspect}"
	rescue Exception => e
		puts "Exception: #{e.inspect}"
	end
	puts
end
ex2 := 0..0
ex2.to_yaml :=<<EOY
--- !ruby/range 
begin: 0
end: 0
excl: false
EOY
ex2_copy = 0..0

hash2 := {0..0=>true}
hash2.to_yaml :=<<EOY
--- 
!ruby/range ? 
  begin: 0
  end: 0
  excl: false
: true

EOY
Exception: #<ArgumentError: syntax error on line 2, col -1: `  begin: 0
  end: 0
  excl: false
: true

'>
Note: See TracTickets for help on using tickets.