9ea3faa354c62c9d3efc4a3929b5fc920bd664d0 — Lee Meichin a month ago fd14e32
Make examples reproducible in Ruby Sorcery Pt 1
1 files changed, 10 insertions(+), 3 deletions(-)

M posts/ruby-sorcery.poly.pm
M posts/ruby-sorcery.poly.pm => posts/ruby-sorcery.poly.pm +10 -3
@@ 15,6 15,10 @@ The first part of this series of posts is all about ◊em{Pattern Matching}.
Ruby's pattern matching support, introduced experimentally in 2.7, is a lot more powerful than you may expect. All you need is to replace ◊code{when} with ◊code{in} and your ◊code{case} statements become capable of matching against ◊em{anything}.

  require 'base64'
  class ParsedJson; end;
  def handle_response(response)
    case response
    in { code: (300..399) }

@@ 137,15 141,18 @@ Building on the poker example, maybe it's valid to play the Joker, but only if t
  def valid_call?(card)
    case in card
    in [:Joker] if dealer_allows_joker? then true
    case card
    in [:Joker] if joker_allowed?
      puts 'joker allowed'
    else true

  # After 23:00
  valid_call?(PlayingCard.new(value: :Joker, colour: nil, suit: nil))
  #=> true
  # => joker allowed
  # => true

◊h2{Destructuring assignment without ◊code{case}}