Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to FarrelFarrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

Post Made Community Wiki
added Proc
Source Link
James A. Rosen
  • 65.6k
  • 62
  • 186
  • 263

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

Source Link
James A. Rosen
  • 65.6k
  • 62
  • 186
  • 263

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.