Skip to content

Instantly share code, notes, and snippets.

@masassiez
Created February 24, 2012 18:53
Show Gist options
  • Save masassiez/1902938 to your computer and use it in GitHub Desktop.
Save masassiez/1902938 to your computer and use it in GitHub Desktop.
Rubyのcase式で気を付けること ref: http://qiita.com/items/2796
require './strange_case.rb'
strange_case( "1" ) # => true
strange_case( :"1" ) # => true
strange_case( 1 ) # => true
strange_case( [1, 2, 3] ) # => false
strange_case( {"1"=>1} ) # => true
strange_case( (1..3) ) # => false
strange_case( /.*/ ) # => false
strange_case( Fixnum ) # => false
def array_case(year)
case year
when [*1925, 1926]
"T"
when [*1926..1989]
"S"
else
"H"
end
end
array_case(1926) # => "H"
array_case(1973) # => "H"
array_case(2012) # => "H"
class Array
alias :=== :include?
end
def ex_array_case(year)
case year
when [*1925, 1926]
"T"
when [*1926..1989]
"S"
else
"H"
end
end
ex_array_case(1926) # => "T"
ex_array_case(1973) # => "S"
ex_array_case(2012) # => "H"
def range_case(year)
case year
when 1912..1926
"T"
when 1926..1989
"S"
else
"H"
end
end
range_case(1926) # => "T"
range_case(1973) # => "S"
range_case(2012) # => "H"
def strange_case(obj)
case obj
when obj
true
else
false
end
end
strange_case( "1" ) # => true
strange_case( :"1" ) # => true
strange_case( 1 ) # => true
strange_case( [1, 2, 3] ) # => true
strange_case( {"1"=>1} ) # => true
strange_case( (1..3) ) # => false
strange_case( /.*/ ) # => false
strange_case( Fixnum ) # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment