Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer Range in Ruby

Tags:

ruby

I'm a newbie to Ruby, I have a problem following the Poignant Guide to Ruby:

Does this expression return true?

2005..2009 === 2007

But I don't know why I got this warning message from the following code

wishTraditional.rb:4: warning: integer literal in conditional range

code:

def makTimeLine(year)
if 1984 === year
        "Born."
elsif 2005..2009 === year
        "Sias."
else
        "Sleeping"
end
end
puts makTimeLine(2007)

and the it return Sleeping, which is wrong and should be the Sias

BTW what does the two dots mean? How can I find more information about it?

like image 558
mko Avatar asked Dec 10 '22 12:12

mko


1 Answers

I think you better use something like that :

elsif (2005..2009).include?(year)

Here is the documentation about Ruby ranges

Update: if you insist on using ===, you should enclose the range in parentheses:

elseif (2005..2009) === year
like image 60
Baramin Avatar answered Jan 03 '23 20:01

Baramin