Project

General

Profile

Feature #14778

Make Object#dup accept a block

Added by hkdnet (Ko Sato) over 1 year ago. Updated over 1 year ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:87197]

Description

I'd like to use Object#dup with a block, which is equivalent to dup.tap.

An example for dup.tap is here.

require 'uri'

module MyUri
  class << self
    def foo_uri
      base_uri.dup.tap { |e| e.path = '/foo' }
    end

    private

    def base_uri
      @base_uri ||= URI.parse('http://example.com')
    end
  end
end

With this patch, we can write base_uri.dup { |e| e.path = '/foo' } and it's simpler.


Files

dup_with_block.diff (296 Bytes) dup_with_block.diff hkdnet (Ko Sato), 05/20/2018 01:14 AM

Related issues

Related to Ruby master - Feature #12092: Allow Object#clone to yield cloned object before freezingRejectedActions

History

Updated by shyouhei (Shyouhei Urabe) over 1 year ago

  • Status changed from Open to Feedback

Not an immediate NG, but curious why we should do this in core, instead of your gem?

Would you mind telling us the reason this should be enabled for all the ruby users around the world?

#2

Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Related to Feature #12092: Allow Object#clone to yield cloned object before freezing added

Also available in: Atom PDF