Skip to content

Instantly share code, notes, and snippets.

04a44d18d670a1cc327f128ed424dfc77273004670d59c2aeadbb2e247bd23473fce8244c797344df8c1115f05fcafbda3df2654f6e2bde22bbdf88dfbcc42ba1f
@pjlowry
pjlowry / dictionary.rb
Created February 11, 2013 23:17
create your own dictionary
class Dictionary
attr_accessor :new_hash
def initialize
@hash = Hash.new
end
def new_entry(word, definition)
@word = word
@definition = definition
@pjlowry
pjlowry / parcel.rb
Created February 11, 2013 22:50
Shipping cost estimator.
class Parcel
attr_accessor :new_hash
def initialize
hash_empty = Hash.new
@hash_empty = hash_empty
end
def new_box(length, width, height, weight) #inches and lbs
@hash_empty = {"length" => length, "width" => width, "height" => height, "weight" => weight}
@pjlowry
pjlowry / dice.rb
Created January 30, 2013 19:30
roll it out
class Die
attr_accessor :sides
def initialize(number)
@number = number
end
def roll
rand(1..@number)
@pjlowry
pjlowry / book.rb
Created January 30, 2013 19:29
book titles...
class Book
attr_writer :title
def initialize(title)
@title = title
end
def new_book
@title.to_s.split(/ /).map { |e| e.capitalize! }.join(" ")
@pjlowry
pjlowry / fibonacci2.rb
Created January 29, 2013 23:52
Integer'ed in your face...
class Integer
def fibonacci
if self.kind_of? Integer
if self < 0
"Must be a positive number"
else
first_num = 0
second_num = 1
return self if (0..1).include? self
(1..self).each do |counter|
@pjlowry
pjlowry / in_words.rb
Created January 29, 2013 23:50
numbers => words
class Fixnum
def in_words
one_nineteen = {"one" => "1", "two" => "2", "three" => "3", "four" => "4", "five" => "5", "six" => "6", "seven" => "7", "eight" => "8", "nine" => "9", "ten" => "10", "eleven" => "11", "twelve" => "12", "thirteen" => "13", "fourteen" => "14", "fifteen" => "15", "sixteen" => "16", "seventeen" => "17", "eighteen" => "18", "nineteen" => "19"}
tens = {"twenty" => "2", "thirty" => "3", "fourty" => "4", "fifty" => "5", "sixty" => "6", "seventy" => "7", "eighty" => "8", "ninty" => "9"}
big_nums = [" hundred ", " thousand ", " million ", " billion ", " trillion "]
arrays = self.to_s.split(//)
arrayi = arrays.map {|e| e.to_i}
arrayi = arrayi.join
@pjlowry
pjlowry / fibonacci.rb
Created January 28, 2013 23:30
0.1.1.2.3....infinity recursively
class Integer
def fibonacci
if self < 0
"Must be a positive number"
elsif self == 0
0
elsif self == 1
1
else
return (self - 1).fibonacci + (self - 2).fibonacci
def factorial(n)
if n <= 1
"1"
else (1..n).inject {|product, nb| product * nb }
end
end
puts "'#{factorial(5)}' should equal '120'."
@pjlowry
pjlowry / abc_menu.rb
Created January 24, 2013 21:22
abc menu
menu = ["a" => 10, "b" => 100, "c" => 200]
menu.each {|item, price| puts "With no discount, #{item} is #{price}."}
menu.each {|item, price| puts "With a 20% discount, #{item} is #{price * 0.8}."}