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