Feature #9451

Refinements and unary & (to_proc)

Added by Jan Lelis about 1 year ago. Updated 10 months ago.

[ruby-core:60109]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto

Description

Not sure if this is a bug or feature request:

require 'minitest/autorun'
require 'set'

module ClassToProc
  refine Class do
    def to_proc
      lambda{ |*args| self.new(*args) }
    end
  end
end

using ClassToProc

describe 'Class#to_proc' do
  it 'works when called directly' do
    Set.to_proc[[1,2]].must_equal Set[1,2]
  end

  it 'fails when called via ampersand' do
    [[1,2]].map(&Set).must_equal [Set[1,2]]
  end
end

The second example errors with NoMethodError: super: no superclass method `to_proc' for Set:Class

Would be great to have it, though.

History

#1 Updated by Yukihiro Matsumoto about 1 year ago

  • Assignee set to Shugo Maeda

As #=== from case statement and #each from for statement, I think refinement should be effective to methods that called implicitly. Let us discuss about the issue.

Matz.

#2 Updated by Shugo Maeda about 1 year ago

  • Tracker changed from Bug to Feature

Yukihiro Matsumoto wrote:

As #=== from case statement and #each from for statement, I think refinement should be effective to methods that called implicitly. Let us discuss about the issue.

What do you think of other implicit conversions such as to_a called by the splat operator.

#3 Updated by Shugo Maeda about 1 year ago

  • Assignee changed from Shugo Maeda to Yukihiro Matsumoto
  • Category set to core
  • Target version set to current: 2.2.0
  • Status changed from Open to Assigned

I'd like to clarify the design policy rather than adding features ad hoc.

All implicit method calls in syntactic constructs should be affected by refinements, right?
Do other implementers, especially JRuby people, accept this policy?

#4 Updated by Nobuyoshi Nakada 10 months ago

  • Description updated (diff)

IMHO, splat should be affected by refinements, because there is no intermediate method calls.

Also available in: Atom PDF