# Why do I get undefined method?

**URL:** <https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479>\
**Category:** Ruby\
**Created:** [July 13, 2010, 11:12am UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479 "2010-07-13T11:12:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:12am UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/1 "2010-07-13T11:12:47Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:22am UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/2 "2010-07-13T11:22:13Z")

</div>

On Tuesday 13 July 2010, Abder-Rahman A. wrote:

> |  
> |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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:23am UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/3 "2010-07-13T11:23:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:33am UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/4 "2010-07-13T11:33:21Z")

</div>

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

> 

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 G.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 12:16pm UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/5 "2010-07-13T12:16:46Z")

</div>

Thanks for your replies. I changed the script to look as follows:  
[http://pastie.org/private/qwcmcpdzvxybm52s3ywxqg](http://pastie.org/private/qwcmcpdzvxybm52s3ywxqg)

But, I ALWAYS get “Out of range…”. Why is that?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/6 "2010-07-13T13:30:09Z")

</div>

Peter H. 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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 1:43pm UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/7 "2010-07-13T13:43:53Z")

</div>

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

> 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 B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/8 "2010-07-13T14:00:33Z")

</div>

Thanks Rob. Yes, the issue seemed .to\_i wasn’t there.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/why-do-i-get-undefined-method/191479/9 "2010-07-13T12:26:38Z")

</div>

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
