# 99.respond\_to? :dup ==\> true

**URL:** <https://www.ruby-forum.com/t/99-respond-to-dup-true/157567>\
**Category:** Ruby\
**Created:** [January 13, 2009, 8:07am UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567 "2009-01-13T08:07:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [January 13, 2009, 8:07am UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567/1 "2009-01-13T08:07:26Z")

</div>

I have values of various datatypes coming into this method and the  
method does a dup, since it modifies the values and obviously does not  
want to change the original.

However, Fixnum’s do respond to :dup but give an error when called.  
First off, can someone explain this to me.  
Second, i have worked around it with a “rescue” clause.

In future, should I avoid using “respond\_to?” since it could cause  
errors. Should I always use rescue.  
I now have:

value = value.dup rescue value

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [January 13, 2009, 8:36am UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567/2 "2009-01-13T08:36:49Z")

</div>

2 points here (and sorry for the top posting – my web mail client does  
not quote properly):

1. It may be the case that if an object is not dup()-able, you better  
not quietly continue with an original object as modifying it may cause  
undesired side effects. In this case, an exception from calling dup(),  
either NoMethodError or TypeError (can’t dup Fixnum), is a valid reason  
to report a datatype error.

2. Instead of dup()-ing, you may consider using a proxy class that will  
delegate to the original object, yet keeping track of all modifications.  
It will work even for data types that do not support dup().

Best,  
Gennady.

* * *

From: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid]  
Sent: Monday, January 12, 2009 23:06  
To: ruby-talk ML  
Subject: 99.respond\_to? :dup ==\> true

I have values of various datatypes coming into this method and the  
method does a dup, since it modifies the values and obviously does not  
want to change the original.

However, Fixnum’s do respond to :dup but give an error when called.  
First off, can someone explain this to me.  
Second, i have worked around it with a “rescue” clause.

In future, should I avoid using “respond\_to?” since it could cause  
errors. Should I always use rescue.  
I now have:

value = value.dup rescue value

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [January 13, 2009, 8:44am UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567/3 "2009-01-13T08:44:39Z")

</div>

Gennady B. wrote:

> 2 points here (and sorry for the top posting – my web mail client does  
> not quote properly):
> 
> 1. It may be the case that if an object is not dup()-able, you better  
> not quietly continue with an original object as modifying it may cause  
> undesired side effects. In this case, an exception from calling dup(),  
> either NoMethodError or TypeError (can’t dup Fixnum), is a valid reason  
> to report a datatype error.
> 
> 2. Instead of dup()-ing, you may consider using a proxy class that will  
> delegate to the original object, yet keeping track of all modifications.  
> It will work even for data types that do not support dup().
> 
> Best,  
> Gennady.
> 
> * * *

Ouch! Your web client has exposed my email id to the spammers ☹  
Anyway,… no, here i do not want the original value touched since the  
user may press Escape and abandon editing.

In the case of numbers and floats, usually its a call by value so a copy  
is made anyway.

Anyway, as a newbie, it was quite a surprise - i’ve relied on  
respond\_to? to take care of a lot of generic code.

thx.

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [January 13, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567/4 "2009-01-13T17:03:08Z")

</div>

Gennady B. wrote:

> First of all, it is not my web mail client to blame (it has lots of its  
> own faults) – if your e-mail were not present in the original message  
> no client would be able to expose it in reply anyway ;-).

Haha, the spam has begun !!! I guess I’ll have to move to a gmail  
account i/o gmx.

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [January 13, 2009, 9:05am UTC](https://www.ruby-forum.com/t/99-respond-to-dup-true/157567/5 "2009-01-13T09:05:35Z")

</div>

First of all, it is not my web mail client to blame (it has lots of its  
own faults) – if your e-mail were not present in the original message  
no client would be able to expose it in reply anyway ;-).

As had been mentioned many times on this list (in one form or another),  
respond\_to? is a lame duck of the duck typing world ;-). It only can  
indicate that an object has a method in its inheritance chain to invoke  
in response to a named message. I has no idea – and can’t have any,  
really – what that method would do for you and even how to properly  
invoke it.

Getting back to dup(), this method is defined for Object class, so every  
Ruby object, including Fixnum instances, will inherit it. On the other  
hand, there’s nothing to dup() for Fixnum – hence method redefinition  
to raise a TypeError.

Gennady.

* * *

From: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid]  
Sent: Monday, January 12, 2009 23:43  
To: ruby-talk ML  
Subject: Re: 99.respond\_to? :dup ==\> true

Gennady B. wrote:

> delegate to the original object, yet keeping track of all modifications.  
> It will work even for data types that do not support dup().
> 
> Best,  
> Gennady.
> 
> * * *

Ouch! Your web client has exposed my email id to the spammers ☹  
Anyway,… no, here i do not want the original value touched since the  
user may press Escape and abandon editing.

In the case of numbers and floats, usually its a call by value so a copy  
is made anyway.

Anyway, as a newbie, it was quite a surprise - i’ve relied on  
respond\_to? to take care of a lot of generic code.

thx.
