From: Gennady Bystritsky Date: 2012-06-21T16:49:22+09:00 Subject: Re: New to Ruby - Scope Question On Jun 21, 2012, at 12:39 AM, Emeka Patrick wrote: > x = 10 > y = 5 > > def square(x) > puts "within the method x is " + x.to_s > puts x*x > end Here, you define method square but do not invoke it yet. 'x' here is a method parameter that will take the value provided in a method invocation. It does not have anything to do with 'x' in 'x = 10'. For example, when you invoke this method as: square(25) x in x.to_s and x*x will be 25. And outer scope x will still be 10 after the method returns. Gennady. > > > In the above when defining the method, the x that is being referenced in > "x.to_s" and puts x*x is by default the variable x, defined above, with > a value of 10, correct? This then means that even though this variable > isn't defined within the method it can still be called on from outside > the code. However, if the variable was given a value within the method > then it wouldn't be available outside of the method, correct? > > Can someone explain why this is so. I guess I don't NEED to know why, > but I'd like to understand it a bit better if possible. Thanks! > > -- > Posted via http://www.ruby-forum.com/. >