What is the best way to handle this type of inclusive logic in Ruby?

Posted by Steve McLelland on Stack Overflow See other posts from Stack Overflow or by Steve McLelland
Published on 2011-01-15T16:21:34Z Indexed on 2011/01/15 23:53 UTC
Read the original article Hit count: 345

Filed under:
|

Is there a better way of handling this in Ruby, while continuing to use the symbols?

pos = :pos1 # can be :pos2, :pos3, etc.

if pos == :pos1 || pos == :pos2 || pos == :pos3
  puts 'a'
end

if pos == :pos1 || pos == :pos2
  puts 'b'
end

if pos == :pos1
  puts 'c'
end

The obvious way would be swapping out the symbols for number constants, but that's not an option.

pos = 3

if pos >= 1
  puts 'a'
end

if pos >= 2
  puts 'b'
end

if pos >= 3
  puts 'c'
end

Thanks.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about logic