Skip to content

Instantly share code, notes, and snippets.

@arlandism
arlandism / merge-with-apply-kind-of.clj
Last active August 29, 2015 13:55
How do I do this?
(def args {:age 3})
(def opts {:age inc})
;; Desired behavior:
;; (my-function args opts) => {:age 4}
;;
;; below example works, but my argument isn't actually in a seq
(def args {:age [3]})
db_yaml = File.expand_path(File.dirname(__FILE__) + "../../../../config/database.yml")
YAML.load(File.open(db_yaml))
@arlandism
arlandism / printer.rb
Last active August 29, 2015 13:56
When dependency injection goes wrong...
class InkReservoir
def contact_point
puts "Contact point activated"
end
end
class InkCartridge
@arlandism
arlandism / minimax.rb
Last active August 29, 2015 13:57
Minimax Example
class Minimax
def score_move(board, move, current_token, depth)
sandbox_board = Board.new(board.state)
sandbox_board.move(move, current_token)
rules = TTTRules.new(sandbox_board)
if rules.game_over or depth == 0
return evaluate_game(rules.game_winner, current_token, depth)
else
opponent_token = rules.other_token(current_token)
class MockBoard
def initialize(initial_state=["", "", "", "", "", "", "", "", ""])
@state = initial_state
end
def move(space, token)
@state[space] = token
end
source "http://rubygems"
gem "resque", "~> 2.0.0.pre.1", github: "resque/resque"
gem "sinatra"
node = @head.next_node
while !node.nil? do
node = node.next_node
end
node.next_node = Node.new(value)
class MockGame
attr_reader :last_received_move
def initialize(*invalid_moves)
@invalid_moves = invalid_moves
end
def place(move)
if @invalid_moves.include?(move)
raise TicTacToe::InvalidMove
/Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/lib/file_line.rb:23:in `update_filesystem!': undefined method `write' for File:Class (NoMethodError)
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/lib/main.rb:76:in `take_user_suggestion!'
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/lib/main.rb:29:in `replace_all'
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/lib/main.rb:20:in `each'
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/lib/main.rb:20:in `replace_all'
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/gems/rbname-0.0.6/bin/rbname:9
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/bin/rbname:23:in `load'
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/bin/rbname:23
from /Users/arlandislawrence/.rvm/gems/ree-1.8.7-2011.12@my-gemset/bin/ruby_executable_hooks:1
class Game
attr_reader :tokens
def initialize
@tokens = [:O]
end
def make_move(space)
@board.place(0, :X)