Skip to content
7 changes: 7 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def greet(name)
return "Hello, #{name}!"
end

def hello
return "Hello!"
end
9 changes: 9 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def ftoc(fahrenheit)
celsius = (fahrenheit-32)*5/9
return celsius
end

def ctof(celsius)
fahrenheit = (celsius*9.0/5.0)+32
return fahrenheit
end
19 changes: 19 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def add(num1, num2)
sum = num1 + num2
return sum
end

def subtract(num1, num2)
difference = num1 - num2
return difference
end

def sum(arr)
total = 0
arr.each{|x| total += x }
return total
end

def multiply

end
42 changes: 42 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def echo(str)
return str
end

def shout(str)
return str.upcase
end

def repeat(str, n=2)
# array where each element is str and there are n of them.
arr = []
n.times{arr.push(str)}
return arr.join(" ")
# then i can join them with a space and return that
# return str+" "+str
end

def start_of_word(word, int)
word[0..(int-1)]
end

def first_word(str)
arr = str.split(" ")
return arr[0]
end

def titleize(str)
little_words = ['and', 'the', 'over']
arr = str.split(" ")
upcase_array = []
i = 0
arr.each do |word|
i += 1
if little_words.include?(word) && i != 1
upcase_array.push(word)
else
upcase_array.push(word.capitalize)
end
end
new_str = upcase_array.join(" ")

end
23 changes: 23 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def translate(word)
vowels = ['a','e','i','o','u']

pre_latin = word.split(" ")

arr=[]

pre_latin.each do |piggy|

if vowels.include?(piggy[0])
arr.push(piggy + "ay")
elsif piggy[0..1] == "qu"
arr.push(piggy[2..(piggy.length-1)] + piggy[0..1] + "ay")
elsif vowels.include?(piggy[1])
arr.push(piggy[1..(piggy.length-1)] + piggy[0] + "ay")
elsif vowels.include?(piggy[2])
arr.push(piggy[2..(piggy.length-1)] + piggy[0..1] + "ay")
else
arr.push(piggy[3..(piggy.length-1)] + piggy[0..2] + "ay")
end
end
latinized = arr.join(" ")
end
14 changes: 14 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def reverser
reversed = yield.split(" ").map{|word| word.reverse}
return reversed.join(" ")
end

def adder(n=1)
yield + n
end

def repeater(n=1)
block_was_executed = false
n.times { yield }
block_was_executed = true
end
5 changes: 5 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def measure(n=1)
start_time = Time.now
n.times{ yield }
elapsed_time = (Time.now - start_time)/n
end
9 changes: 9 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Friend
def greeting(name=nil)
if name != nil
"Hello, #{name}!"
else
"Hello!"
end
end
end
21 changes: 21 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Book

def title=(title)
no_nos = ['and','in','the','of','a','an']
i = 0

@title = title.split(" ").map! do |word|
i+=1
if !no_nos.include?(word) || i == 1
word.capitalize
else
word
end
end.join(" ")
end

def title
@title
end

end
20 changes: 20 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Timer

def initialize
self.seconds = (0)
end

def seconds=(seconds)
@seconds = seconds
@time_string = Time.at(seconds).utc.strftime("%H:%M:%S")
end

def seconds
@seconds
end

def time_string
@time_string
end

end
49 changes: 49 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Temperature
attr_accessor :f, :c

def Temperature.from_celsius temp
Temperature.new(:c => temp)
end

def Temperature.from_fahrenheit temp
Temperature.new(:f => temp)
end

def initialize args
@f = args[:f]
@c = args[:c]
if @f
@c = ftoc
else
@f = ctof
end
end

def ftoc
(@f-32)*5/9.0
end

def ctof
(@c*9/5.0)+32
end

def in_fahrenheit
@f
end

def in_celsius
@c
end
end

class Celsius < Temperature
def Celsius.new args
Temperature.new(:c => 50)
end
end

class Fahrenheit < Temperature
def Fahrenheit.new args
Temperature.new(:f => 50)
end
end
38 changes: 38 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Dictionary

attr_reader :entries

def initialize
@entries = {}
end

def add items
if items.is_a?(Hash)
items.each {|key, definition| @entries[key] = definition}
else
@entries[items] = nil
end
@entries = Hash[@entries.sort_by {|key, definition| key}]
end

def find partial
@entries.select {|key, definition| key.include? partial}
end

def include? key
@entries.has_key? key
end

def keywords
@entries.keys
end

def printable
output = ""
@entries.each do |key, definition|
output += "[#{key}] \"#{definition}\"\n"
end
output.chomp
end

end
49 changes: 49 additions & 0 deletions 12_rpn_calculator/rpn_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class RPNCalculator

attr_reader :value

def initialize
@value = 0
@entered = []
end

def divide
operate &:/
end

def evaluate formula
formula = tokens formula
formula.each do |token|
(token.class == Symbol ? operate(&token) : push(token))
end
return @value
end

def minus
operate &:-
end

def operate &operator
raise "calculator is empty" if @entered.length < 2
@value = @entered.push(@entered.pop(2).reduce &operator)[-1]
end

def plus
operate &:+
end

def push num
@entered << num.to_f
end

def times
operate &:*
end

def tokens token_list
token_list.split(" ").map do |token|
(token.to_i.to_s == token ? token.to_i : token.to_sym)
end
end

end
5 changes: 5 additions & 0 deletions 13_xml_document/xml_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class XmlDocument

def hello
end
end
24 changes: 24 additions & 0 deletions 14_array_extensions/array_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Array

def sum
result = 0
self.each do |i|
result += i
end
result
end

def square
result = []
self.map do |i|
result << i * i
end
result
end

def square!
self.map! do |i|
i * i
end
end
end
Loading