Hwo to compare a number use in[a..b]

dear all

----------<8-------------
------------1-----------
if A=13
do something
else
raise some warning
end
-------------2-----------
if A=14
do something #(the same as 1 )
else
raise some warning #(the same as 1)
end
------------8>----------------

so, is there other word like
if A is in[13…14]
do something
else
raise some warning #(the same as 1)
end

any help would be appropriated.

Edward

Hi,

Be careful not to mix up “=” (assignment) and “==” (equality).

You can either use the logical “or”:

if A == 13 or A == 14

Or you can use Array#include?:

if [13, 14].include? A

The latter doesn’t really make sense here, but it’s useful when you have
a lot of values.

In your specific case it might make more sense to put the error handling
on top:

raise … unless A == 13 or A == 14

do something

On Mon, Sep 3, 2012 at 1:47 PM, Jan E. [email protected] wrote:

The latter doesn’t really make sense here, but it’s useful when you have
a lot of values.

And for integers we can also facilitate ranges

irb(main):001:0> (13…17).include? 15
=> true

Kind regards

robert

Thanks Jan, robert.
Regards
Edward.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs