Project

General

Profile

Actions

Bug #7893

closed

Issues with loading YAML and respond_to_missing?

Added by injekt (Lee Jarvis) almost 12 years ago. Updated almost 12 years ago.

Status:
Rejected
Target version:
-
ruby -v:
1.9.3p385
Backport:
[ruby-core:52575]

Description

This is perhaps not a bug, I could be missing something, but shouldn't the following code work?

require 'yaml'

class Person
  def initialize(name)
    @attributes = { name: name }
  end

  def method_missing(m, *args, &block)
    @attributes.key?(m) ? @attributes[m] : super
  end

  def respond_to_missing?(m, include_private = false)
    @attributes.key?(m) || super
  end
end

dump = YAML.dump Person.new("lee")
load = YAML.load dump

The YAML.dump appears to work as expected, yet when I attempt to load the dump back, I get the following exception:

/Users/lee/bin/rb:15:in `respond_to_missing?': undefined method `key?' for nil:NilClass (NoMethodError)
  from /Users/lee/.rubies/1.9.3-p385/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:291:in `respond_to?'
  from /Users/lee/.rubies/1.9.3-p385/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:291:in `init_with'
  from /Users/lee/.rubies/1.9.3-p385/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:284:in `revive'

However doing the same thing with JSON for example, works fine: https://gist.github.com/injekt/0a4aa3e25f09ae2f8cba

Actions

Also available in: Atom PDF

Like0
Like0Like0