Skip to content

Instantly share code, notes, and snippets.

@sd
Created November 30, 2012 17:05
Show Gist options
  • Save sd/4177024 to your computer and use it in GitHub Desktop.
Save sd/4177024 to your computer and use it in GitHub Desktop.
x = "123"
case x
when /\d+/
...
when String
...
when 1..3
...
when "123"
...
end
# is equivalent to
if (/\d+/.=== x)
...
elsif (String.=== x)
...
elsif (1..3.=== x)
...
elsif ("123".=== x)
...
end
each class defines === in a way that makes sense... regexps try to match, classes check for the class of the object, ranges check for inclusion, strings check for equality, etc...
@gubatron
Copy link

that's a powerful case/switch statement, Java is missing out on that (i think they made it slightly better for Java 7), also we're missing out on operator overloading. It's pretty cool you can be so expressive with Ruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment