Why do I get undefined method?

A

Abder-Rahman Ali

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

Stefano Crocco

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

Peter Hickman

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

Florian Gilcher

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
=20
The case is when I enter a year, I get the following:
=20
sub.rb:3: undefined method `what_were_you_doing_on' for main:Object
(NoMethodError)
=20
But, the method is there. Why am I getting this?
=20

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

Peter Hickman

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
 
A

Abder-Rahman Ali

Peter said:
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.
 
R

Rob Biedenharn

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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top