Forum: Ruby-core Hash#select and Hash#reject bug in 1.9.2?

Posted by Lin Jen-Shin (Guest)
on 2011-01-03 13:26
(Received via mailing list)
(sorry if this is a duplicated post, i forgot to subscribe)

Block parameter with or without () is not consistent in select or
reject,
while each and map is consistent. Is this a bug?

> ruby -v
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.5.0]

# each and map is consistent with or without ()

>> {a:1}.each{ |k,v| p [k,v].map(&:inspect) }
[":a", "1"]

>> {a:1}.each{ |(k,v)| p [k,v].map(&:inspect) }
[":a", "1"]

>> {a:1}.map{ |k,v| p [k,v].map(&:inspect) }
[":a", "1"]

>> {a:1}.map{ |(k,v)| p [k,v].map(&:inspect) }
[":a", "1"]

# select and reject is not consistent with or without ()

>> {a:1}.select{ |k,v| p [k,v].map(&:inspect) }
[":a", "1"]

>> {a:1}.select{ |(k,v)| p [k,v].map(&:inspect) }
[":a", "nil"]

>> {a:1}.reject{ |k,v| p [k,v].map(&:inspect) }
[":a", "1"]

>> {a:1}.reject{ |(k,v)| p [k,v].map(&:inspect) }
[":a", "nil"]

Thanks a lot!

p.s. http://redmine.ruby-lang.org is down at the moment
Posted by Yusuke ENDOH (Guest)
on 2011-01-05 10:59
(Received via mailing list)
Hi,

2011/1/3 Lin Jen-Shin <godfat@gmail.com>:
> Block parameter with or without () is not consistent in select or
> reject,
> while each and map is consistent. Is this a bug?

This is because Hash#each and map uses yield([k, v]) while select and
reject uses yield(k, v).  Perhaps this difference is unintentional and
meaningless.

However, unless there is any actual problem caused by the difference
(except for consisitency), it will be difficult to change the behavior
because of compatibility.

Thanks,
Posted by Lin Jen-Shin (Guest)
on 2011-01-05 11:54
(Received via mailing list)
Hi,

On Jan 5, 5:58pm, Yusuke ENDOH <m...@tsg.ne.jp> wrote:
[...]
> However, unless there is any actual problem caused by the difference
> (except for consisitency), it will be difficult to change the behavior
> because of compatibility.
>
> Thanks,
>
> --
> Yusuke Endoh <m...@tsg.ne.jp>

In terms of compatibility, I think it might be better to change
actually,
because 1.8.7 is consistent here. On the other hand, I wonder if
anyone is depending on `hash.select{ |(k, v)| }' while v is nil?

Anyway, If we can't change this behaviour on 1.9.2, I would hope
this could be changed in 1.9.3+

Thanks a lot,
Jen-Shin.

p.s. ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Posted by Yusuke ENDOH (Guest)
on 2011-01-05 13:46
(Received via mailing list)
Hi,

2011/1/5 Lin Jen-Shin <godfat@gmail.com>:
> In terms of compatibility, I think it might be better to change
> actually,
> because 1.8.7 is consistent here.

Hmm.
At r13809, matz changed Hash#each to use yield([k, v]) instead of
yield(k, v).

http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=13809

Matz, did you intend this behavior change that Lin reported?


> On the other hand, I wonder if
> anyone is depending on `hash.select{ |(k, v)| }' while v is nil?

Though I may be too serious, I think of:

  h = {
    [1, 2] => "foo",
    [1, 3] => "bar",
    [2, 3] => "baz",
  }
  h = h.select {|(k1, k2)| (k1 + k2).odd? }
  p h  #=>  {[1, 2]=>"foo", [2, 3]=>"baz"}
Posted by Lin Jen-Shin (Guest)
on 2011-01-12 13:02
(Received via mailing list)
Hi,

On Jan 5, 8:45pm, Yusuke ENDOH <m...@tsg.ne.jp> wrote:
> --
> Yusuke Endoh <m...@tsg.ne.jp>

Oh, you're right. I totally forgot that a key might be an array,
although still did I hope this could be consistent in the future.
Should I file a ticket for this? Would this be considered a
bug request (thus for 1.9.2), or a feature request (thus for trunk)?

Thanks!
Posted by Yukihiro Matsumoto (Guest)
on 2011-01-12 17:18
(Received via mailing list)
Hi,

In message "Re: [ruby-core:34101] Re: Hash#select and Hash#reject bug in 
1.9.2?"
    on Wed, 5 Jan 2011 21:45:34 +0900, Yusuke ENDOH <mame@tsg.ne.jp> 
writes:

|Matz, did you intend this behavior change that Lin reported?

This change is intentional.  The block parameter behavior has changed
for 1.9 to make it more consistent.  Sorry for incompatibility.  If it
has huge impact (than I expected), please tell me.

              matz.
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.