Bug #9898

Keyword argument oddities

Added by Brian Shirai about 1 year ago. Updated 12 months ago.

[ruby-core:62921]
Status:Closed
Priority:Normal
Assignee:-
ruby -v:ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] Backport:2.0.0: DONTNEED, 2.1: DONE

Description

Case 1: If a method takes a single argument, and the method is called with a keyword splat, the method receives the passed object, not the result of calling #to_hash on the object:

# kw1.rb
def m(a) a end
o = Object.new
def o.to_hash() puts "to_hash"; { a: 1 } end
p m(**o)
$ ruby -v kw1.rb
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
to_hash
#<Object:0x007fdee188ab90>

Would someone please explain why the result of #to_hash in not passed to the method?

Case 2: If the method takes a keyword argument, #to_hash is called at the send site and at the receiver:

# kw2.rb
def mk(a:) a end
o = Object.new
def o.to_hash() puts "to_hash"; { a: 1 } end
p mk(**o)
$ ruby -v kw2.rb
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
to_hash
to_hash
1

Would someone please explain why #to_hash is called at both the send site and at the receiver?


Related issues

Duplicated by Ruby trunk - Bug #9954: Unexpected Hash Modification and Segmentation Fault with Named Arguments Closed 06/18/2014

Associated revisions

Revision 46342
Added by Nobuyoshi Nakada about 1 year ago

vm.c: return the result hash

  • vm.c (core_hash_merge_kwd): should return the result hash, which may be converted from and differ from the given argument. [Bug #9898]

Revision 46342
Added by Nobuyoshi Nakada about 1 year ago

vm.c: return the result hash

  • vm.c (core_hash_merge_kwd): should return the result hash, which may be converted from and differ from the given argument. [Bug #9898]

Revision 46619
Added by Tomoyuki Chikanaga about 1 year ago

merge revision(s) r46342: [Backport #9954]

* vm.c (core_hash_merge_kwd): should return the result hash, which
  may be converted from and differ from the given argument.
   [Bug #9898]

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

  • Description updated (diff)
  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN to 2.0.0: DONTNEED, 2.1: REQUIRED

#2 Updated by Nobuyoshi Nakada about 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r46342.


vm.c: return the result hash

  • vm.c (core_hash_merge_kwd): should return the result hash, which may be converted from and differ from the given argument. [Bug #9898]

#3 Updated by Nobuyoshi Nakada about 1 year ago

  • Duplicated by Bug #9954: Unexpected Hash Modification and Segmentation Fault with Named Arguments added

#4 Updated by Tomoyuki Chikanaga 12 months ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: REQUIRED to 2.0.0: DONTNEED, 2.1: DONE

Backported into ruby_2_1 at r46619.

Also available in: Atom PDF