Skip to content

Instantly share code, notes, and snippets.

# There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.
# There are 64 squares on a chessboard.
# Write a program that shows
# - how many grains were on each square, and
# - the total number of grains
# ## For bonus points
@loganhasson
loganhasson / person.rb
Last active December 25, 2015 04:29
Mass assign arbitrary attributes on initialization
class Person
attr_accessor :name, :birthday, :hair_color, :eye_color, :height, :weight,
:handed, :complexion, :t_shirt_size, :wrist_size, :glove_size,
:pant_length, :pant_width
def initialize(data)
data.each do |attrib, value|
self.send("#{attrib}=", value) unless !self.respond_to?(attrib.to_sym)
end
class TriangleError < StandardError
end
class Triangle
attr_accessor :a, :b, :c
def initialize(a, b, c)
@a = a
@b = b
@loganhasson
loganhasson / jukebox_spec.rb
Created October 9, 2013 00:31
RSpec coverage using simplecov
require 'simplecov'
SimpleCov.start
require 'json'
require 'rspec'
require_relative 'jukebox'
require_relative 'song'
describe Song do
class Anagram
attr_accessor :word
def initialize(word)
@word = word
end
# def match(anagram_array)
# anagram_array.map do |potential|
@loganhasson
loganhasson / command.line.reddit.rb
Last active December 24, 2015 13:19
Browse reddit via commandline.
require 'json'
require 'rest_client'
require 'pry'
require 'awesome_print'
require 'colorize'
system("clear")
puts("Starting up...")
if ARGV.first
while true
system("clear")
longest_upvote = 0
Post::POSTS.each do |post|
if post.upvotes.to_s.length > longest_upvote
longest_upvote = post.upvotes.to_s.length
end
end
Post::POSTS.each do |post|
require 'pry'
require 'awesome_print'
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigeons.
pigeon_data = {
@loganhasson
loganhasson / api.parsing.rb
Last active December 24, 2015 11:49
reddit api parser
require 'json'
require 'rest_client'
require 'pry'
require 'awesome_print'
reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json'))
reddit_html = " "
def make_post_html(post_hash)
@loganhasson
loganhasson / working.tower.of.hanoi.rb
Created October 2, 2013 04:01
Working Tower of Hanoi that I don't entirely understand, but was able to piece together based on reading.
a = [1,2,3,4]
b = []
c = []
def move_disc(num_discs, from_peg, via_peg, to_peg)
watch_hannoi(from_peg, via_peg, to_peg)
sleep(1)
if num_discs == 1
to_peg.unshift(from_peg.shift)
else