From: Peter Hickman on
year = gets

returns a string but your function requires a fixnum

so try

year = gets.to_i

this is one of the few times that you have to explicitly convert
types. It doesn't happen alot in ruby and each time it catches me out

From: Abder-Rahman Ali on
Peter Hickman wrote:
> year = gets
>
> returns a string but your function requires a fixnum
>
> so try
>
> year = gets.to_i
>
> this is one of the few times that you have to explicitly convert
> types. It doesn't happen alot in ruby and each time it catches me out

Thanks a lot Peter, that solves it. And, thanks for you other for your
replies.
--
Posted via http://www.ruby-forum.com/.

From: Rob Biedenharn on

On Jul 13, 2010, at 6:16 AM, Abder-Rahman Ali wrote:

> Thanks for your replies. I changed the script to look as follows:
> http://pastie.org/private/qwcmcpdzvxybm52s3ywxqg
>
> But, I ALWAYS get "Out of range...". Why is that?
>
> Thanks.
>
> def what_were_you_doing_on (year)
> if (2001..2006).include?(year)
> puts "BSc"
> elsif (2007..2009).include?(year)
> puts "MSc"
> else
> puts "Out of range..."
> end
> end
>
> puts "Enter year "
> year = gets
> what_were_you_doing_on (year)

Because gets returns a string (which will still have a newline). So
you're asking:
(2001..2006).include?("2003\n")

Try: year = gets.to_i

-Rob

Rob Biedenharn
Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab(a)GaslightSoftware.com http://GaslightSoftware.com/


From: Abder-Rahman Ali on
Thanks Rob. Yes, the issue seemed .to_i wasn't there.
--
Posted via http://www.ruby-forum.com/.