Archive for the Ruby Category

  • Easily compare a variable with multiple values - May 27th, 2009

    I often forget this simple little trick for comparing multiple values against a single variable.
    Instead of
    var = 2
    if var == 1 || var == 2 || var == 3
      puts "yes"
    end
    #=> "yes"
    You can do the following
    var = 2
    if [1,2,3].include?(var)
      puts "yes"
    end
    #=> "yes"