# Another reason to know Ruby better

**URL:** <https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693>\
**Category:** Rails\
**Created:** [June 17, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693 "2008-06-17T17:15:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [June 17, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693/1 "2008-06-17T17:15:32Z")

</div>

We’ve heard the more knowledgeable of the community say that we should  
invest some time into learning and understanding Ruby in our quest to  
master Rails. This morning, I stumbled on a good reason to do so. So, to  
the ever growing list of reasons, I add this:

I have a hash of user-input values that I want to do some tests on and  
then save the original content once the tests pass. My original code  
looked like

value = a\_hash[key]  
value.gsub!(’-’, ‘’)  
value.gsub!(’$’, ‘’)  
etc

Imagine my surprise when I later wrote the contents of a\_hash[key] to  
the database, only to find that it had been changed! Some testing in  
script/console illustrates the situation:

> > h = {“1”=\>“2008-06-17”}  
> > =\> {“1”=\>“2008-06-17”}
> 
> > v = h[“1”]  
> > =\> “2008-06-17”
> 
> > v.gsub!(’-’, ‘’)  
> > =\> “20080617”
> 
> > v  
> > =\> “20080617”
> 
> > h  
> > =\> {“1”=\>“20080617”}

Now, a little thought about the fact that Ruby is object oriented, and  
therefore variables are probably just pointers, makes this sensible. But  
for those who are not accustomed to object oriented programming, or a  
language that is _fully_ object oriented (that’s my dilemma), this might  
not be so obvious. One of two methods on Object can be used to rectify  
the problem:

Object#dup:

> > h = {“1”=\>“2008-06-17”}  
> > =\> {“1”=\>“2008-06-17”}
> 
> > v = h[“1”].dup  
> > =\> “2008-06-17”
> 
> > v.gsub!(’-’, ‘’)  
> > =\> “20080617”
> 
> > h  
> > =\> {“1”=\>“2008-06-17”}
> 
> > v  
> > =\> “20080617”

or

Object#clone:

> > h = {“1”=\>“2008-06-17”}  
> > =\> {“1”=\>“2008-06-17”}
> 
> > v = h[“1”].clone  
> > =\> “2008-06-17”
> 
> > v.gsub!(’-’, ‘’)  
> > =\> “20080617”
> 
> > v  
> > =\> “20080617”
> 
> > h  
> > =\> {“1”=\>“2008-06-17”}

Do read the Ruby docs before using dup or clone, to make sure you  
understand what they are doing.

Maybe this post will save someone a little head scratching.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [June 17, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693/2 "2008-06-17T17:35:20Z")

</div>

On 17 Jun 2008, at 16:15, Phillip K. wrote:

> value = a\_hash[key]  
> value.gsub!(’-’, ‘’)  
> value.gsub!(’$’, ‘’)  
> etc
> 
> Imagine my surprise when I later wrote the contents of a\_hash[key] to  
> the database, only to find that it had been changed! Some testing in  
> script/console illustrates the situation:

An additional trick is to call freeze on things you don’t want people  
changing by accident.

Fred

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [June 17, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693/3 "2008-06-17T17:47:06Z")

</div>

Phillip K. schrieb:

> value.gsub!(’-’, ‘’)  
> value.gsub!(’$’, ‘’)  
> etc

or just use:

value = a\_hash[key].gsub(’-’, ‘’)  
value.gsub!(’$’, ‘’)  
etc.

Martin

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [June 17, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/another-reason-to-know-ruby-better/140693/4 "2008-06-17T18:29:07Z")

</div>

> An additional trick is to call freeze on things you don’t want people  
> changing by accident.
> 
> Fred

Thanks, Fred. I’ll keep that in mind for future use. It looks like it  
will work well in some situations, but not so well in others.

> or just use:
> 
> value = a\_hash[key].gsub(’-’, ‘’)  
> value.gsub!(’$’, ‘’)  
> etc.
> 
> Martin

I thought about something like that, Martin. The reason I didn’t go with  
something like that is the process can one 0…n steps on a value. The  
example I used was very simple and just a smidge of what I’m actually  
doing. In some cases, the first thing I might do is a gsub, in others it  
might be an attempt to case the value to a Date. So just grabbing it out  
initially is the most flexible thing to do.

But you’re certainly correct in that this would be a way to get around  
the problem.

Peace,  
Phillip
