Skip to content

Instantly share code, notes, and snippets.

View dmgarland's full-sized avatar

Dan Garland dmgarland

View GitHub Profile
irb(main):001:0> a = ["hello", "there"]
=> ["hello", "there"]
irb(main):002:0> a.map { |w| w.include?[^Chere"]}
irb(main):002:0> a
=> ["hello", "there"]
irb(main):003:0> b = ["there"]
=> ["there"]
irb(main):004:0> a.map { |w| b.include?(w) ? "<b>#{w}</b>" : w }
=> ["hello", "<b>there</b>"]
irb(main):005:0> a
@dmgarland
dmgarland / gist:1376053
Created November 18, 2011 10:00
HTML Push Parser with Nokogiri?
module Nokogiri
module XML
module SAX
class PushParser
# The Nokogiri::XML::SAX::Document on which the PushParser will be
# operating
attr_accessor :document
###
@dmgarland
dmgarland / gist:5248211
Created March 26, 2013 19:06
Here is an example of a gist guessing game using a while loop....
secret = rand(100)
print "Enter a number..."
while secret != (input = gets.chomp.strip.to_i)
puts "Wrong. Guess again..."
if secret > input
puts "I'll give you a clue... its higher..."
else
puts "I'll give you a clue... its lower..."
@dmgarland
dmgarland / gist:5251569
Created March 27, 2013 04:10
Here was that work in progress for the strings and cases stuff we did earlier...
print "Hello, what is your name ? > "
first_name = gets.strip.chomp
puts "Hello #{first_name}"
# Do the same thing for the second name
print "Ok, what is your second name ? >"
last_name = gets.strip.chomp
puts "Your full name is : #{first_name} #{last_name}"
# Ask the user for their age, and say that they can't come
@dmgarland
dmgarland / gist:5255678
Created March 27, 2013 16:27
Here is my example...
require 'rubygems'
require 'pry'
class Animal
def speak
puts "hello"
end
def self.create_animal(type)
case type
@dmgarland
dmgarland / gist:5266379
Created March 28, 2013 20:09
An example implementation of the Student Directory exercise making use of inheritance and the super keyword
require 'rubygems'
require 'yaml'
class Person
attr_accessor :name
attr_accessor :email
attr_accessor :github_user
attr_accessor :twitter
attr_accessor :fun_fact
attr_accessor :favorite_food
@dmgarland
dmgarland / gist:5272911
Created March 29, 2013 19:10
Here are some ideas for how to achieve a working StudentDirectory using YAML
# Let's make @directory an array, rather than a String
@directory = []
# Pull in existing people from a YAML file
@directory += YAML.load_documents(File.open('student_directory.yml'))
# Save people to a YAML file
File.open('student_directory.yml', 'a') { |f|
@directory.compact.each do |person|
f.write(person.to_yaml)
@dmgarland
dmgarland / gist:5285916
Created April 1, 2013 16:14
Here is an example block of code that reads and writes to a SQLite3 database
require 'pry'
require 'sqlite3'
# Open a database called my_amazing_database.db
# and store a reference to it in db
db = SQLite3::Database.new("my_amazing_database.db")
# Print all of the people in the database so far
results = db.execute("select * from people")
results.each do |row|
@dmgarland
dmgarland / gist:5303972
Created April 3, 2013 18:41
Bit.ly in a few lines...
require 'sinatra'
set :urls, {
:xyz => "http://www.generalassemb.ly"
}
get '/:short_url' do
full_url = settings.urls[params[:short_url].to_sym]
if full_url
redirect full_url
@dmgarland
dmgarland / movie_server_test.rb
Created April 5, 2013 15:43
Movie Server Test
require 'test/unit'
require 'rack/test'
require_relative '../movie_server'
class MovieServerTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Sinatra::Application
end