$30 off During Our Annual Pro Sale. View Details »

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. Ruby’s
 case Statement by Tim Moore

  2. Ruby’s Magical
 case Statement by Tim Moore Magical

  3. There is Totally Such a Thing as Magic

  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 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. 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. 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. 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. 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. 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. 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
  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) 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. 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. 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. 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. 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. 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. 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. 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. 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 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. None
  65. === “case equality operator”

  66. === “threequals”

  67. case object when case1 do_something when case2 do_another_thing else do_something_else

    end
  68. object case1 do_something case2 do_another_thing else do_something_else end

  69. ! if case1 === object do_something elsif case2 === object

    do_another_thing else do_something_else end
  70. class Range def ===(value) self.include?(value) end end

  71. class Module def ===(object) object.is_a?(self) end end

  72. class Regexp def ===(string) if self =~ string true else

    false end end end
  73. class Proc def ===(object) self.call(object) end end

  74. class Object def ===(other) self == other 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 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. 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. 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. 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. 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. 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
  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. 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. 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. 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. 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
  88. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher|

    matcher === version } end ! end
  89. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher|

    matcher === version } end ! end
  90. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher|

    matcher === version } end ! end
  91. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher|

    matcher === version } end ! end
  92. class VersionMatcher::All < Struct.new(:version_matchers) ! def ===(version) version_matchers .all? {|matcher|

    matcher === version } end ! end
  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. 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. 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. 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
  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. 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
  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. 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
  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. 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
  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. 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
  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. 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
  107. 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
  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
  109. 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>
  110. Thank you!