Skip to content

Instantly share code, notes, and snippets.

@pierrax
Last active July 27, 2016 12:47
Show Gist options
  • Save pierrax/a4a8d1e86b30124ab48e3ec7711686c0 to your computer and use it in GitHub Desktop.
Save pierrax/a4a8d1e86b30124ab48e3ec7711686c0 to your computer and use it in GitHub Desktop.
=== operator

=== Operator

How it works ?

x === y checks if y belongs to x group.

  String === "hello" # true
  String === 1 # false
  
  (1..10) === 3 # true
  (1..10) === 11 # false

When ===is called on a Class, Class.===(item)method is called.

Case statement

It's the method behind a case statement

  case 5
  when (1..10)
    puts "case statements match inclusion in a range"
  end

  # case statements match inclusion in a range

Complex case statement

  class Success
    def self.===(item)
      item.status >= 200 && item.status < 300
    end
  end

  class Empty
    def self.===(item)
      item.response_size == 0
    end
  end

  case http_response
  when Empty
    puts "response was empty"
  when Success
    puts "response was a success"
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment