Project

General

Profile

Actions

Bug #20003

closed

Array#rassoc does not preform implicit conversion

Added by temabolshakov (Tema Bolshakov) 5 months ago. Updated 5 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:115355]

Description

There is a difference between behaviour of Array#assoc and Array#rassoc. The first one performs implicit conversion (calls #to_ary) while the former does not.

class ArrayConvertible
  def initialize(*values)
    @values = values;
  end

  def to_ary
    @values
  end
end

s1 = [1, 2]
s2 = ArrayConvertible.new(2, 3)
a = [s1, s2]

The a.assoc(2) call returns [2, 3] as expected. However, a.rassoc(3) returns nil

Expected behaviour: a.rassoc(3) returns [2, 3] in such cases.

Updated by temabolshakov (Tema Bolshakov) 5 months ago

temabolshakov (Tema Bolshakov) wrote:

There is a difference between behaviour of Array#assoc and Array#rassoc. The first one performs implicit conversion (calls #to_ary) while the former does not.

class ArrayConvertible
  def initialize(*values)
    @values = values;
  end

  def to_ary
    @values
  end
end

s1 = [1, 2]
s2 = ArrayConvertible.new(2, 3)
a = [s1, s2]

The a.assoc(2) call returns [2, 3] as expected. However, a.rassoc(3) returns nil

Expected behaviour: a.rassoc(3) returns [2, 3] in such cases.

I prepared a fix here https://github.com/ruby/ruby/pull/8904

Updated by mame (Yusuke Endoh) 5 months ago

Thanks, I will merge it.

Actions #3

Updated by temabolshakov (Tema Bolshakov) 5 months ago

  • Status changed from Open to Closed

Applied in changeset git|e4a11a1283da07fd1d94535298c605caf299a834.


Array#rassoc should try to convert to array implicitly. Fixes #20003

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0