Skip to content

Instantly share code, notes, and snippets.

@TGSmith
TGSmith / P6._OO_Terminology_solution.rb
Created July 11, 2013 03:30
Solution for P6. OO Terminology
# Solution for Challenge: P6. OO Terminology. Started 2013-07-11T03:30:13+00:00
class Bicycle
attr_reader :color, :gears, :move, :brake, :wheels
def initialize(args={})
@color = args[:color] || "silver"
@gears = args[:gears] || "single"
@wheels = 2
@move = false
@brake = false
@TGSmith
TGSmith / cohort.rb
Last active December 19, 2015 14:29
Solution for ActiveRecord, Jr. 2: SQL Be Gone *** Moved all these method into database_model.rb *** Student.all and Cohort.all Student.create and Cohort.create Student.where and Cohort.where Student.find and Cohort.find Student#new_record? and Cohort#new_record? Student#insert! and Cohort#insert! Student#update! and Cohort#update!
class Cohort < Database::Model
self.attribute_names = [:id, :name, :created_at, :updated_at]
attr_reader :attributes, :old_attributes
# e.g., Cohort.new(:id => 1, :name => 'Alpha', :created_at => '2012-12-01 05:54:30')
def initialize(attributes = {})
attributes.symbolize_keys!
@TGSmith
TGSmith / cohort.rb
Last active December 19, 2015 14:29
Solution for ActiveRecord, Jr. 1: A Basic ORM
class Cohort < Database::Model
# def self.all
# Database::Model.execute("SELECT * FROM cohorts").map do |row|
# Cohort.new(row)
# end
# end
def self.create(attributes)
record = self.new(attributes)
record.save
@TGSmith
TGSmith / importing_political_db.rb
Last active December 19, 2015 14:08 — forked from dbc-challenges/importing_political_db.rb
Importing A Political Database
require 'csv'
require 'sqlite3'
class PoliticianDB
@@politicians_stats =[]
def self.setup
system('rm politicians.db')
$db = SQLite3::Database.new "politicians.db"
@TGSmith
TGSmith / DB_Drill:_Address_Book_Schema_solution.rb
Last active May 3, 2017 11:36
Solution for DB Drill: Address Book Schema
# Solution for Challenge: DB Drill: Address Book Schema. Started 2013-07-09T21:32:24+00:00
http://min.us/l0GieGZ7cxACS
<?xml version="1.0" encoding="utf-8" ?>
<!-- SQL XML created by WWW SQL Designer, http://code.google.com/p/wwwsqldesigner/ -->
<!-- Active URL: http://socrates.devbootcamp.com/sql.html -->
<sql>
<datatypes db="mysql">
<group label="Numeric" color="rgb(238,238,170)">
<type label="Integer" length="0" sql="INTEGER" re="INT" quote=""/>
@TGSmith
TGSmith / University_Course_Database_Design_solution.rb
Last active December 19, 2015 13:19
Solution for University Course Database Design
# Solution for Challenge: University Course Database Design. Started 2013-07-09T21:19:57+00:00
http://min.us/lbpNuNnFYBkAQJ
<?xml version="1.0" encoding="utf-8" ?>
<!-- SQL XML created by WWW SQL Designer, http://code.google.com/p/wwwsqldesigner/ -->
<!-- Active URL: http://socrates.devbootcamp.com/sql.html -->
<sql>
<datatypes db="mysql">
<group label="Numeric" color="rgb(238,238,170)">
<type label="Integer" length="0" sql="INTEGER" re="INT" quote=""/>
@TGSmith
TGSmith / Student_Roster_DB_from_Schema_solution.rb
Last active May 3, 2017 11:34
Solution for Student Roster DB from Schema
# Solution for Challenge: Student Roster DB from Schema. Started 2013-07-09T19:29:26+00:00
require 'sqlite3'
# If you want to overwrite your database you will need
# to delete it before running this file
$db = SQLite3::Database.new "students.db"
module StudentDB
def self.setup
$db.execute(
@TGSmith
TGSmith / Poll_DB_1:_Queries_solution.rb
Last active May 3, 2017 11:14
Solution for Poll DB 1: Queries
# Solution for Challenge: Poll DB 1: Queries. Started 2013-07-08T21:40:24+00:00
SELECT * FROM congress_members;
SELECT sql FROM (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x FROM sqlite_master UNION ALL SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'ORDER BY substr(type,2,1), CASE type WHEN 'view' THEN rowid ELSE name END
SELECT * FROM congress_members
WHERE id='524';
SELECT * FROM votes
WHERE politician_id = '524';
SELECT count(id) FROM votes
@TGSmith
TGSmith / bistro.rb
Last active December 19, 2015 09:39 — forked from dbc-challenges/bistro.rb
Bernie's Bistro
require 'csv'
class Recipe
attr_reader :id, :name, :description, :ingredients, :directions
def initialize(data={})
@id = data[:id]
@name = data[:name]
@description = data[:description]
@TGSmith
TGSmith / Scraping_HN_1:_Building_Objects_solution.rb
Last active December 19, 2015 08:09
Solution for Scraping HN 1: Building Objects
#Solution for Challenge: Scraping HN 1: Building Objects. Started 2013-07-03T22:33:29+00:00
require 'nokogiri'
require 'pry'
class Post
attr_accessor :title, :url, :points, :comments_array #, :item_id
def initialize(args)
@title = args[:title]
@url = args[:url]