Ruby’s

case Statement
by Tim Moore
Ruby’s Magical

case Statement
by Tim Moore
Magical
There is Totally
Such a Thing as Magic
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301
handle_redire...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301, 302, 303, 30...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301, 302, 303, 30...
def handle_response(http_response)
case http_response.status
when 200
handle_success(http_response)
when 301, 302, 303, 30...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
case first_name
when 'Abraham'
nickname = 'Abe'
when 'Abigail'
nickname = 'Abby'
when 'Christophe...
def nickname(first_name)
nickname = case first_name
when 'Abraham'
'Abe'
when 'Abigail'
'Abby'
when 'Christopher',
'Christ...
def nickname(first_name)
nickname = case first_name
when 'Abraham'
'Abe'
when 'Abigail'
'Abby'
when 'Christopher',
'Christ...
def nickname(first_name)
case first_name
when 'Abraham'
'Abe'
when 'Abigail'
'Abby'
when 'Christopher',
'Christian',
'Chri...
class Color <
Struct.new(:r,:g,:b)
!
RED = Color.new(
0xFF, 0x00, 0x00
)
GREEN = Color.new(
0x00, 0xFF, 0x00
)
BLUE = Colo...
class Color <
Struct.new(:r,:g,:b)
!
RED = Color.new(
0xFF, 0x00, 0x00
)
GREEN = Color.new(
0x00, 0xFF, 0x00
)
BLUE = Colo...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def local_search(query)
case query
when String
specs_by_name(query)
when Gem::Dependency
search_by_dependency(query)
when ...
def want_a_beer?(beer_type)
case beer_type
when /^Sierra Nevada|IPA/
:yes
when /V(ictoria)? ?B(itter)?/,
/Bud(weiser)?/
:n...
def want_a_beer?(beer_type)
case beer_type
when /^Sierra Nevada|IPA/
:yes
when /V(ictoria)? ?B(itter)?/,
/Bud(weiser)?/
:n...
def want_a_beer?(beer_type)
case beer_type
when /^Sierra Nevada|IPA/
:yes
when /V(ictoria)? ?B(itter)?/,
/Bud(weiser)?/
:n...
def want_a_beer?(beer_type)
case beer_type
when /^Sierra Nevada|IPA/
:yes
when /V(ictoria)? ?B(itter)?/,
/Bud(weiser)?/
:n...
def want_a_beer?(beer_type)
case beer_type
when /^Sierra Nevada|IPA/
:yes
when /V(ictoria)? ?B(itter)?/,
/Bud(weiser)?/
:n...
class Color
def more_blue?
b > r && b > g
end
!
def more_red?
r > b && r > g
end
!
def more_green?
g > r && g > b
end
end
...
class Color
def more_blue?
b > r && b > g
end
!
def more_red?
r > b && r > g
end
!
def more_green?
g > r && g > b
end
end
...
class Color
def more_blue?
b > r && b > g
end
!
def more_red?
r > b && r > g
end
!
def more_green?
g > r && g > b
end
end
...
class Color
def more_blue?
b > r && b > g
end
!
def more_red?
r > b && r > g
end
!
def more_green?
g > r && g > b
end
end
...
===“case equality operator”
===“threequals”
case object
when case1
do_something
when case2
do_another_thing
else
do_something_else
end
object
case1
do_something
case2
do_another_thing
else
do_something_else
end
!
if case1 === object
do_something
elsif case2 === object
do_another_thing
else
do_something_else
end
class Range
def ===(value)
self.include?(value)
end
end
class Module
def ===(object)
object.is_a?(self)
end
end
class Regexp
def ===(string)
if self =~ string
true
else
false
end
end
end
class Proc
def ===(object)
self.call(object)
end
end
class Object
def ===(other)
self == other
end
end
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
class VersionMatcher <
Struct.new(:operator, :req_version)
def ===(version)
case operator
when "="
version == req_version
...
class VersionMatcher <
Struct.new(:operator, :req_version)
def ===(version)
case operator
when "="
version == req_version
...
class VersionMatcher <
Struct.new(:operator, :req_version)
def ===(version)
case operator
when "="
version == req_version
...
class VersionMatcher <
Struct.new(:operator, :req_version)
def ===(version)
case operator
when "="
version == req_version
...
class VersionMatcher <
Struct.new(:operator, :req_version)
def ===(version)
case operator
when "="
version == req_version
...
class VersionMatcher::All <
Struct.new(:version_matchers)
!
def ===(version)
version_matchers
.all? {|matcher| matcher ===...
class VersionMatcher::All <
Struct.new(:version_matchers)
!
def ===(version)
version_matchers
.all? {|matcher| matcher ===...
class VersionMatcher::All <
Struct.new(:version_matchers)
!
def ===(version)
version_matchers
.all? {|matcher| matcher ===...
class VersionMatcher::All <
Struct.new(:version_matchers)
!
def ===(version)
version_matchers
.all? {|matcher| matcher ===...
class VersionMatcher::All <
Struct.new(:version_matchers)
!
def ===(version)
version_matchers
.all? {|matcher| matcher ===...
def version(requirements)
case requirements
when String
op, req_version = requirements.split
VersionMatcher.new(op, req_ve...
def version(requirements)
case requirements
when String
op, req_version = requirements.split
VersionMatcher.new(op, req_ve...
def version(requirements)
case requirements
when String
op, req_version = requirements.split
VersionMatcher.new(op, req_ve...
def version(requirements)
case requirements
when String
op, req_version = requirements.split
VersionMatcher.new(op, req_ve...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when version("< 1.0")
OtherGemAdapter::Prerelease.new
when version([">= 1.0", "...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
case OtherGem::VERSION
when VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
when Versio...
def somegem_adapter
OtherGem::VERSION
VersionMatcher.new("<", "1.0")
OtherGemAdapter::Prerelease.new
VersionMatcher::All.n...
def somegem_adapter
!
if VersionMatcher.new("<", "1.0") 
=== OtherGem::VERSION
OtherGemAdapter::Prerelease.new
elsif Versi...
def somegem_adapter
!
if VersionMatcher.new("<", "1.0") 
=== OtherGem::VERSION
OtherGemAdapter::Prerelease.new
elsif Versi...
Thank you!
Ruby’s Magical case Statement
Upcoming SlideShare
Loading in …5
×

Ruby’s Magical case Statement

430 views
315 views

Published on

Ruby's case statement has a lot of cool tricks. See how to use it to match classes, ranges, regular expressions, and your own custom matcher classes.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
430
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby’s Magical case Statement

  1. 1. Ruby’s
 case Statement by Tim Moore
  2. 2. Ruby’s Magical
 case Statement by Tim Moore Magical
  3. 3. There is Totally Such a Thing as Magic
  4. 4. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  5. 5. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  6. 6. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  7. 7. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  8. 8. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  9. 9. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  10. 10. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  11. 11. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  12. 12. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  13. 13. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  14. 14. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  15. 15. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  16. 16. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  17. 17. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  18. 18. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  19. 19. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  20. 20. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  21. 21. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301 handle_redirect(http_response) when 302 handle_redirect(http_response) when 303 handle_redirect(http_response) when 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  22. 22. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301, 302, 303, 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  23. 23. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301, 302, 303, 307 handle_redirect(http_response) when 404 handle_not_found(http_response) else handle_unknown_error(http_response) end end
  24. 24. def handle_response(http_response) case http_response.status when 200 handle_success(http_response) when 301, 302, 303, 307 handle_redirect(http_response) when 404 handle_not_found(http_response) when 400...500 handle_client_error(http_response) when 500...600 handle_server_error(http_response) else handle_unknown_error(http_response) end end
  25. 25. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  26. 26. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  27. 27. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  28. 28. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  29. 29. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  30. 30. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  31. 31. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  32. 32. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  33. 33. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  34. 34. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  35. 35. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  36. 36. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  37. 37. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end irb> nickname 'Christopher' => "Chris" irb> nickname 'Abigail' => "Abby" irb> nickname 'Christina' => "Chris" irb> nickname 'Fabio' => "Fabio"
  38. 38. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  39. 39. def nickname(first_name) case first_name when 'Abraham' nickname = 'Abe' when 'Abigail' nickname = 'Abby' when 'Christopher', 'Christian', 'Christina' nickname = 'Chris' # etc., etc. else nickname = first_name end nickname end
  40. 40. def nickname(first_name) nickname = case first_name when 'Abraham' 'Abe' when 'Abigail' 'Abby' when 'Christopher', 'Christian', 'Christina' 'Chris' # etc., etc. else first_name end nickname end
  41. 41. def nickname(first_name) nickname = case first_name when 'Abraham' 'Abe' when 'Abigail' 'Abby' when 'Christopher', 'Christian', 'Christina' 'Chris' # etc., etc. else first_name end nickname end
  42. 42. def nickname(first_name) case first_name when 'Abraham' 'Abe' when 'Abigail' 'Abby' when 'Christopher', 'Christian', 'Christina' 'Chris' # etc., etc. else first_name end ! end
  43. 43. class Color < Struct.new(:r,:g,:b) ! RED = Color.new( 0xFF, 0x00, 0x00 ) GREEN = Color.new( 0x00, 0xFF, 0x00 ) BLUE = Color.new( 0x00, 0x00, 0xFF ) CYAN = Color.new( 0x00, 0xFF, 0xFF ) MAGENTA = Color.new( 0xFF, 0x00, 0xFF ) YELLOW = Color.new( 0xFF, 0xFF, 0x00 ) ! end def primary?(color, mode) case color when Color::RED, Color::GREEN, Color::BLUE mode == :additive when Color::CYAN, Color::MAGENTA, Color::YELLOW mode == :subtractive else false end end
  44. 44. class Color < Struct.new(:r,:g,:b) ! RED = Color.new( 0xFF, 0x00, 0x00 ) GREEN = Color.new( 0x00, 0xFF, 0x00 ) BLUE = Color.new( 0x00, 0x00, 0xFF ) CYAN = Color.new( 0x00, 0xFF, 0xFF ) MAGENTA = Color.new( 0xFF, 0x00, 0xFF ) YELLOW = Color.new( 0xFF, 0xFF, 0x00 ) ! end def primary?(color, mode) case color when Color::RED, Color::GREEN, Color::BLUE mode == :additive when Color::CYAN, Color::MAGENTA, Color::YELLOW mode == :subtractive else false end end irb> primary? Color::RED, :additive => true irb> primary? Color::RED, :subtractive => false irb> primary? Color::CYAN, :subtractive => true irb> primary? Color.new(0,0,255), :additive => true irb> primary? Color.new(0,127,255), :additive => false
  45. 45. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  46. 46. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  47. 47. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  48. 48. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  49. 49. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  50. 50. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  51. 51. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  52. 52. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  53. 53. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  54. 54. def local_search(query) case query when String specs_by_name(query) when Gem::Dependency search_by_dependency(query) when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification search_by_spec(query) else raise "You can't search for #{query}." end end
  55. 55. def want_a_beer?(beer_type) case beer_type when /^Sierra Nevada|IPA/ :yes when /V(ictoria)? ?B(itter)?/, /Bud(weiser)?/ :no else :probably end end
  56. 56. def want_a_beer?(beer_type) case beer_type when /^Sierra Nevada|IPA/ :yes when /V(ictoria)? ?B(itter)?/, /Bud(weiser)?/ :no else :probably end end
  57. 57. def want_a_beer?(beer_type) case beer_type when /^Sierra Nevada|IPA/ :yes when /V(ictoria)? ?B(itter)?/, /Bud(weiser)?/ :no else :probably end end
  58. 58. def want_a_beer?(beer_type) case beer_type when /^Sierra Nevada|IPA/ :yes when /V(ictoria)? ?B(itter)?/, /Bud(weiser)?/ :no else :probably end end
  59. 59. def want_a_beer?(beer_type) case beer_type when /^Sierra Nevada|IPA/ :yes when /V(ictoria)? ?B(itter)?/, /Bud(weiser)?/ :no else :probably end end irb> want_a_beer? 'Sierra Nevada Pale Ale' => :yes irb> want_a_beer? 'Sierra Nevada Torpedo IPA' => :yes irb> want_a_beer? 'Mountain Goat IPA' => :yes irb> want_a_beer? 'Victoria Bitter' => :no irb> want_a_beer? 'VB' => :no irb> want_a_beer? 'V Bitter' => :no irb> want_a_beer? 'Budweiser' => :no irb> want_a_beer? 'Bud' => :no irb> want_a_beer? 'Bud Light' => :no irb> want_a_beer? 'Coopers Pale Ale' => :probably irb> want_a_beer? 'Stella Artois' => :probably
  60. 60. class Color def more_blue? b > r && b > g end ! def more_red? r > b && r > g end ! def more_green? g > r && g > b end end def describe_color(color) case color when proc {|color| color.more_blue? } "blueish" when proc {|color| color.more_red? } "reddish" when proc {|color| color.more_green? } "greenish" else "pretty" end end
  61. 61. class Color def more_blue? b > r && b > g end ! def more_red? r > b && r > g end ! def more_green? g > r && g > b end end def describe_color(color) case color when proc {|color| color.more_blue? } "blueish" when proc {|color| color.more_red? } "reddish" when proc {|color| color.more_green? } "greenish" else "pretty" end end irb> describe_color Color::RED => "reddish" irb> describe_color Color::GREEN => "greenish" irb> describe_color Color.new(0xCC,0x11,0xFF) => "blueish" irb> describe_color Color::CYAN => "pretty"
  62. 62. class Color def more_blue? b > r && b > g end ! def more_red? r > b && r > g end ! def more_green? g > r && g > b end end def describe_color(color) case color when proc {|color| color.more_blue? } "blueish" when proc {|color| color.more_red? } "reddish" when proc {|color| color.more_green? } "greenish" else "pretty" end end
  63. 63. class Color def more_blue? b > r && b > g end ! def more_red? r > b && r > g end ! def more_green? g > r && g > b end end def describe_color(color) case color when :more_blue?.to_proc "blueish" when :more_red?.to_proc "reddish" when :more_green?.to_proc "greenish" else "pretty" end end
  64. 64. ===“case equality operator”
  65. 65. ===“threequals”
  66. 66. case object when case1 do_something when case2 do_another_thing else do_something_else end
  67. 67. object case1 do_something case2 do_another_thing else do_something_else end
  68. 68. ! if case1 === object do_something elsif case2 === object do_another_thing else do_something_else end
  69. 69. class Range def ===(value) self.include?(value) end end
  70. 70. class Module def ===(object) object.is_a?(self) end end
  71. 71. class Regexp def ===(string) if self =~ string true else false end end end
  72. 72. class Proc def ===(object) self.call(object) end end
  73. 73. class Object def ===(other) self == other end end
  74. 74. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  75. 75. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  76. 76. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  77. 77. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  78. 78. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  79. 79. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  80. 80. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  81. 81. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  82. 82. class VersionMatcher < Struct.new(:operator, :req_version) def ===(version) case operator when "=" version == req_version when ">" version > req_version when "<" version < req_version when ">=" version >= req_version when "<=" version <= req_version else false end end end
  83. 83. class VersionMatcher < Struct.new(:operator, :req_version) def ===(version) case operator when "=" version == req_version when ">" version > req_version when "<" version < req_version when ">=" version >= req_version when "<=" version <= req_version else false end end end
  84. 84. class VersionMatcher < Struct.new(:operator, :req_version) def ===(version) case operator when "=" version == req_version when ">" version > req_version when "<" version < req_version when ">=" version >= req_version when "<=" version <= req_version else false end end end
  85. 85. class VersionMatcher < Struct.new(:operator, :req_version) def ===(version) case operator when "=" version == req_version when ">" version > req_version when "<" version < req_version when ">=" version >= req_version when "<=" version <= req_version else false end end end
  86. 86. class VersionMatcher < Struct.new(:operator, :req_version) def ===(version) case operator when "=" version == req_version when ">" version > req_version when "<" version < req_version when ">=" version >= req_version when "<=" version <= req_version else false end end end
  87. 87. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher| matcher === version } end ! end
  88. 88. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher| matcher === version } end ! end
  89. 89. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher| matcher === version } end ! end
  90. 90. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher| matcher === version } end ! end
  91. 91. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher| matcher === version } end ! end
  92. 92. def version(requirements) case requirements when String op, req_version = requirements.split VersionMatcher.new(op, req_version) when Array matchers = requirements.map {|r| version(r)} VersionMatcher::All.new(matchers) end end
  93. 93. def version(requirements) case requirements when String op, req_version = requirements.split VersionMatcher.new(op, req_version) when Array matchers = requirements.map {|r| version(r)} VersionMatcher::All.new(matchers) end end
  94. 94. def version(requirements) case requirements when String op, req_version = requirements.split VersionMatcher.new(op, req_version) when Array matchers = requirements.map {|r| version(r)} VersionMatcher::All.new(matchers) end end
  95. 95. def version(requirements) case requirements when String op, req_version = requirements.split VersionMatcher.new(op, req_version) when Array matchers = requirements.map {|r| version(r)} VersionMatcher::All.new(matchers) end end
  96. 96. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  97. 97. def somegem_adapter case OtherGem::VERSION when version("< 1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  98. 98. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  99. 99. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when version([">= 1.0", "< 2.0"]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  100. 100. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ version(">= 1.0"), version("< 2.0") ]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  101. 101. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ version(">= 1.0"), version("< 2.0") ]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  102. 102. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  103. 103. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) OtherGemAdapter::Version1x.new when version(">= 2.0") OtherGemAdapter::Current.new end end
  104. 104. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) OtherGemAdapter::Version1x.new when VersionMatcher.new(">=", "2.0") OtherGemAdapter::Current.new end end
  105. 105. def somegem_adapter case OtherGem::VERSION when VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new when VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) OtherGemAdapter::Version1x.new when VersionMatcher.new(">=", "2.0") OtherGemAdapter::Current.new end end
  106. 106. def somegem_adapter OtherGem::VERSION VersionMatcher.new("<", "1.0") OtherGemAdapter::Prerelease.new VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) OtherGemAdapter::Version1x.new VersionMatcher.new(">=", "2.0") OtherGemAdapter::Current.new end end
  107. 107. def somegem_adapter ! if VersionMatcher.new("<", "1.0") === OtherGem::VERSION OtherGemAdapter::Prerelease.new elsif VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) === OtherGem::VERSION OtherGemAdapter::Version1x.new elsif VersionMatcher.new(">=", "2.0") === OtherGem::VERSION OtherGemAdapter::Current.new end ! end
  108. 108. def somegem_adapter ! if VersionMatcher.new("<", "1.0") === OtherGem::VERSION OtherGemAdapter::Prerelease.new elsif VersionMatcher::All.new([ VersionMatcher.new(">=", "1.0"), VersionMatcher.new("<" , "2.0") ]) === OtherGem::VERSION OtherGemAdapter::Version1x.new elsif VersionMatcher.new(">=", "2.0") === OtherGem::VERSION OtherGemAdapter::Current.new end ! end irb> OtherGem::VERSION = '0.9' irb> somegem_adapter => #<OtherGemAdapter::Prerelease> irb> OtherGem::VERSION = '1.0' irb> somegem_adapter => #<OtherGemAdapter::Version1x> irb> OtherGem::VERSION = '1.2' irb> somegem_adapter => #<OtherGemAdapter::Version1x> irb> OtherGem::VERSION = '2.0' irb> somegem_adapter => #<OtherGemAdapter::Current> irb> OtherGem::VERSION = '4.0' irb> somegem_adapter => #<OtherGemAdapter::Current>
  109. 109. Thank you!

×