thebox
3/3/2007 4:26:00 AM
What am I doing wrong?
#!/opt/local/bin/ruby
###############################
require 'set'
require 'yaml'
require 'tempfile'
def tempname(basename)
file = Tempfile.new(basename)
file.close(false)
file.path
end
class U
attr_accessor :name
def initialize(n)
@name = n
end
def ==(other)
name == other.name
end
end
set = Set.new
set.add(U.new("one"))
set.add(U.new("two"))
filename = tempname("set_yaml")
File.open(filename, "w") {|f| YAML.dump(set, f)}
another_set = YAML.load(File.open(filename))
puts "#{set == another_set}"
###############################
The result that I get is the following:
###############################
xxx:~ piergiulianobossi$ ./set_yaml.rb
/opt/local/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 3,
col -1: ` name: one (ArgumentError)
: true
!ruby/object:U ?
name: two
: true
' from /opt/local/lib/ruby/1.8/yaml.rb:133:in `load'
from ./set_yaml.rb:30
###############################
Running ruby on Mac OS/X 10.4
###############################
xxx:~ piergiulianobossi$ ruby -v
ruby 1.8.5 (2006-08-25) [i686-darwin8.8.1]
###############################