Skip to content

Instantly share code, notes, and snippets.

@mzemel
mzemel / practices.rb
Last active August 29, 2015 14:20
What do
########
# Hash #
########
def function(opts)
request = {}
request[:name] = opts[:name] if opts[:name]
request[:email] = opts[:email] if opts[:email]
request[:address] = opts[:name] if opts[:name]
post_to_service(request)
end
@mzemel
mzemel / clojure.clj
Last active August 29, 2015 14:10
Problem 3 in Project Euler
(ns clojure-euler.core
(:gen-class))
;The prime factors of 13195 are 5, 7, 13 and 29.
;What is the largest prime factor of the number 600851475143 ?
(defn divides_cleanly
"[arg1 arg2] True if the remainder of arg1 / arg2 is 0"
[arg1 arg2]
@mzemel
mzemel / attr_accessor.rb
Created October 4, 2014 06:50
Implementation of attr_accessor
module Fatter
def self.included(base)
base.class_eval do
def self.fattr_accessor(_symbol)
raise "NotASymbol" unless _symbol.is_a? Symbol
ivar_as_symbol = ("@" + _symbol.to_s).to_sym
define_method(_symbol.to_s) do
self.instance_variable_get(ivar_as_symbol)
end
@mzemel
mzemel / sinatra.rb
Created October 3, 2014 18:16
Sinatra Example
# gem install sinatra
# sinatra.rb
require 'rubygems'
require 'sinatra'
get "/" do
"This is the main page"
end
@mzemel
mzemel / send.rb
Created September 26, 2014 21:45
class Person
attr_accessor :name, :age, :weight
def initialize(hash)
hash.each do |key, value|
setter_method = key.to_s + "="
send(setter_method, value)
end
end
end
@mzemel
mzemel / meta.rb
Created September 26, 2014 21:01
META
methods = ['foo', 'bar', 'baz']
methods.each do |method_name|
define_method(method_name) do |arg|
puts arg.upcase
end
end
foo("hi")
# => HI
@mzemel
mzemel / hangman.rb
Created July 14, 2014 03:52
Hangman program
# Let's play hangman!
module HangmanErrors
class YaDunGoofedError < StandardError; end
class HolyShitYouWon < StandardError; end
end
class TheGame
include HangmanErrors
attr_accessor :round
10.times do
@rate ||= 0.0
start_time = Time.now.to_i
start_count = Property.external.count.to_f
sleep rand(30..60)
end_time = Time.now.to_i
end_count = Property.external.count.to_f
properties_per_second = ((end_count - start_count) / (end_time - start_time) )
@rate += properties_per_second
end
@mzemel
mzemel / hs_citycleaner.rb
Created July 2, 2013 01:58
Messily cleans cities
cities_array = IO.readlines('cities.txt')
cities_array.each do |entry|
entry.gsub!("\\n","")
entry.gsub!("{\"Name\":\"","")
entry.gsub!(/\",\"Type\":\"City\",\"BID\":null,\"City\":null,\"State\":\"..\"}/,"")
entry.gsub!("][","],[")
end
cities_array = cities_array.first.split(",").select{ |i| i != "[]" }
cities_array.each do |city|
city.gsub!("[","")
@mzemel
mzemel / hs_cityscraper.sh
Created July 2, 2013 01:57
Gets all cities (messy)
#!/bin/bash
for a in {a..z}
do
for b in {a..z}
do
q=$a$b
curl "http://www.homeseekers.com/Include/AJAX/MapSearch/GetLocations.aspx?q="$q"&type=city" >> cities.txt
done
done