This file contains 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
PS1="\[\e[0;33;49m\]\n\W\[\e[0m\e[0;34;49m\] \$ \[\e[0m\]" |
This file contains 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 '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 |
This file contains 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 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." | |
This file contains 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 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 |
This file contains 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 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 |
This file contains 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 '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 |
This file contains 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
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 |
This file contains 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
# 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 | |
This file contains 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 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 |
This file contains 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
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. |