Another reason to know Ruby better

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

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

Phillip K. schrieb:

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

or just use:

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

Martin

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