Skip to content

Instantly share code, notes, and snippets.

View DavidRagone's full-sized avatar

David Ragone DavidRagone

View GitHub Profile
@DavidRagone
DavidRagone / gist:4100458
Created November 17, 2012 21:30
Setup PostgreSQL for Rails development in Mac OS 10.7.x (aka Lion)
# Clean a previous install
brew uninstall postgre
rm -rf /usr/local/var/postgres/
# Install the database with brew package manager
brew install postgres
brew info postgresql
# Initialize the database
initdb /usr/local/var/postgres
@DavidRagone
DavidRagone / card.rb
Created October 21, 2012 19:07 — forked from dbc-challenges/card.rb
FlashCardinator
class Card
def initialize(term, definition)
@term = term
@definition = definition.chomp
end
attr_reader :term, :definition
def to_s
"Term: '#{term}', Definition: '#{definition}'"
@DavidRagone
DavidRagone / csv_io.rb
Created October 16, 2012 16:26
CSV reader/writer
class CsvIO
def self.read_from_file(filename, class_type, storage_location)
File.open(filename) do |f|
f.each_line do |line|
params = line.split(',')
storage_location << Kernel.const_get(class_type).send(:new, *params)
end
end
end
@DavidRagone
DavidRagone / binary_search.rb
Created October 6, 2012 06:12
Binary Search
def binary_search(obj, sorted_arr, search_index=0)
arr_length = sorted_arr.length
if sorted_arr[arr_length/2] == obj
arr_length/2 + search_index
elsif sorted_arr[arr_length/2] > obj
binary_search(obj, sorted_arr[0..arr_length/2-1], search_index)
else
binary_search(obj, sorted_arr[arr_length/2+1..-1], search_index + arr_length/2+1)
end
end
@DavidRagone
DavidRagone / binary_vs_linear_searching.rb
Created October 6, 2012 05:44
binary vs linear searching
require 'benchmark'
require_relative "linear_search"
require_relative 'binary_search'
one_thousand = (1..1000).to_a
ten_thousand = (1..10_000).to_a
one_million = (1..1_000_000).to_a
test_cases = {1000 => one_thousand, 10000 => ten_thousand, 1000000 => one_million}
test_cases.each do |int, test_array|
# Put your answers here!
rvm rubies
ruby-1.8.7-p370 [ i686 ]
ruby-1.9.3-p125 [ x86_64 ]
=* ruby-1.9.3-p194 [ x86_64 ]
# => - current
# =* - current && default
# * - default