Skip to content

Instantly share code, notes, and snippets.

@davidbella
davidbella / anagram.rb
Created October 7, 2013 12:42
Ruby: Anagram detector class - way cleaner than my old one
class Anagram
def initialize(anagram)
@anagram = anagram
end
def match(words)
words.select do |word|
is_anagram?(word)
end
end
@davidbella
davidbella / crowd_funding_create.sql
Created October 4, 2013 03:22
SQL: JOINs and SUMs exercise, crowd funding database
CREATE TABLE projects(
id INTEGER,
title TEXT,
category_id INTEGER,
funding_goal INTEGER,
start_date INTEGER,
end_date INTEGER
);
CREATE TABLE categories(
@davidbella
davidbella / version_sort.rb
Created October 3, 2013 16:03
Ruby: Version sort (bad, incomplete)
require 'pp'
class Array
def version_sort
version_sorted = []
versions = self.collect do |filename|
filename =~ /^foo-(.*)\.ext$/
versions = $1.split('.')
end
@davidbella
davidbella / jukebox.rb
Created October 3, 2013 15:25
Ruby: Debugging the Jukebox cli app
require_relative './song_library.rb'
def jukebox(command)
if command.downcase == "list"
list_library
else
parse_command(command)
end
end
@davidbella
davidbella / pigeons.rb
Created October 3, 2013 02:48
Ruby: Hash rearranging with pigeons
pigeon_data = {
:color => {
:purple => ["Theo", "Peter Jr.", "Lucky"],
:grey => ["Theo", "Peter Jr.", "Ms .K"],
:white => ["Queenie", "Andrew", "Ms .K", "Alex"],
:brown => ["Queenie", "Alex"]
},
:gender => {
:male => ["Alex", "Theo", "Peter Jr.", "Andrew", "Lucky"],
:female => ["Queenie", "Ms .K"]
@davidbella
davidbella / reddit_json.rb
Created October 2, 2013 14:04
Ruby: Reddit API and HTML output
require 'json'
require 'rest_client'
reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json'))
reddit_hash["data"]["children"].select! { |story| story["data"]["subreddit"] != "gaming" }
puts "<html>"
puts "<head>"
puts "</head>"
@davidbella
davidbella / blog_post_scheduler.rb
Created October 2, 2013 13:07
Ruby: Group Shuffle
# create a method called create_groups that, given an array of student names,
# a group size, and a number of groups, will return an array of groups of
# of students with no student in adjacent groups
def create_groups(student_names, group_size, number_of_groups)
student_names.shuffle!
groups = []
number_of_groups.times do
group = []
@davidbella
davidbella / tower_of_hanoi.rb
Created October 2, 2013 13:04
Ruby: Tower of Hanoi with heavy annotation
# tower_of_hanoi.rb
a = [1,2,3,4]
b = []
c = []
# Move the disks from one peg to another following the rules of Hanoi.
#
# number_of_disks - the total number of disks
# from - the starting peg
@davidbella
davidbella / prime.rb
Created October 1, 2013 20:42
Ruby: Simple prime number solution in Ruby
def prime?(number)
tries = 0
i = 5
prime = true
return false if number.even? || number % 3 == 0 || number < 2
while (i*i) < number
tries += 1
prime = false and break if number % i == 0
i += 2
@davidbella
davidbella / iterators.rb
Created October 1, 2013 20:41
Ruby: Creating iterators
def my_each(array)
i = 0
while i < array.length
yield(array[i])
i+=1
end
array
end