Project

General

Profile

Actions

Bug #5406

closed

inconsistent odd-length array rejection in Hash[*ary] vs Hash[ary]

Added by sunaku (Suraj Kurapati) about 13 years ago. Updated over 12 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
Backport:
[ruby-core:39945]

Description

Hello,

Hash[*ary] raises an error when given odd number of arguments:

Hash.[]( 1, 2, 3 )
ArgumentError: odd number of arguments for Hash
from (irb):3:in []' from (irb):3 from /usr/bin/irb:12:in '

In contrast, Hash[ary] silently accepts odd-length arrays:

Hash.[]( [1, 2, 3] )
{}
Hash.[]( [[1, 2], 3] )
{
1 => 2
}
Hash.[]( [[1, 2], [3]] )
{
1 => 2,
3 => nil
}
Hash.[]( [[1, 2], [3,4,5]] )
{
1 => 2
}

I think this behavior is inconsistent. Please either:

(1) change Hash[ary] to raise errors upon seeing odd-length arrays or

(2) change Hash[*ary] to not raise an error for odd number of arguments and instead accept the last odd argument as a key with value nil, just like this existing behavior:

Hash.[]( [[1, 2], [3]] )
{
1 => 2,
3 => nil
}

Thanks for your consideration.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #1385: Wonderful undocumented feature in Ruby 1.8.7 & 1.9Closedyugui (Yuki Sonoda)04/17/2009Actions

Updated by ko1 (Koichi Sasada) almost 13 years ago

  • Category set to core
  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by matz (Yukihiro Matsumoto) over 12 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)

Hash should raise an exception, when invalid input comes.

Matz.

Actions #3

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

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

This issue was solved with changeset r36391.
Suraj, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


hash.c: raise on invalid input

  • hash.c (rb_hash_s_create): raise an exception, when input elements
    are not one or two elements arrays. [ruby-core:39945] [Bug #5406]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0