From: Abder-Rahman Ali on
I'm trying to write a script such that when you enter a year, you get
back a result of what occurred in this year or in the range of years, as
follows: http://pastie.org/private/0nibq5i7abwghydfu0yzg

The case is when I enter a year, I get the following:

sub.rb:3: undefined method `what_were_you_doing_on' for main:Object
(NoMethodError)

But, the method is there. Why am I getting this?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Stefano Crocco on
On Tuesday 13 July 2010, Abder-Rahman Ali wrote:
> |I'm trying to write a script such that when you enter a year, you get
> |back a result of what occurred in this year or in the range of years, as
> |follows: http://pastie.org/private/0nibq5i7abwghydfu0yzg
> |
> |The case is when I enter a year, I get the following:
> |
> |sub.rb:3: undefined method `what_were_you_doing_on' for main:Object
> |(NoMethodError)
> |
> |But, the method is there. Why am I getting this?
> |
> |Thanks.

Because you call the method (line 3) before defining it (line 6). Define the
method before calling it and the error will disappear.

Stefano

From: Peter Hickman on
year == 2001..2006

means is the variable 'year' is a range starting with 2001 up to and
including 2006

That is it expects that 'year' will be a range and not a fixnum.

What you need is something like this

if (2001..2006).include?(year)

From: Florian Gilcher on

On Jul 13, 2010, at 11:12 AM, Abder-Rahman Ali wrote:

> I'm trying to write a script such that when you enter a year, you get
> back a result of what occurred in this year or in the range of years, as
> follows: http://pastie.org/private/0nibq5i7abwghydfu0yzg
>
> The case is when I enter a year, I get the following:
>
> sub.rb:3: undefined method `what_were_you_doing_on' for main:Object
> (NoMethodError)
>
> But, the method is there. Why am I getting this?
>

Order is important. The method definition has to be evaluated before calling it.

If you put the call below the method definition, the call will work.

Regards,
Florian Gilcher
From: Abder-Rahman Ali on
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.
--
Posted via http://www.ruby-forum.com/.