Skip to content

Instantly share code, notes, and snippets.

View jeffreybaird's full-sized avatar

Jeffrey Baird jeffreybaird

View GitHub Profile
@jeffreybaird
jeffreybaird / subtract_letters.rb
Created May 25, 2012 04:48 — forked from agoodman/subtract_letters.rb
Word Pruning by Letter Subtraction
possible_words = ["seek", "find", "ignore", "pursue", "covet"]
available_letters = [ "w5","g6","f7","s2","e1","l3","h8","n1","f7","b8","r12","u3","g6","i4","q9","o3","d2","s2","f7"]
words = Array[possible_words].flatten
available_letters = tiles.map {|e| e[0]}
available_letters.each {|e| words = words.map {|w| w.sub(e, '')}}
viable_words = words.each_with_index.collect {|w,k| possible_words[k] if w==""}.compact
letter_value_hash = Hash[tiles.map {|l| [l.slice(0), l.slice(1..3).to_i]}]
word_as_values = viable_words.map do |word|
@jeffreybaird
jeffreybaird / shuffle_algorithm.rb
Created May 30, 2012 23:17
Learn to Program Exercise 10.3
@new_array = %w(fun apple dance Zebra goose teenager)
def choose_a_random_word some_array
word = some_array[rand(0...some_array.size)]
some_array.delete_if do |w|
w == word
end
word
end
@jeffreybaird
jeffreybaird / playlist_maker.rb
Created May 31, 2012 20:01
Script for creating a playlist Chapter 11 exercises
Dir.chdir #add your music directory here
all_music = (Dir['**/*.mp3'].shuffle)
puts "what do you want to name your playlist?"
answer = gets.chomp
File.open "#{answer}.m3u",'w' do |file|
all_music.each do |mp3_file_name|
file.write "#{mp3_file_name}\n"
end
end
@jeffreybaird
jeffreybaird / gist:3295351
Created August 8, 2012 14:14
Find out which classes are called most often (stolen from gary bearnhart)
grep -rh '^[[:space:]]*\(class\|module\)\b' app lib --include='*.rb' |
sed 's/^[[:space:]]*//' |
cut -d ' ' -f 2 |
while read class; do
echo "`grep -rl "\b$class\b" app lib --include="*.rb" | wc -l` $class";
done |
sort -n
@jeffreybaird
jeffreybaird / marketing_sources.rb
Created August 17, 2012 15:24
Script to cleanup the cap production marketing:sources output
#create a file (here called "file.txt") to paste the cap output into, run this file to clean the output.
require 'fileutils'
require 'tempfile'
t_file = Tempfile.new('sources.txt')
File.open("file.txt", 'r+') do |f|
f.each_line do |line|
line = line.to_s
line = line.strip
require 'csv'
def remove_dupes
uniq_csv = []
CSV.foreach("input.csv","r+") {|csv| uniq_csv << csv }
uniq_csv.uniq!.compact.compact
end
CSV.open("ouput.csv","w+") do |csv|
@jeffreybaird
jeffreybaird / gist:4039285
Created November 8, 2012 14:58
A user search
scope :search, lambda{|query|
return scoped if query.blank?
tokens = query.split(/ |\+|,/).collect {|c| "%#{c.downcase}%"}
joins(:user).where(["#{(["(lower(user.first_name) like ? \
or lower(user.last_name) like ? \
or lower(user.social_security_digits) like ? \
or lower(users.email) like ? \
or user.date_of_birth like ? \
or user.id like ? \
require 'oauth_util.rb'
require 'net/http'
o = OauthUtil.new
o.consumer_key = 'examplek9SGJUTUpocjZ5QjBJmQ9WVdrOVVFNHdSR2x1TkhFbWNHbzlNQS0tJnM9Y29uc3VtkZXJzZWNyZXQmeD0yYg--';
o.consumer_secret = 'exampled88d4109c63e778dsadcdd5c1875814977';
url = 'http://query.yahooapis.com/v1/yql?q=select%20*%20from%20social.updates.search%20where%20query%3D%22search%20terms%22&diagnostics=true';
require 'fis/test'
require_relative 'student'
include Fis::Test
test 'should create a table' do
assert Student.drop
assert !Student.table_exists?('students')
assert Student.create_table
assert Student.table_exists?('students')
#CLASS/INSTANCE METHODS AND VARIABLES PRACTICE:
# ADD YOUR CODE TO THIS FILE
#
#1. Class/Methods
# -create a class called Project
# -create a class variable called count
# -create a instance method called "say_class_name" that prints name of the class
#2.Instance Variables/Class Variables/Constants
# - create a class method to return @@count
# - give it the instance variables title, description, role, date created and date updated