Skip to content

Instantly share code, notes, and snippets.

View geowy's full-sized avatar

George Wyatt geowy

  • Agworld
  • Perth, Western Australia
View GitHub Profile
@geowy
geowy / game.rb
Created July 20, 2020 14:36
Tic Tac Toe in Ruby
class Game
WINNING_PATTERNS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
require 'matrix'
NEIGHBOUR_RELATIVE_COORDS =
[[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]]
# Init matrix
# true = alive, false = dead
matrix = Matrix.build(35, 45) { [true, false].sample }
@geowy
geowy / Rspec_subject_behaviour_question.rb
Last active February 10, 2016 03:20 — forked from damonjmurray/Rspec_subject_behaviour_question.rb
RSpec subject behaviour question
require 'spec_helper'
class Foo
def self.bar(first, second, third)
err_msg = '%s cannot be nil'
raise ArgumentError, err_msg % 'first' unless first
raise ArgumentError, err_msg % 'second' unless second
raise ArgumentError, err_msg % 'third' unless third
"#{first} #{second} #{third}"
@geowy
geowy / array_to_proc.rb
Last active September 16, 2015 12:19
Array#to_proc, shorthand for creating a block that selects elements of an array/hash/collection.
class Array
# Returns a proc which calls [] with the array's contents as arguments
#
# ====Usage
# [[1, 2], [3, 4], [5, 6]].map(&[0])
# # => [1, 3, 5]
#
# [{ hello: 'world' }, { hello: 'sun', goodbye: 'moon' }].map(&[:hello])
# # => ['world', 'sun']