# Methods and scope - (noob alert)

**URL:** <https://rubytalk.org/t/methods-and-scope-noob-alert/43909>\
**Category:** ruby-talk\
**Created:** [20 January 2008 13:56 UTC](https://rubytalk.org/t/methods-and-scope-noob-alert/43909 "2008-01-20T13:56:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Maclean2](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@John\_Maclean2](https://rubytalk.org/u/John_Maclean2)\
**Post date:** [20 January 2008 13:56 UTC](https://rubytalk.org/t/methods-and-scope-noob-alert/43909/1 "2008-01-20T13:56:07Z")

</div>

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

def add\_two\_num  
&nbsp;&nbsp;p "gimme two numbers. first : "  
&nbsp;&nbsp;x = gets.chomp  
&nbsp;&nbsp;p "second : "  
&nbsp;&nbsp;y = gets.chomp  
&nbsp;&nbsp;out = x.to\_i + y.to\_i  
&nbsp;&nbsp;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 : "  
&nbsp;&nbsp;ask = gets.chomp  
&nbsp;&nbsp;n = ask.to\_i

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

test\_gets\_positive

wtf has happened to n?

Regards,

John Maclean  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![Andrew\_Timberlake](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Andrew\_Timberlake](https://rubytalk.org/u/Andrew_Timberlake)\
**Post date:** [20 January 2008 15:10 UTC](https://rubytalk.org/t/methods-and-scope-noob-alert/43909/2 "2008-01-20T15:10:50Z")

</div>

> def  
> test\_gets\_positive p "gimme a number : "  
> &nbsp;&nbsp;ask = gets.chomp  
> &nbsp;&nbsp;n = ask.to\_i
> 
> &nbsp;&nbsp;case  
> &nbsp;&nbsp;&nbsp;&nbsp;when n \< 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "this number is -ve. i ain't gonna play no more! STOP!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;&nbsp;&nbsp;when n = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;print " this number is +ve! this it\'s square:"  
> &nbsp;&nbsp;&nbsp;&nbsp;p n\*n  
> &nbsp;&nbsp;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 Timberlake  
andrew@andrewtimberlake.com  
082 415 8283  
skype: andrewtimberlake

"I have never let my schooling interfere with my education."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Mark Twain

---

<div class="post-metadata">

**Author:** ![John\_Maclean2](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@John\_Maclean2](https://rubytalk.org/u/John_Maclean2)\
**Post date:** [20 January 2008 15:33 UTC](https://rubytalk.org/t/methods-and-scope-noob-alert/43909/3 "2008-01-20T15:33:40Z")

</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  
&nbsp;&nbsp;p "enter a digit : "  
&nbsp;&nbsp;gets.chomp  
&nbsp;&nbsp;number = $\_.to\_i  
&nbsp;&nbsp;puts number  
&nbsp;&nbsp;p number.class

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

> **···**
>
> On Mon, 21 Jan 2008 00:10:50 +0900 "Andrew Timberlake" \<andrew@andrewtimberlake.com\> wrote:
> 
> > \> 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?  
> > \>
> > 
> > In this code the line:  
> > when n = 0  
> > looks wrong  
> > Shouldn't it be:  
> > when n \>= 0
> > 
> > Andrew Timberlake  
> > andrew@andrewtimberlake.com  
> > 082 415 8283  
> > skype: andrewtimberlake
> > 
> > "I have never let my schooling interfere with my education."  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Mark Twain
> 
> Regards,
> 
> John Maclean  
> MSc (DIC)  
> +44 7739 171 531

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [21 January 2008 00:04 UTC](https://rubytalk.org/t/methods-and-scope-noob-alert/43909/4 "2008-01-21T00:04:57Z")

</div>

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

...

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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".)

> **···**
>
> On Jan 20, 8:33 am, John Maclean \<j...@jayeola.org\> wrote:
