# Methods and scope - (noob alert)

**URL:** <https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620>\
**Category:** Ruby\
**Created:** [January 20, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620 "2008-01-20T14:56:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 20, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620/1 "2008-01-20T14:56:37Z")

</div>

This block of code works and it’s easy to see why:-

def add\_two\_num  
p "gimme two numbers. first : "  
x = gets.chomp  
p "second : "  
y = gets.chomp  
out = x.to\_i + y.to\_i  
p out  
end

add\_two\_num

This next lot --doesn’t-- and I get the feeling that it has something to  
do with scope, maybe within the case block. Am I right?

def  
test\_gets\_positive p "gimme a number : "  
ask = gets.chomp  
n = ask.to\_i

case  
when n \< 0  
puts “this number is -ve. i ain’t gonna play no more! STOP!”  
exit  
when n = 0  
print " this number is +ve! this it’s square:"  
p n\*n  
end  
end

test\_gets\_positive

wtf has happened to n?

Regards,

John M.  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 20, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620/2 "2008-01-20T16:12:15Z")

</div>

> ```
> print " this number is +ve! this it\'s square:"
> p n*n
> 
> ```
> 
> end  
> end
> 
> test\_gets\_positive
> 
> wtf has happened to n?

In this code the line:  
when n = 0  
looks wrong  
Shouldn’t it be:  
when n \>= 0

Andrew T.  
removed\_email\_address@domain.invalid  
082 415 8283  
skype: andrewtimberlake

“I have never let my schooling interfere with my education.”  
–Mark Twain

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 20, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620/3 "2008-01-20T16:34:34Z")

</div>

Sheesh! All is working now and I can move along 😉 ty! This is all that  
I wanted to do:-

def grab\_spit\_number\_case  
p "enter a digit : "  
gets.chomp  
number = $\_.to\_i  
puts number  
p number.class

case  
when number \< 0  
p “this number is -ve. can’t play: STOP!”  
when number == 0  
p “can't do much with a Zero. You sure you gave me a +ve number?”  
when number \> 0  
p “this is your number: #{number} and this is it’s  
square#{number\*number}”  
else p “what number did you pass to me exactly? can’t seem to  
determine it”  
end  
end

grab\_spit\_number\_case

On Mon, 21 Jan 2008 00:10:50 +0900  
“Andrew T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > print " this number is +ve! this it\'s square:"
> > 
> > ```
> 
> when n = 0  
> –Mark Twain

Regards,

John M.  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2008, 1:07am UTC](https://www.ruby-forum.com/t/methods-and-scope-noob-alert/126620/4 "2008-01-21T01:07:06Z")

</div>

On Jan 20, 8:33 am, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sheesh! All is working now and I can move along 😉 ty! This is all that I wanted to do:-  
> …  
> p “this is your number: #{number} and this is it’s square#{number\*number}”

FYI, the word “it’s” is only used for the contraction of “it is”; the  
possessive form is always simply “its”. Understandable confusion,  
since the possessive of most other nouns does use the apostrophe. (As  
in “the dog’s house” or “Phrogz’s annoying English pedantry”.)
