From: corey konrad <0011@...> Date: 2006-05-16T09:27:21+09:00 Subject: Re: learn to program oh ok he is just talking about variable scope it was kind of hard to tell from his example for some resaon, i dont know i think i am going to skip this learn to program book, this chris guy really doesnt know how to explain things to a person with a beginner mind set. i also am having problems running his code examples i really dont understand how the code below works but when i copy and paste it into the live ruby program all it does is close the live ruby program and shuts it down. def ask question good_answer = false while (not good_answer) puts question reply = gets.chomp.downcase if (reply == ' yes' or reply == ' no' ) good_answer = true if reply == ' yes' answer = true else answer = false end else puts ' Please answer "yes" or "no".' end end answer # This is what we return (true or false). end Natevw wrote: > corey konrad wrote: >> puts tough_var >> 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. >> > > Looks like he's trying to get at the "scope" of variables. > > I think that his example might be more clearly written with parentheses: > > [code] > 1: def little_pest(tough_var) > 2: tough_var = nil > 3: puts ' HAHA! I ruined your variable!' > 4: end > 5: > 6: tough_var = ' You can\' t even touch my variable!' > 7: little_pest(tough_var) > 8: puts tough_var > [/code] > > Pay attention to 'tough_var' through the code, it actually refers to two > different variables: > 1) in line 1, 'tough_var' is the name given to the one argument (~=input > value) that the little_pest function takes. It is a local variable. > in line 2, this local variable is set to nil. > > 2) in line 6, a global variable named 'tough_var' is declared and set to > the "You can't touch..." line > in line 7, little_pest is called with the value of the global > tough_var > > > So when tough_var is set to nil in line 2, all that is being set to nil > is the local variable named 'tough_var', not the variable from lines > 6-8. > > > To the Ruby interpretter, the code might as well be written as: > > 1: def little_pest(var1) > 2: var1 = nil > 3: puts ' HAHA! I ruined your variable!' > 4: end > 5: > 6: var2 = ' You can\' t even touch my variable!' > 7: little_pest(var2) > 8: puts var2 > > > > HTH...I'm just learning Ruby, so maybe somebody could correct the > details (passing by value,etc.)....but that's what the author is getting > at. > > > > -natevw -- Posted via http://www.ruby-forum.com/.