Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
ivanbrennan / anagram.rb
Last active December 24, 2015 21:39
Anagrams
class Anagram
attr_reader :word
def initialize(word)
@word = word
end
def match(strings)
strings.select do |string|
@ivanbrennan
ivanbrennan / scrape-to-db.rb
Created October 4, 2013 21:31
Scrape-to-db
require "open-uri"
require "Nokogiri"
require "sqlite3"
def create_db
schema = students.first.keys.join(", ")
rows = db.execute <<-SQL
create table students (
schema
);
@ivanbrennan
ivanbrennan / sfw-reddit.html
Created October 3, 2013 04:25
Safe for work Reddit - generated html
<html>
<head>
</head>
<body>
<ul>
<li>
<a href=http://imgur.com/a/eMkM4>
<h1>My friend having a great time in DC</h1>
<img src=http://c.thumbs.redditmedia.com/mZ49t1GIpxyLU5MD.jpg />
<h4>Upvotes</h4>
@ivanbrennan
ivanbrennan / nyc-pidgeon-organizer-2.rb
Created October 3, 2013 03:16
NYC Pigeon Oganizer 2
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigs.
pigeon_data = {
:color => {
:purple => ["Theo", "Peter Jr.", "Lucky"],
:grey => ["Theo", "Peter Jr.", "Ms .K"],
@ivanbrennan
ivanbrennan / nyc-pigeon-organizer.rb
Created October 3, 2013 01:08
NYC Pigeon Organizer
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigs.
pigeon_data = {
:color => {
:purple => ["Theo", "Peter Jr.", "Lucky"],
:grey => ["Theo", "Peter Jr.", "Ms .K"],
@ivanbrennan
ivanbrennan / sfw-reddit.rb
Last active December 24, 2015 12:59
Safe for work Reddit
require 'json'
require 'rest_client'
reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json'))
def indent(string, num=1)
string.each_line.
map{|line| "#{" " * num}#{line}"}.join
end
def open_tag(string)
@ivanbrennan
ivanbrennan / blog_post_scheduler.rb
Created October 2, 2013 01:58
Blog post scheduler
# 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(students, group_size, num_of_groups)
out_of_order = students.shuffle
groups = []
index = 0
num_of_groups.times do
group = []
group_size.times do
@ivanbrennan
ivanbrennan / tower_of_hanoi.rb
Last active December 24, 2015 10:39
Tower of Hanoi
# 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
@ivanbrennan
ivanbrennan / version-sort.rb
Last active December 24, 2015 10:09
Version Sort
class Array
def version_sort
self.sort_by do |file|
file.scan(/\d+|[a-z]*/).map { |el| el.to_i }
end
end
end
filenames = [
"foo-1.10.2.ext",
@ivanbrennan
ivanbrennan / hashketball.rb
Created October 1, 2013 03:57
Hashketball
game = {
:home => {
:name => "Wu-Tang Clan",
:colors => ["Black", "Yellow"],
:players => [
{ :name => "Method Man",
:number => 6,
:shoe_size => 10,
:stats => {
:points => 49,