Skip to content

Instantly share code, notes, and snippets.

View jhamon's full-sized avatar

Jennifer Hamon jhamon

View GitHub Profile
@jhamon
jhamon / gist:7425894
Created November 12, 2013 05:21
My terminal prompt.
PS1="\[\e[0;33;49m\]\n\W\[\e[0m\e[0;34;49m\] \$ \[\e[0m\]"
@jhamon
jhamon / num_to_code.rb
Last active December 25, 2015 18:19
Iteratively converting a FixNum into a base 62 string representation.
require 'CMath'
URLSAFE_CHARS = '0123456789' +
'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def num_to_code(decimal_number, base=62)
raise "Invalid base" unless (2..62).include?(base)
raise ArgumentError unless decimal_number > 0
class Cat < ActiveRecord::Base
validates :name, exclusion: { in: %w(Garfield Sylvester Tom Felix),
message: "No celebrity cat names allowed."}
validates :hair_length, inclusion: { in: %w(long_hair short_hair hairless),
message: "%{value} is not a valid hair length" }
validates :age, numericality: { only_integer: true,
message: "Only integer ages are allowed."
class Object
def self.new_attr_accessor(*attr_names)
# Metaprogramming implementation of the
# self.attr_accessor class method
attr_names.each do |attr_name|
define_method(attr_name) do
instance_variable_get("@#{attr_name}")
end
class User
# ... clipped for brevity
def save
# Create a new row in the users table
if @id.nil?
QuestionsDatabase.instance.execute(<<-SQL, @fname, @lname)
INSERT INTO
users (fname, lname)
VALUES
require 'sqlite3'
require 'singleton'
class QuestionsDatabase < SQLite3::Database
include Singleton
def initialize
super "aafollowers.db" # Our test db
self.results_as_hash = true
self.type_translation = true
key_array = [1,2,3] # => [1, 2, 3]
value_array = [4,5,6] # => [4, 5, 6]
silly_hash = { key_array => value_array } # => {[1, 2, 3]=>[4, 5, 6]}
key_array.pop # => 3
# After mutating the key the hash is broken
silly_hash # => {[1, 2]=>[4, 5, 6]}
silly_hash[key_array] # => nil
silly_hash[[1,2]] # => nil
@jhamon
jhamon / king.rb
Last active December 23, 2015 21:19
# coding: utf-8
require 'colorize'
class King < Piece
include SteppingPieces
def move_dirs
[[0, -1], [0, 1], [1, 0], [-1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]]
end
class King < Piece
include SteppingPieces
def move_dirs
[[0, -1], [0, 1], [1, 0], [-1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]]
end
def mark
return "♚".colorize(@color)
end
@jhamon
jhamon / stepping.rb
Last active December 23, 2015 21:19
module SteppingPieces
# Movement for stepping pieces, e.g. King, Knight
def moves(board)
# returns array of valid move positions for self (a Piece instance)
# Add movement vectors to current position
moves = self.move_dirs.map { |vec| new_position(@pos, vec)}
# We only allow moves that fall on the board.