Skip to content

Instantly share code, notes, and snippets.

View kennyt's full-sized avatar

Kenny Tran kennyt

  • San Francisco, CA
View GitHub Profile
@kennyt
kennyt / gist:3963805
Created October 27, 2012 09:47
14_array_extensions
class Array
def sum
answer = 0
self.each do |number|
answer = answer + number
end
answer
end
def square
@kennyt
kennyt / gist:3963803
Created October 27, 2012 09:46
12_rpn_calculator
class RPNCalculator
attr_accessor :calculator
def initialize
@calculator = []
end
def push (number)
@calculator << number
end
@kennyt
kennyt / gist:3963802
Created October 27, 2012 09:45
11_dictionary
class Dictionary
def initialize
@dictionary = {}
end
def entries
@dictionary
end
def add (entry)
@kennyt
kennyt / gist:3963800
Created October 27, 2012 09:44
10_timer
class Timer
def initialize
@seconds = 0
end
def seconds= (seconds)
@seconds = seconds
end
@kennyt
kennyt / gist:3963796
Created October 27, 2012 09:44
09_book_titles
class Book
attr_accessor :title
def title= (title)
do_not_capitalize = %w{the and a an in of}
answer = []
title.split(' ').each do |word|
do_not_capitalize.include?(word) ? word = word.downcase : word = word.capitalize
word == "i" ? word = word.capitalize : word
answer << word
@kennyt
kennyt / gist:3963791
Created October 27, 2012 09:43
08_temperature
class Temperature
def initialize (input)
@temperature = input
end
def ftoc (number)
number * 9/5.to_f + 32
end
def ctof (number)
@kennyt
kennyt / gist:3963782
Created October 27, 2012 09:42
07_hello_friend
class Friend
def greeting(person = nil)
person.nil? ? "Hello!" : "Hello, #{person}!"
end
end
@kennyt
kennyt / gist:3963780
Created October 27, 2012 09:42
06_performance_monitor
def measure number=1, &someproc
start_time = Time.now
number.times {someproc.call}
run_time = (Time.now - start_time) / number
end
@kennyt
kennyt / gist:3963777
Created October 27, 2012 09:41
05_silly_blocks
def reverser &proc
answer = []
proc.call.split(' ').each do |word|
answer<<word.reverse
end
answer.join(' ')
end
def adder number=1, &proc
number + proc.call
@kennyt
kennyt / gist:3963773
Created October 27, 2012 09:39
04_pig_latin
def translate (phrase)
vowels = %w{a e i o u}
words = phrase.split(' ')
first_letter_of_phrase = phrase.scan(/./).first
new_phrase = []
words.each do |word|
word = word.downcase
first_letter = word.scan(/./).first