Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 23:33 (UTC -08:00)
View GitHub Profile
@havenwood
havenwood / scrape.rb
Created July 10, 2012 20:42
scrape eet example
require 'nokogiri'
require 'open-uri'
module Scrape
def self.links site
page = Nokogiri::HTML(open site)
@links = []
page.search('link').each do |link|
@links << link.attributes['href'].value
end
@havenwood
havenwood / genes.rb
Created July 19, 2012 18:42 — forked from Poincare/gist:3055730
refactored genetic
class Chromosome
attr_accessor :bit_string, :fitness
def initialize length, bit_string = nil
@length = length
if bit_string.nil?
generate_random length
else
@bit_string = bit_string
@havenwood
havenwood / om.rb
Created July 19, 2012 22:27
DSL for marshaling Ruby to Ohm.
require 'ohm'
module Om
class << self
def connect
Ohm.connect
end
def [] key
Marshal.load(Ohm.redis.get key)
@havenwood
havenwood / celluloid-clock.rb
Created July 28, 2012 18:47
Silly Celluloid Clock Example
require 'celluloid'
class Clock
include Celluloid
def initialize
@ticker = %w[tick tock].cycle
end
def wind
@havenwood
havenwood / new-string-benchmark.rb
Created August 3, 2012 20:45
Shaughnessy String Creation Benchmarks Across VMs
#!/usr/bin/env ruby
require 'benchmark'
def run str, bench
bench.report "#{str.length + 1} chars" do
1_000_000.times do
new_string = str + 'x'
end
end
end
@havenwood
havenwood / fizz-buzz.coffee
Created August 5, 2012 21:27
FizzBuzz CoffeeScript
for i in [1..100]
console.log(['Fizz' if i % 3 is 0] + ['Buzz' if i % 5 is 0] or i)
@havenwood
havenwood / rbx-float-round.rb
Created August 8, 2012 22:33
RBX Implementation of Float#round
class Float
def round(ndigits=0)
ndigits = Rubinius::Type.coerce_to(ndigits, Integer, :to_int)
if ndigits == 0
return Rubinius.invoke_primitive :float_round, self
elsif ndigits < 0
return truncate.round ndigits
end
@havenwood
havenwood / ruby-the-easy-way.rb
Created August 24, 2012 19:25
Just pretend the rest doesn't exist...
# Code inside a 'Module' is organized into 'methods'.
# Modules are named with CamelCase and methods are named with snake_case.
module ModulesAreCamelCase # This is a Module.
# The Module's methods' go here.
end
module ModulesAreCamelCase
def self.methods_are_snake_case # This is a method.
@havenwood
havenwood / fib.lesson.rb
Last active October 9, 2015 08:47
Teaching little sis Ruby!
module Fib
def self.show this_many # this_many is an arguement.
puts this_many # arguements are available inside the method
end
end
Fib.show # If Ruby expects a method to have an arguement, you'd better provide.
# ArgumentError: wrong number of arguments (0 for 1)
Fib.show 5
>> Benchmark.measure do
| 10_000.times { "aoiudfudasf\nasoidufasjdfads\n".split("\n").reverse_each.first }
| end
=> 0.020000 0.000000 0.020000 ( 0.020835)
>> Benchmark.measure do
| 10_000.times { "aoiudfudasf\nasoidufasjdfads\n"[/(.*)\Z/] }
| end
=> 0.030000 0.000000 0.030000 ( 0.030893)