Memory allocation in ruby
(written by Lawrence Krubner, however indented passages are often quotes)
But the real surprise?
Okay, so I started this by saying there’s a big difference in a ||= b vs. a = a || b and that is only sort of true. Yes you’re calling a setter every time (setting a to a) in the second example but did you know redeclaring a variable to itself does not reinitialize it in memory?
And did you know that when setting a variable to another variable, you’re actually creating a pointer to the original object in memory?
Let’s run another experiment.
s = “John”
s.object_id
d = s
d.object_id
See that? Good, now, need some real proof on how handling these objects in memory can matter to you?
s << “smith”
puts s
puts d
Yeah, so, when I said you were doing concatenation wrong, I meant it. There’s concatenation and there’s copying. Sometimes you want to copy, so make sure which one you mean to use.
I’d say that for every language, remembering when you are copying by value, versus copying by reference, is crucial to avoiding bugs. And it’s one of the toughest things that I struggle to remember when I move from one language to another.
Source
May 17, 2012 2:06 am
From free cell phone ringtones on MySql Workbench is a total waste of time
"I like it so much, http://dailybooth.com/freecellphoneringto free cell phone ringtones, jsneke,..."