# Can't change the value of self - II

**URL:** https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596
**Category:** ruby-talk
**Created:** [11 August 2010 23:44 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596 "2010-08-11T23:44:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)
#### Post date: [11 August 2010 23:44 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/1 "2010-08-11T23:44:27Z")

</div>

I'm still not "getting it."

Consider

- - -  
class DirectoryString \< String  
&nbsp;&nbsp;def initialize(arg)  
&nbsp;&nbsp;&nbsp;&nbsp;super arg  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;def convert\_forward\_slash\_to\_back\_slash  
&nbsp;&nbsp;&nbsp;&nbsp;self.gsub(/\//, "\\")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def convert\_forward\_slash\_to\_back\_slash!  
&nbsp;&nbsp;&nbsp;&nbsp;self.gsub!(/\//, "\\")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def remove\_trailing\_backslash\_if\_any  
&nbsp;&nbsp;&nbsp;&nbsp;self.chomp("\\")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def remove\_trailing\_backslash\_if\_any!  
&nbsp;&nbsp;&nbsp;&nbsp;self.chomp!("\\")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def append\_trailing\_backslash\_if\_needed  
&nbsp;&nbsp;&nbsp;&nbsp;remove\_trailing\_backslash\_if\_any + "\\"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def append\_trailing\_backslash\_if\_needed!  
&nbsp;&nbsp;&nbsp;&nbsp;# ??? What do I do here ??? … The following is illegal  
&nbsp;&nbsp;&nbsp;&nbsp;self = "Help!"  
&nbsp;&nbsp;end  
end

- - -

The only thing I can think of is converting from the standard IS\_A (Inheritance) to a HAS\_A (create a member variable).

If I do the latter, above, I will have to create a ton of simple "forwarding" messages.

What's the Ruby way of doing this sort of thing?

---

<div class="post-metadata">

### Author: ![jeremy\_Ruten](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jeremy\_Ruten](https://rubytalk.org/u/jeremy_Ruten)
#### Post date: [11 August 2010 23:52 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/2 "2010-08-11T23:52:57Z")

</div>

You can use the #replace method to do that sort of thing:

&nbsp;&nbsp;&nbsp;&nbsp;self.replace "Help! Oh, well, I'm saved!"

This method is defined in classes String, Hash, Array, and Set, and  
works the same way in each one.

jeremy

> **···**
>
> On Wed, Aug 11, 2010 at 5:44 PM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> 
> > I'm still not "getting it."
> > 
> > Consider
> > 
> > - - -  
> > class DirectoryString \< String  
> > def initialize(arg)  
> > super arg  
> > end
> > 
> > def convert\_forward\_slash\_to\_back\_slash  
> > self.gsub(/\//, "\\")  
> > end
> > 
> > def convert\_forward\_slash\_to\_back\_slash!  
> > self.gsub!(/\//, "\\")  
> > end
> > 
> > def remove\_trailing\_backslash\_if\_any  
> > self.chomp("\\")  
> > end
> > 
> > def remove\_trailing\_backslash\_if\_any!  
> > self.chomp!("\\")  
> > end
> > 
> > def append\_trailing\_backslash\_if\_needed  
> > remove\_trailing\_backslash\_if\_any + "\\"  
> > end
> > 
> > def append\_trailing\_backslash\_if\_needed!  
> > # ??? What do I do here ??? ... The following is illegal  
> > self = "Help!"  
> > end  
> > end
> > 
> > - - -
> > 
> > The only thing I can think of is converting from the standard IS\_A (Inheritance) to a HAS\_A (create a member variable).
> > 
> > If I do the latter, above, I will have to create a ton of simple "forwarding" messages.
> > 
> > What's the Ruby way of doing this sort of thing?

---

<div class="post-metadata">

### Author: ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)
#### Post date: [12 August 2010 00:12 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/3 "2010-08-12T00:12:44Z")

</div>

Wednesday, August 11, 2010, 5:52:57 PM, you wrote:

> You can use the #replace method to do that sort of thing:

> &nbsp;&nbsp;&nbsp;&nbsp;self.replace "Help! Oh, well, I'm saved!"

> This method is defined in classes String, Hash, Array, and Set, and  
> works the same way in each one.

> jeremy

jeremy,

That's great and solves my particular problem.

But what if the class being derived from does not have a replace method?

Ralph

---

<div class="post-metadata">

### Author: ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)
#### Post date: [12 August 2010 00:29 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/4 "2010-08-12T00:29:03Z")

</div>

If the object has state that can be modified, then there will be (by  
definition) ways to modify that state. If it doesn't, then there won't  
be, and the class in question is probably a bad starting point if you  
want to create objects with state that can be modified.

That's one of the advantages of using proxy objects and delegators: you  
gain an extra axis along which you can make decisions about things like  
object state. Even though you can't change (say) a Fixnum, you can  
create objects with integer attributes that \*can\* be changed.

David

> **···**
>
> On Thu, 12 Aug 2010, Ralph Shnelvar wrote:
> 
> > Wednesday, August 11, 2010, 5:52:57 PM, you wrote:
> > 
> > \> You can use the #replace method to do that sort of thing:
> > 
> > \> self.replace "Help! Oh, well, I'm saved!"
> > 
> > \> This method is defined in classes String, Hash, Array, and Set, and  
> > \> works the same way in each one.
> > 
> > \> jeremy
> > 
> > jeremy,
> > 
> > That's great and solves my particular problem.
> > 
> > But what if the class being derived from does not have a replace method?
> 
> --  
> David A. Black, Senior Developer, Cyrus Innovation Inc.
> 
> &nbsp;&nbsp;&nbsp;The Ruby training with Black/Brown/McAnally  
> &nbsp;&nbsp;&nbsp;Compleat Philadelphia, PA, October 1-2, 2010  
> &nbsp;&nbsp;&nbsp;Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

### Author: ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)
#### Post date: [12 August 2010 00:38 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/5 "2010-08-12T00:38:10Z")

</div>

David,

> > That's great and solves my particular problem.

> > But what if the class being derived from does not have a replace method?

> If the object has state that can be modified, then there will be (by  
> definition) ways to modify that state. If it doesn't, then there won't  
> be, and the class in question is probably a bad starting point if you  
> want to create objects with state that can be modified.

> That's one of the advantages of using proxy objects and delegators: you  
> gain an extra axis along which you can make decisions about things like  
> object state. Even though you can't change (say) a Fixnum, you can  
> create objects with integer attributes that \*can\* be changed.

Could you expand on this with an example, please?

---

<div class="post-metadata">

### Author: ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)
#### Post date: [12 August 2010 07:21 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/6 "2010-08-12T07:21:57Z")

</div>

irb(main):001:0\> require 'delegate'  
=\> true  
irb(main):002:0\> class MutableFixnum \< Delegator  
irb(main):003:1\> def initialize i  
irb(main):004:2\> super  
irb(main):005:2\> @value = i  
irb(main):006:2\> end  
irb(main):007:1\> def \_\_getobj\_\_  
irb(main):008:2\> @value  
irb(main):009:2\> end  
irb(main):014:1\> def \_\_setobj\_\_ value  
irb(main):015:2\> @value = value  
irb(main):016:2\> end  
irb(main):017:1\> def add! x  
irb(main):018:2\> \_\_setobj\_\_(\_\_getobj\_\_ + x)  
irb(main):019:2\> end  
irb(main):020:1\> end  
=\> nil  
irb(main):021:0\> a = MutableFixnum.new 1  
=\> 1  
irb(main):022:0\> a + 3  
=\> 4  
irb(main):023:0\> a.add! 3  
=\> 4  
irb(main):024:0\> a  
=\> 4

Jesus.

> **···**
>
> On Thu, Aug 12, 2010 at 2:38 AM, Ralph Shnelvar \<ralphs@dos32.com\> wrote:
> 
> > David,
> > 
> > > > That's great and solves my particular problem.
> > 
> > > > But what if the class being derived from does not have a replace method?
> > 
> > \> If the object has state that can be modified, then there will be (by  
> > \> definition) ways to modify that state. If it doesn't, then there won't  
> > \> be, and the class in question is probably a bad starting point if you  
> > \> want to create objects with state that can be modified.
> > 
> > \> That's one of the advantages of using proxy objects and delegators: you  
> > \> gain an extra axis along which you can make decisions about things like  
> > \> object state. Even though you can't change (say) a Fixnum, you can  
> > \> create objects with integer attributes that \*can\* be changed.
> > 
> > Could you expand on this with an example, please?

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [12 August 2010 08:00 UTC](https://rubytalk.org/t/cant-change-the-value-of-self-ii/59596/7 "2010-08-12T08:00:06Z")

</div>

Ralph Shnelvar wrote:

> Could you expand on this with an example, please?

[Just to show that delegation is nothing magic, and you don't need to  
use the Delegator class]

class MyCounter  
&nbsp;&nbsp;def initialize(n=0)  
&nbsp;&nbsp;&nbsp;&nbsp;@n = n  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def inc!  
&nbsp;&nbsp;&nbsp;&nbsp;@n += 1  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def to\_int  
&nbsp;&nbsp;&nbsp;&nbsp;@n  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;@n.to\_s  
&nbsp;&nbsp;end  
end

c = MyCounter.new  
c.inc!  
c.inc!  
puts "c is #{c}"

Some more work is required to make c duck-type like an Integer though.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
