Project

General

Profile

Actions

Bug #11228

closed

Classes Cannot be used in case Statements

Bug #11228: Classes Cannot be used in case Statements

Added by martin_vahi (Martin Vahi) over 10 years ago. Updated over 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
[ruby-core:69472]

Description

#!/usr/bin/env ruby def flaw_demo cl_selector="A String instance".class case cl_selector when String puts "flaw_demo: This is, where the control flow should be." when Regexp puts "flaw_demo: Hi Regexp!" else puts "flaw_demo: The control flow should not reach this line." end # case end ; flaw_demo() CONST_1=42 CONST_2=99 def works_ok i_selector=42 case i_selector when CONST_1 puts " works_ok: This is, where the control flow should be." when CONST_2 puts " works_ok: Hi CONST_2!" else puts " works_ok: The control flow should not reach this line." end # case end ; works_ok() # Console output: # flaw_demo: The control flow should not reach this line. # works_ok: This is, where the control flow should be. 

Files

test_case.rb (798 Bytes) test_case.rb Nothing new, just for convenience. martin_vahi (Martin Vahi), 06/05/2015 11:21 PM

Updated by marcandre (Marc-Andre Lafortune) over 10 years ago Actions #1 [ruby-core:69476]

  • Status changed from Open to Rejected

This is per spec, please check the definition of Class#===, since a switch is sugar for calls to ===.

String === String # => false 

Updated by marcandre (Marc-Andre Lafortune) over 10 years ago Actions #2 [ruby-core:69477]

PS: The definition of Class#=== allows you to write

 case "A String instance" when String puts "flaw_demo: This is, where the control flow should be." when Regexp puts "flaw_demo: Hi Regexp!" else puts "flaw_demo: The control flow should not reach this line." end # case # => flaw_demo: This is, where the control flow should be. 
Actions

Also available in: PDF Atom