Project

General

Profile

Feature #9451

Refinements and unary & (to_proc)

Added by rbjl (Jan Lelis) about 3 years ago. Updated 6 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:60109]

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.


Related issues

Duplicated by Ruby trunk - Feature #12638: Symbol#to_proc probable bug (seems not to support refinements) Closed
Duplicated by Ruby trunk - Feature #12079: Loosening the condition for refinement Rejected

Associated revisions

Revision 56426
Added by nobu (Nobuyoshi Nakada) 6 months ago

vm_args.c: allow refinements in Symbol proc

  • vm_args.c (refine_sym_proc_call): search and call method with refinements.
  • vm_args.c (vm_caller_setup_arg_block): enable refinements when enabled in the caller. [Feature #9451]

Revision 56426
Added by nobu (Nobuyoshi Nakada) 6 months ago

vm_args.c: allow refinements in Symbol proc

  • vm_args.c (refine_sym_proc_call): search and call method with refinements.
  • vm_args.c (vm_caller_setup_arg_block): enable refinements when enabled in the caller. [Feature #9451]

Revision 56427
Added by nobu (Nobuyoshi Nakada) 6 months ago

vm_args.c: cache procs

  • vm_args.c (vm_caller_setup_arg_block): cache proc with refinements. [Feature #9451]

Revision 56427
Added by nobu (Nobuyoshi Nakada) 6 months ago

vm_args.c: cache procs

  • vm_args.c (vm_caller_setup_arg_block): cache proc with refinements. [Feature #9451]

Revision 56431
Added by nobu (Nobuyoshi Nakada) 6 months ago

test_refinement.rb: test_symbol_proc

  • test/ruby/test_refinement.rb (test_symbol_proc): move from test_symbol.rb [Feature #9451]

Revision 56431
Added by nobu (Nobuyoshi Nakada) 6 months ago

test_refinement.rb: test_symbol_proc

  • test/ruby/test_refinement.rb (test_symbol_proc): move from test_symbol.rb [Feature #9451]

Revision 56476
Added by nobu (Nobuyoshi Nakada) 6 months ago

test_refinement.rb: Symbol#to_proc test

  • test/ruby/test_refinement.rb (test_symbol_proc): a Symbol is converted to a Proc in the caller's context. [Feature #9451]

Revision 56476
Added by nobu (Nobuyoshi Nakada) 6 months ago

test_refinement.rb: Symbol#to_proc test

  • test/ruby/test_refinement.rb (test_symbol_proc): a Symbol is converted to a Proc in the caller's context. [Feature #9451]

History

#1 [ruby-core:60187] Updated by matz (Yukihiro Matsumoto) about 3 years ago

  • Assignee set to shugo (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 [ruby-core:60193] Updated by shugo (Shugo Maeda) about 3 years 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 [ruby-core:60414] Updated by shugo (Shugo Maeda) about 3 years ago

  • Target version set to 2.2.0
  • Status changed from Open to Assigned
  • Assignee changed from shugo (Shugo Maeda) to matz (Yukihiro Matsumoto)
  • Category set to core

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 [ruby-core:62859] Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Description updated (diff)

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

#5 [ruby-core:71849] Updated by justcolin (Colin Fulton) over 1 year ago

Is there any update on this feature? In Ruby 2.2.3 I still run into a problem where the unary & can not be used when the method was added with a refinement, such as the following:

module Example
  refine String do
    def pugs
      "Pugs!"
    end
  end
end

using Example

('a'..'z').map(&:pugs)

I can understand why they currently do not work, but it leads to uglier code.

Cheers!

#6 [ruby-core:72058] Updated by justcolin (Colin Fulton) over 1 year ago

Sorry, just saw that issue 9786 is equivalent to this one and was marked as rejected. Thanks!

#7 Updated by nobu (Nobuyoshi Nakada) 9 months ago

  • Duplicated by Feature #12638: Symbol#to_proc probable bug (seems not to support refinements) added

#8 [ruby-core:77185] Updated by matz (Yukihiro Matsumoto) 8 months ago

I now think it's OK to accept this proposal. But I don't know how difficult to implement the issue.

Matz.

#9 Updated by shugo (Shugo Maeda) 8 months ago

  • Duplicated by Feature #12079: Loosening the condition for refinement added

#10 Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Status changed from Assigned to Closed

Applied in changeset r56426.


vm_args.c: allow refinements in Symbol proc

  • vm_args.c (refine_sym_proc_call): search and call method with refinements.
  • vm_args.c (vm_caller_setup_arg_block): enable refinements when enabled in the caller. [Feature #9451]

Also available in: Atom PDF