Public, Private, Instance, Global, Class


#1

I'm just a bit watery in general on the subject of scope, and I probably have many questions.

However, the one that jumps out at me, and that which I feel will get the ball rolling is this:

 When defining a private method, wouldn't all variables declared therein inherit the privacy of their context? As such, can't there arise some redundancies and/or cancellations in scope?

#2

When defining a private method, wouldn't all variables declared therein inherit the privacy of their context? Yes, unless defined otherwise, all variables declared inherit the privacy of their context. I believe that your other question is What happens when you make another variable with the same name and scope? (correct?) The variable will have the value that the last assignment gave it.


#3

The variable will have the value that the last assignment gave it.

In this case, "the last assignment" being that made upon creating this same-named variable for the second (or further) time?


#4

Yes, except any variable call above "the last assignment" will use the previous value.

i.e.

def puts_a_variable()
    puts a_variable
end
a_variable = true
puts a_variable #this will print true, because it is above the assignment to false
puts_a_variable() #this will also print true, because you called the method above the assignment to false
a_variable = false
puts a_variable #this will print false
puts_a_variable() #this will print false, because you called the method below the assignment to false