Skip to content

Instantly share code, notes, and snippets.

View michaelrkn's full-sized avatar

Michael Kaiser-Nyman michaelrkn

View GitHub Profile
@michaelrkn
michaelrkn / in_words.rb
Created June 16, 2012 00:10
numbers in words
class Integer
def in_words
up_to_twenty = { 0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
@michaelrkn
michaelrkn / in_words_-1.rb
Created June 16, 2012 01:26
numbers in words -1
class Integer
def in_words
if self < 20
{0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@michaelrkn
michaelrkn / in_words_-2.rb
Created June 16, 2012 01:29
numbers in words -2
class Integer
def in_words
if self < 20
{0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@michaelrkn
michaelrkn / in_words_-3.rb
Created June 16, 2012 01:34
numbers in words -3
class Integer
def in_words
if self < 20
{0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@michaelrkn
michaelrkn / in_words_-4.rb
Created June 16, 2012 01:35
numbers in words -4
class Integer
def in_words
if self < 20
{0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@michaelrkn
michaelrkn / in_words_-5.rb
Created June 16, 2012 01:36
numbers in words -5
class Integer
def in_words
if self < 20
{0 => "\b",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@michaelrkn
michaelrkn / in_words_-6.rb
Created June 16, 2012 01:36
numbers in words - 6
class Integer
def in_words
if self < 20
{1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
@michaelrkn
michaelrkn / in_words_ultimate.rb
Created June 16, 2012 01:51
numbers in words ultimate edition
class Integer
def in_words
if self < 20
{0 => "\b", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen"}[self]
elsif self < 100
"#{{20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", 90 => "ninety"}[self - (self % 10)]} #{(self % 10).in_words}"
elsif self < 1_000
"#{(self / 100).in_words} hundred #{(self % 100).in_words}"
else
{10 ** 12 => "trillion", 10 ** 9 => "billion", 10 ** 6 => "million", 10 ** 3 => "thousand"}.each { |number, word| return "#{(self / number).in_words} #{word} #{(self % number).in_words}" if (self / number) >= 1 && (self / number) < 1_000 }
@michaelrkn
michaelrkn / factorial.rb
Last active December 11, 2015 16:48 — forked from pjlowry/factorial.rb
def factorial(n)
if n < 0
raise "You can't take the factorial of a negative number."
elsif n == 0
1
else
(1..n).inject(:*)
end
end
@michaelrkn
michaelrkn / gist_spec.rb
Last active December 15, 2015 02:39
webmock stub example
require 'spec_helper'
describe Gist do
context '.create' do
it 'POSTs a new Gist to the user\'s account' do
gist = {:public => 'true',
:description => 'a test gist',
:files => {'test_file.rb' => {:content => 'puts "hello world!"'}}}
stub = stub_request(:post, "https://#{GITHUB_USERNAME}:#{GITHUB_PASSWORD}@api.github.com/gists").with(:body => gist.to_json)
Gist.create(gist)