Created
February 24, 2012 18:53
-
-
Save masassiez/1902938 to your computer and use it in GitHub Desktop.
Rubyのcase式で気を付けること ref: http://qiita.com/items/2796
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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