This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Card | |
| SUITS = %w(S H D C).freeze | |
| RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze | |
| RANKS_SCORES = RANKS.each_with_index.to_h | |
| include Comparable | |
| attr_reader :suit, :rank | |
| def initialize(suit, rank) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'benchmark/ips' | |
| module Enumerable | |
| def deconstruct() = to_a | |
| end | |
| class Collection | |
| attr_reader :items | |
| include Enumerable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'dio' | |
| # Dive into arbitrary objects using their methods | |
| Dio[1] in { succ: { succ: { succ: 4 } } } | |
| # => true | |
| Node = Struct.new(:value, :children) | |
| tree = Node[1, | |
| Node[2, Node[3, Node[4]]], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Card | |
| SUITS = %w(S H D C).freeze | |
| SUITS_SCORES = SUITS.each_with_index.to_h | |
| RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze | |
| RANKS_SCORES = RANKS.each_with_index.to_h | |
| include Comparable | |
| attr_reader :suit, :rank |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Card = Struct.new(:suit, :rank) do | |
| include Comparable | |
| def precedence() = [SUITS_SCORES[self.suit], RANKS_SCORES[self.rank]] | |
| def rank_precedence() = RANKS_SCORES[self.rank] | |
| def suit_precedence() = SUITS_SCORES[self.rank] | |
| def <=>(other) = self.precedence <=> other.precedence | |
| def to_s() = "#{self.suit}#{self.rank}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RANKS = [*2..10, *%w(J Q K A)].map(&:to_s).freeze | |
| RANKS_SCORES = RANKS.each_with_index.to_h | |
| Card = Struct.new(:suit, :rank) | |
| straight = [Card['H', RANKS.first], *RANKS[1..4].map { Card['S', _1] }] | |
| not_straight = [Card['H', RANKS.last], *RANKS[1..4].map { Card['S', _1] }] | |
| def add_rank(r, n) = RANKS[RANKS_SCORES[r] + n] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MOVE = /[XO]/.freeze | |
| def board(*rows) = rows.map(&:chars) | |
| def winner(board) | |
| case board | |
| in [ | |
| [MOVE => move, ^move, ^move], | |
| [_, _, _], | |
| [_, _, _] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Card = Struct.new(:suite, :rank) do | |
| include Comparable | |
| def precedence() = [SUITES_SCORES[self.suite], RANKS_SCORES[self.rank]] | |
| def rank_precedence() = RANKS_SCORES[self.rank] | |
| def suite_precedence() = SUITES_SCORES[self.rank] | |
| def <=>(other) = self.precedence <=> other.precedence | |
| def to_s() = "#{self.suite}#{self.rank}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Either | |
| def self.deconstruct_keys(*) | |
| { Right: Right, Left: Left } | |
| end | |
| class Right < Struct.new(:value); end | |
| class Left < Struct.new(:value); end | |
| end | |
| Either => { Right:, Left: } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ideal syntax for pattern matched methods: | |
| def handle_response(body) | |
| in 200, response | |
| # Ok response | |
| in 500, error | |
| # Handle failure | |
| else | |
| # No idea? | |
| end |