Project

General

Profile

Actions

Bug #12027

closed

Hash descendants are ignoring custom to_hash method

Bug #12027: Hash descendants are ignoring custom to_hash method

Added by zverok (Victor Shepelev) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:73506]

Description

I'm not sure it is not intended, but feels really strange:

class A def to_hash {foo: 'bar'} end end class B < Hash def to_hash {foo: 'bar'} end end a = A.new b = B.new p({**a}) # => {foo: 'bar'} p({**b}) # => {} -- ooops. Seems "internal" hash state is used # therefore: b['test'] = 1 p({**b}) # => wrong argument type String (expected Symbol) (TypeError) 

What am I missing here?

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago Actions #1 [ruby-core:73512]

  • Description updated (diff)
  • Status changed from Open to Rejected

Because b.is_a?(Hash) already.
to_hash is the method to pretend to be a Hash.

Actions

Also available in: PDF Atom