Hey all,
I guess this should go on the Ruby-list, but I failed to find what
it’s called, so here goes.
This might be a bit of a stupid question, but I would like to
understand why what causes this behaviour:
params[‘string’] = “foo_bar”
@baz = params[‘string’]
@baz.gsub!(/[_]/, ’ ') #replace underscore with space
puts params[‘string’] # => “foo bar”
Why whould gsub!() alter the value in params? Same goes if I copy it
again and as far as I can tell even if variables doesn’t have the
@-sign or using any other string operator ending with an explamation
point.
Does my equal sign make references rather then copies? Is there only
some occasions when that is true?
Thanks in advance,
Mathias.
Mathias W. wrote:
Hey all,
I guess this should go on the Ruby-list, but I failed to find what
it’s called, so here goes.
This might be a bit of a stupid question, but I would like to
understand why what causes this behaviour:
params[‘string’] = “foo_bar”
@baz = params[‘string’]
@baz.gsub!(/[_]/, ’ ') #replace underscore with space
puts params[‘string’] # => “foo bar”
Why whould gsub!() alter the value in params? Same goes if I copy it
again and as far as I can tell even if variables doesn’t have the
@-sign or using any other string operator ending with an explamation
point.
Does my equal sign make references rather then copies? Is there only
some occasions when that is true?
Thanks in advance,
Mathias.
In ruby, usually, when you assign one variable to another, both
variables point to the same value. You can use the object_id method to
test this. objects with the same object_id are the same object.
a = ‘foo’
a.object_id #=> 964138
b = ‘foo’
b.object_id #=> 935788
b = a
b.object_id #=> 964138
You can usually call the dup method to create a new copy of any object
a = b
a.object_id == b.object_id #=> true
a = b.dup
a.object_id == b.object_id #=> false
On 11/22/06, Mathias W. [email protected]
wrote:
Hey all,
I guess this should go on the Ruby-list, but I failed to find what
it’s called, so here goes.
IMO the best place for ruby discussions is comp.lang.ruby on usenet.
For instance through
http://groups-beta.google.com/group/comp.lang.ruby/topics.
Does my equal sign make references rather then copies? Is there only
some occasions when that is true?
See 4.1 here http://www.rubycentral.com/faq/rubyfaq.html.
Isak