Skip to content
Open
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 hello
"Hello!"
end

def greet(name)
"Hello, #{name}!"
end
7 changes: 7 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def ftoc(degrees)
(degrees - 32).to_f * 5.0 / 9.0
end

def ctof(d)
d * 9.0 / 5.0 + 32.0
end
27 changes: 27 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def add(x, y)
x + y
end

def subtract(x, y)
x - y
end

def sum(nums)
nums.reduce(0){ |total, n| total + n }
end

def multiply(*nums)
nums.reduce(1){ |total, n| total * n }
end

def power(x, y)
x ** y
end

def factorial(x)
if x <= 1
1
else
x * factorial(x - 1)
end
end
38 changes: 29 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,41 @@

describe "#multiply" do

it "multiplies two numbers"
it "multiplies two numbers" do
expect(multiply(3,3)).to eq(9)
end

it "multiplies several numbers" do
expect(multiply(3, 4, 5)).to eq(60)
end

it "multiplies several numbers"

end

describe "#power" do
it "raises one number to the power of another number"
it "raises one number to the power of another number" do
expect(power(2, 3)).to eq(8)
end
end

# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
it "computes the factorial of 0"
it "computes the factorial of 1"
it "computes the factorial of 2"
it "computes the factorial of 5"
it "computes the factorial of 10"
it "computes the factorial of 0" do
expect(factorial(0)).to eq(1)
end

it "computes the factorial of 1" do
expect(factorial(1)).to eq(1)
end

it "computes the factorial of 2" do
expect(factorial(2)).to eq(2)
end

it "computes the factorial of 5" do
expect(factorial(5)).to eq(120)
end

it "computes the factorial of 10" do
expect(factorial(10)).to eq(3628800)
end
end
40 changes: 40 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def echo(s)
s
end

def shout(s)
s.upcase
end

def repeat(s, times=2)
if times <= 1
s
else
s + " " + repeat(s, times - 1)
end
end

def start_of_word(s, length)
s[0...length]
end

def first_word(s)
s.split(" ")[0]
end

def titleize(s)
littlewords = ["the", "and", "over"]
split = s.split(" ")
capwords = split.each_with_index.map do |word, i|
if i == 0 && split[0] == word
word.capitalize
elsif littlewords.include?(word)
word
else
word.capitalize
end
end
capwords.join(" ")
end

titleize("the bridge over the river kwai")
24 changes: 24 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def translate(s)
vowels = "aeiou".split("")
split = s.split(" ")
split.reduce([]) do |acc, word|
# begins with vowel
if vowels.include?(word[0].downcase)
word = word + "ay"
# with 3 consonants
elsif word[0..2].split("").all?{ |char| not(vowels.include?(char)) } ||
(not(vowels.include?(word[0])) && word[1..2] == "qu")
word = word[3..-1] + word[0..2] + "ay"
# with 2 consonants
elsif word[0..1].split("").all?{ |char| not(vowels.include?(char)) } ||
word[0..1].downcase == "qu"
word = word[2..-1] + word[0..1] + "ay"
# with 1 consonant
else
word = word[1..-1] + word[0] + "ay"
end
acc.push(word)
end.join(" ")
end

translate("the quick brown fox")
13 changes: 13 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def reverser
yield.split(" ").map(&:reverse).join(" ")
end

def adder(n=1)
yield + n
end

def repeater(times=1)
times.times do
yield
end
end
11 changes: 11 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "time"

def measure(times=1)
acc = []
times.times do
start = Time.now
yield
acc.push(Time.now - start)
end
acc.reduce(&:+).to_f / times.to_f
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
"Hello, #{name}!"
else
"Hello!"
end
end
end
24 changes: 24 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Book

attr_reader :title, :app_state

def initialize
@app_state = {
exceptions: "the a an and in of".split(" ")
}
@title = nil
end

def title=(s)
@title = s.split(" ").each_with_index.map do |word, i|
if i == 0
word.capitalize
elsif app_state[:exceptions].include?(word)
word
else
word.capitalize
end
end.join(" ")
end

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

def initialize
@seconds = 0
end

def pre_zero(int)
if int.to_s.length == 1
"0#{int}"
else
int.to_s
end
end

def time_string
seconds_left = seconds % 60
minutes_left = (seconds/60) % 60
hour = (seconds/60) / 60
"#{pre_zero(hour)}:#{pre_zero(minutes_left)}:#{pre_zero(seconds_left)}"
end
end
89 changes: 89 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
def ftoc(degrees)
(degrees - 32).to_f * 5.0 / 9.0
end

def ctof(d)
d * 9.0 / 5.0 + 32.0
end

# ----------------------------------------------------------------------
# Temperature
# ----------------------------------------------------------------------

class Temperature

attr_accessor :degrees

def initialize(options = {})
@degrees = options
end

def self.from_celsius(d)
Temperature.new({c: d})
end

def self.from_fahrenheit(d)
Temperature.new({f: d})
end

def in_fahrenheit
if degrees[:f]
degrees[:f]
else
ctof(degrees[:c])
end
end

def in_celsius
if degrees[:f]
ftoc(degrees[:f])
else
degrees[:c]
end
end

end

# ----------------------------------------------------------------------
# Celsius
# ----------------------------------------------------------------------

class Celsius < Temperature

attr_accessor :degree

def initialize(d)
@degree = d
end

def in_celsius
degree
end

def in_fahrenheit
ctof(degree)
end

end

# ----------------------------------------------------------------------
# Fahrenheit
# ----------------------------------------------------------------------

class Fahrenheit < Temperature

attr_accessor :degree

def initialize(d)
@degree = d
end

def in_fahrenheit
degree
end

def in_celsius
ftoc(degree)
end

end
41 changes: 41 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Dictionary

attr_accessor :entries

def initialize
@entries = {}
end

def add(keyvals)
if keyvals.is_a?(Hash)
@entries = entries.merge(keyvals)
else
@entries = entries.merge({keyvals => nil})
end
end

def keywords
entries.keys.sort
end

def include?(k)
entries.include?(k)
end

def find(s)
entries.select{ |k, v| k.include?(s) }
end

def printable
entries.sort.reduce("") do |acc, kv|
k, v = kv
s = %Q{[#{k}] "#{v}"}
if acc.empty?
s
else
acc + "\n" + s
end
end
end

end
Loading