Project

General

Profile

Actions

Feature #17336

closed

using refined: do ... end

Added by k0kubun (Takashi Kokubun) over 1 year ago. Updated over 1 year ago.

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

Description

Problem

When we need a monkey patch which is used only in a single file, we'd like to define a refinement and use it in the same place. The problem is that it needs deep indentation and Module.new { ... } which feels redundant.

class Foo
  using Module.new {
    refine Array do
      def flat_map!(&block)
        replace(flat_map(&block))
      end
    end
  }
  
  # ...
end

@tagomoris (Satoshi TAGOMORI) proposed an idea to reduce indentation and remove Module.new { ... }. This looks pretty convenient, but I want to write do ... end, which would make it a block of using here, because we almost always use ... end for defining methods or modules.

module Kernel
  def refined(mod, &block)
    Module.new do
      refine(mod, &block)
    end
  end
end

class Foo
  using refined(Array) {
    def flat_map!(&block)
      replace(flat_map(&block))
    end
  }

  # ...
end

Proposal

How about supporting this? Because using currently doesn't take a block, it doesn't conflict with the existing syntax.

class Foo
  using refined: Array do
    def flat_map!(&block)
      replace(flat_map(&block))
    end
  end

  # ...
end

This syntax is based on ideas of @tagomoris (Satoshi TAGOMORI) and @znz (Kazuhiro NISHIYAMA) .


Related issues 1 (1 open0 closed)

Is duplicate of Ruby master - Feature #16241: Shorter syntax for anonymous refinementsOpenActions
Actions

Also available in: Atom PDF