[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Methods and scope - (noob alert

John Maclean

1/20/2008 1:56:00 PM

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 Maclean
MSc (DIC)
+44 7739 171 531

3 Answers

Andrew Timberlake

1/20/2008 3:11:00 PM

0

> 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."
--Mark Twain



John Maclean

1/20/2008 3:34:00 PM

0

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 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."
> --Mark Twain
>
>
>




Regards,

John Maclean
MSc (DIC)
+44 7739 171 531

Phrogz

1/21/2008 12:01:00 AM

0

On Jan 20, 8:33 am, John Maclean <j...@jayeola.org> 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".)