Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby's Magical case Statement

Ruby's Magical case Statement

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.

Tim Moore

July 30, 2014
Tweet

More Decks by Tim Moore

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  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. 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. 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. 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. 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. 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. 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. 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. 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. 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"
  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. 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. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  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. 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. 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. 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. 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. 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. 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. 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
  53. 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
  54. 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"
  55. 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
  56. 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
  57. ! if case1 === object do_something elsif case2 === object

    do_another_thing else do_something_else end
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  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. 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. 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
  78. 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
  79. 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
  80. 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
  81. 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
  82. 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
  83. 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
  84. 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
  85. 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
  86. 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>