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. Ruby’s

    case Statement
    by Tim Moore

    View full-size slide

  2. Ruby’s Magical

    case Statement
    by Tim Moore
    Magical

    View full-size slide

  3. There is Totally
    Such a Thing as Magic

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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"

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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"

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  64. ===
    “case equality operator”

    View full-size slide

  65. ===
    “threequals”

    View full-size slide

  66. case object
    when case1
    do_something
    when case2
    do_another_thing
    else
    do_something_else
    end

    View full-size slide

  67. object
    case1
    do_something
    case2
    do_another_thing
    else
    do_something_else
    end

    View full-size slide

  68. !
    if case1 === object
    do_something
    elsif case2 === object
    do_another_thing
    else
    do_something_else
    end

    View full-size slide

  69. class Range
    def ===(value)
    self.include?(value)
    end
    end

    View full-size slide

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

    View full-size slide

  71. class Regexp
    def ===(string)
    if self =~ string
    true
    else
    false
    end
    end
    end

    View full-size slide

  72. class Proc
    def ===(object)
    self.call(object)
    end
    end

    View full-size slide

  73. class Object
    def ===(other)
    self == other
    end
    end

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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
    => #
    irb> OtherGem::VERSION = '1.0'
    irb> somegem_adapter
    => #
    irb> OtherGem::VERSION = '1.2'
    irb> somegem_adapter
    => #
    irb> OtherGem::VERSION = '2.0'
    irb> somegem_adapter
    => #
    irb> OtherGem::VERSION = '4.0'
    irb> somegem_adapter
    => #

    View full-size slide