https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112009-10-17T04:52:54ZRuby Issue Tracking SystemRuby master - Bug #2221: lib/delegate: freeze has odd effectshttps://redmine.ruby-lang.org/issues/2221?journal_id=62762009-10-17T04:52:54Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>=begin<br>
Hi,</p>
<p>On Fri, Oct 16, 2009 at 7:24 AM, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>I think both should be frozen. I checked in the patch. I'd like to<br>
see if it works (or not).</p>
</blockquote>
<p>Indeed, I thought about it and came to the same conclusion when thinking of other non-trivial classes that would extend Delegate. It was too cold outside my bed though :-)</p>
<p>(a) Just to be sure: SimpleDelegator([1,2,3].freeze).frozen? should return false, right?</p>
<p>(b) Still can't clone it, though.<br>
Is there a nice way to avoid the exception? The best I can think of is the inelegant:</p>
<p>diff --git a/lib/delegate.rb b/lib/delegate.rb<br>
index 5d25632..5ef9c1c 100644<br>
--- a/lib/delegate.rb<br>
+++ b/lib/delegate.rb<br>
@@ -188,11 +188,14 @@ class Delegator<br>
<strong>setobj</strong>(obj)<br>
end</p>
<ul>
<li>alias_method :<strong>original_dup</strong>, :dup</li>
<li>private :<strong>original_dup</strong>
</li>
<li>
<a name="Clone-support-for-the-object-returned-by-__getobj__"></a>
<h1 >Clone support for the object returned by __getobj__.<a href="#Clone-support-for-the-object-returned-by-__getobj__" class="wiki-anchor">¶</a></h1>
def clone</li>
</ul>
<ul>
<li>new = super</li>
</ul>
<ul>
<li>new = frozen? ? <strong>original_dup</strong> : super<br>
new.<strong>setobj</strong>(<strong>getobj</strong>.clone)</li>
</ul>
<ul>
<li>new</li>
</ul>
<ul>
<li>frozen? ? new.freeze : new<br>
end<br>
# Duplication support for the object returned by __getobj__.<br>
def dup</li>
</ul>
<p>=end</p> Ruby master - Bug #2221: lib/delegate: freeze has odd effectshttps://redmine.ruby-lang.org/issues/2221?journal_id=62862009-10-18T10:40:36Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>=begin<br>
Hi,</p>
<p>In message "Re: <a href="/issues/2221">[ruby-core:26118]</a> [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: lib/delegate: freeze has odd effects (Closed)" href="https://redmine.ruby-lang.org/issues/2221">#2221</a>] lib/delegate: freeze has odd effects"<br>
on Fri, 16 Oct 2009 15:43:40 +0900, Marc-Andre Lafortune <a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a> writes:</p>
<p>|Freeze doesn't quite freeze, and can't be cloned:</p>
<p>|But maybe the Delegator itself should also be frozen? I mean should a call to #<strong>setobj</strong> raise a runtime error or not?</p>
<p>I think both should be frozen. I checked in the patch. I'd like to<br>
see if it works (or not).</p>
<p>|Also, there are other methods that should probably be forwarded (eql?, hash, etc...); see issue <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Delegator < BasicObject (Rejected)" href="https://redmine.ruby-lang.org/issues/1333">#1333</a>.</p>
<p>I have tried once, but code became vary clumsy, mostly because no<br>
constant can be accessed or initialized from BasicObject.</p>
<pre><code> matz.
</code></pre>
<p>=end</p> Ruby master - Bug #2221: lib/delegate: freeze has odd effectshttps://redmine.ruby-lang.org/issues/2221?journal_id=102482010-04-17T00:09:18Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>=begin<br>
Hi,</p>
<p>This ticket was duplicated by <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: rubyspec: SimpleDelegator when frozen creates a frozen clone ERROR (Closed)" href="https://redmine.ruby-lang.org/issues/2679">#2679</a>.<br>
After that, lib/delegate.rb has been changed bit by bit.<br>
As a result of matz and nobu's effort, this seems to work now.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a><br>
=end</p>