Forum: Ruby-core [ruby-trunk - Bug #7893][Open] Issues with loading YAML and respond_to_missing?

Posted by Lee Jarvis (hiroshi)
on 2013-02-20 12:28
(Received via mailing list)
Issue #7893 has been reported by injekt (Lee Jarvis).

----------------------------------------
Bug #7893: Issues with loading YAML and respond_to_missing?
https://bugs.ruby-lang.org/issues/7893

Author: injekt (Lee Jarvis)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version:
ruby -v: 1.9.3p385


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
Posted by Hans Mackowiak (hanmac)
on 2013-02-20 13:54
(Received via mailing list)
Issue #7893 has been updated by Hanmac (Hans Mackowiak).



the problem is in  yaml: to_ruby.rb "if o.respond_to?(:init_with)"   so 
eigher you need to define a init_with method or

def respond_to_missing?(m, include_private = false)
  (@attributes && @attributes.key?(m)) || super
end
----------------------------------------
Bug #7893: Issues with loading YAML and respond_to_missing?
https://bugs.ruby-lang.org/issues/7893#change-36671

Author: injekt (Lee Jarvis)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version:
ruby -v: 1.9.3p385


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
Posted by drbrain (Eric Hodel) (Guest)
on 2013-02-22 00:01
(Received via mailing list)
Issue #7893 has been updated by drbrain (Eric Hodel).

Category changed from core to ext
Status changed from Open to Rejected
Assignee set to tenderlovemaking (Aaron Patterson)

As Hanmac noted, this exception is possible with 
Person.allocate.respond_to? which is exactly what Psych does.

Perhaps Psych needs some documentation improvement around dump and load.
----------------------------------------
Bug #7893: Issues with loading YAML and respond_to_missing?
https://bugs.ruby-lang.org/issues/7893#change-36722

Author: injekt (Lee Jarvis)
Status: Rejected
Priority: Normal
Assignee: tenderlovemaking (Aaron Patterson)
Category: ext
Target version:
ruby -v: 1.9.3p385


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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.