~mrlee/www.kamelasa.dev

4d1d7c2db6ae372912736fc818660d6f2ffbc501 — Lee Meichin a month ago 234061b
Add some weird shorthand
1 files changed, 5 insertions(+), 2 deletions(-)

M posts/ruby-sorcery.poly.pm
M posts/ruby-sorcery.poly.pm => posts/ruby-sorcery.poly.pm +5 -2
@@ 108,7 108,7 @@ Now that a hand of cards is represented, it should be possible to use pattern ma
This particular solution depends on the hand being ordered, but that's fine, a lot of computational problems become simpler if you sort them first. For the sake of example, assume that has already happened.

◊codeblock['ruby]{
  def is_royal_flush?(hand)
  def royal_flush?(hand)
    case hand
    in [[1, c, s], ['K', ^c, ^s], ['Q', ^c, ^s], ['J', ^c, ^s], [10, ^c, ^s]]
      true


@@ 116,6 116,9 @@ This particular solution depends on the hand being ordered, but that's fine, a l
    end
  end

  # alternatively, if golfing in Ruby 3:
  # def royal_flush?(hand) = !!(hand in [[1, c, s], ['K', ^c, ^s], ['Q', ^c, ^s], ['J', ^c, ^s], [10, ^c, ^s]] rescue false)


  my_hand = PokerHand.new(cards: [
    PlayingCard.new(value: 1, colour: :black, suit: :hearts),


@@ 125,7 128,7 @@ This particular solution depends on the hand being ordered, but that's fine, a l
    PlayingCard.new(value: 10, colour: :black, suit: :hearts),
  ])

  is_royal_flush?(my_hand)
  royal_flush?(my_hand)
  # => true
}