Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(who)
"Hello, #{who}!"
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(temp)
(temp - 32) * 5.0/9
end

def ctof(temp)
temp * 9.0/5 + 32
end
36 changes: 36 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def add(a, b)
a + b
end

def subtract(a,b)
a - b
end

def sum(nums)
return 0 if nums == []
sum = 0
nums.each do |n|
sum += n
end
sum
end

def multiply(*args)
product = 1
args.each do |n|
product *= n
end
product
end

def power(base, exp)
base ** exp
end

def factorial(num)
product = 1
(1..num).reverse_each do |n|
product *= n
end
product
end
34 changes: 25 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,37 @@

describe "#multiply" do

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

it "multiplies several numbers" do
expect(multiply(6,7,8)).to eq(336)
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(9,2))).to eq(81)
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
29 changes: 29 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def echo(word)
word
end

def shout(word)
word.upcase
end

def repeat(word, num=2)
word += ((' ' + word) * (num - 1))
end

def start_of_word(word, num=1)
word[0...num]
end

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

def titleize(words)
return words.capitalize unless words.include?(' ')
little_words = ['a', 'the', 'in', 'to',
'and', 'an', 'over']
words = words.split(' ').each_with_index do |word, i|
word.capitalize! unless little_words.include?(word) && i != 0
end
words.join(' ')
end
53 changes: 53 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
def translate(words)
vowels = ['a', 'e', 'i', 'o', 'u']
special_sequences = ['qu', 'squ']

words = words.split(' ').each do |word|

# deal with words that start with vowel
next if starts_with_vowel?(word, vowels)

# deal with words that start with special sequence
next if starts_with_special_sequence?(word, special_sequences)

# deal with words that start with consonant
next if starts_with_consonant(word, vowels)

end

words.join(' ')

end

def starts_with_consonant(word, vowels)
i = word.length - 1
vowels.each do |vowel|
unless word.index(vowel).nil?
if word.index(vowel) < i
i = word.index(vowel)
end
end
end
start = word.slice!(0...i)
word << start + 'ay'

end

def starts_with_vowel?(word, vowels)
if vowels.include?(word[0])
word << 'ay'
return true
end
false
end

def starts_with_special_sequence?(word, sequences)
sequences.each do |seq|
if word.index(seq) == 0
word.slice!(0...seq.length)
word << seq + 'ay'
return true
end
end
false
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()
return yield.reverse if yield.split(' ').size == 1
yield.split(' ').each { |w| w.reverse! }.join(' ')
end

def adder(num=1)
yield + num
end

def repeater(repeats=1)
repeats.times do
yield
end
end
4 changes: 2 additions & 2 deletions 05_silly_blocks/silly_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
describe "adder" do
it "adds one to the value returned by the default block" do
expect(adder do
5
5
end).to eq(6)
end

it "adds 3 to the value returned by the default block" do
expect(adder(3) do
5
5
end).to eq(8)
end
end
Expand Down
7 changes: 7 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def measure(n=1)
elapsed_time = Time.now
n.times do
yield
end
( Time.now - elapsed_time ) / n
end
7 changes: 7 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Friend

def greeting(friend='')
return "Hello!" if friend == ''
"Hello, #{friend}!"
end
end
13 changes: 13 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Book
attr_accessor :title

def title
non_caps = ['and', 'in', 'of', 'the', 'a', 'an']
@title = @title.split(' ').each_with_index do |word, i|
word.capitalize! unless non_caps.include?(word) && i != 0
word
end
@title.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)
@seconds = seconds
end

def time_string
time = @seconds
hours, mins, secs = 0, 0, 0
while time > 60
if time >= 60 * 60
hours = time / 60 / 60
time -= hours * 60 * 60
elsif time >= 60
mins = time / 60
time -= mins * 60
end
end
human_time = "#{sprintf '%02d', hours}:#{sprintf '%02d', mins}:#{sprintf '%02d', time}"
end
end
41 changes: 41 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Temperature

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

def in_fahrenheit
return @options[:f] unless @options[:f].nil?
(@options[:c] * 9)/5.0 + 32
end

def in_celsius
return (@options[:f] - 32) * 5/9 if @options[:c].nil?
@options[:c]
end

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

def self.in_celsius
@options[:c]
end

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

end

class Celsius < Temperature
def initialize(temp)
@options = {:c => temp}
end
end

class Fahrenheit < Temperature
def initialize(temp)
@options = {:f => temp}
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_accessor :entries

def initialize(opts = {})
@entries = opts
end

def add(entry)
if entry.is_a?(String)
@entries[entry] = nil
else
@entries.merge!(entry)
end
end

def include?(keyword)
keywords.include?(keyword)
end

def find(keyword)
return @entries if @entries.empty?
@entries.select { |word, definition| word.match(keyword)}
end

def printable
printout = ''
keywords.each_with_index do |key, i|
printout << "[#{key}] \"#{@entries[key]}\""
printout << "\n" if i < keywords.size-1
end
printout
end

def keywords
@entries.keys.sort
end

end
Loading