Project

General

Profile

Feature #14778

Make Object#dup accept a block

Added by hkdnet (Ko Sato) 4 months ago. Updated 4 months 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.

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

Related issues

Related to Ruby trunk - Feature #12092: Allow Object#clone to yield cloned object before freezingRejected

History

#1 [ruby-core:87212] Updated by shyouhei (Shyouhei Urabe) 4 months 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) 4 months ago

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

Also available in: Atom PDF