Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / palindrome.rb
Created September 13, 2011 15:54
Find Palindromes
#!/usr/bin/env ruby
require "rubygems"
gem "minitest"
require "minitest/autorun"
require "minitest/pride"
class Palindrome
def search string
@array = string.gsub(/[^0-9a-z ]/i, '').gsub(' ', '').downcase.split ''
@palindromes = []
@havenwood
havenwood / dog.rb
Created September 13, 2011 17:19
dog years
class Dog
attr_accessor :age
def dog_years
return "Gimme a human age first, damnit..." unless @age
@dog_years = 15
@dog_years += 9 if @age > 1
@dog_years += (@age - 2) * 4 if @age > 2
@dog_years
end
@havenwood
havenwood / self.rb
Created September 16, 2011 03:22
self.something
def self.something
:red
end
something #=> :red
defined? something #=> "method"
something = :blue # then local variable hijacks something, oh noes
@havenwood
havenwood / gvhd.rb
Created September 16, 2011 15:15
gvhd
require 'set'
symptoms = Set["bezoars", "turning purple", "jaundice"]
gvhd = Set["jaundice", "bloody diarrhea", "skin inflammation"]
"whipple" if gvhd.proper_subset? symptoms
@havenwood
havenwood / array.rb
Created September 16, 2011 15:21
gvhd array
symptoms = ["bezoars", "turning purple", "jaundice", "bloody diarrhea", "skin inflammation"]
gvhd = ["jaundice", "bloody diarrhea", "skin inflammation"]
gvhd.all? { |symptom| symptoms.include? symptom }
@havenwood
havenwood / method.rb
Created September 16, 2011 17:12
methods and instance variables, oh my
def x
:a_method
end
x # => :a_method
x = :a_variable
x # => :a_variable
@havenwood
havenwood / ordinalize.rb
Last active September 27, 2015 06:17
ordinalize based on active_support's
require 'minitest/autorun'
require 'minitest/pride'
class Integer
def ordinalize
suffix = if (abs % 100).between? 11, 13
'th'
else
case abs % 10
when 1; 'st'
@havenwood
havenwood / say.rb
Created September 18, 2011 06:09
Say
class String
def say
puts self
end
end
%w[cats vanilla trees].each(&:say)
#=>
cats
@havenwood
havenwood / putnprint.rb
Created September 20, 2011 16:07
Puts versus print in rbx
def puts(*a)
$stdout.puts(*a)
nil
end
def puts(*args)
if args.empty?
write DEFAULT_RECORD_SEPARATOR
else
args.each do |arg|
@havenwood
havenwood / block.rb
Created September 28, 2011 00:51
Proc Block Gibberish
def fun(&block)
@block = proc { yield }
end
fun { puts "How is this fun?" }
@block.call #=> How is this fun?