On 5/15/06, corey konrad <0011 / hush.com> wrote:
> excerpt from his book:
> In fact, two variables in that little program are named tough_var:
> one inside little_pest and one outside of it. They don't communicate.
> They aren't related. They aren't even friends. When we called
> little_pest tough_var, we really just passed the string from one
> tough_var to the other (via the method call, the only way they can even
> sort of communicate) so that both were pointing to the same string.
> Then little_pest pointed its own local tough_var to nil, but that
> did nothing to the tough_var variable outside the method.

Here's something that's bothered the hell out of me.  Can someone
please explain it to me?

def major_pest(wussy_var)
  wussy_var.slice!(0..-1)
  puts "HAHA! I ruined your variable!"
end

wuss_var = "I'm about to get owned"
major_pest wuss_var
puts wuss_var

wuss_var is "" after major_pest returns.  So apparently you can affect
it.  The only way (that I know of) to write that method without
affecting the outside variable is

def major_pest(wussy_var)
  guardian_angel = wussy_var.clone
  guardian_angel.slice!(0..-1)
  puts "HAHA! I ruined your variable!"
end

Anyway I guess that example merely serves to show method scope?
Changing the variable named tough_var inside the method doesn't do
anything to the one outside the method.  The object that tough_var
points to inside the method is the same object that the external
tough_var points to, so when you modify the object inside the method,
those changes are seen outside of it as well.

Pat