Skip to content

Dexter Ford - Test-First Ruby #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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(farenheit)
celsius = (farenheit - 32) * 5.0/9.0
end

def ctof(celsius)
farenheit = ( celsius * 9.0/5.0 ) + 32
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(in1, in2)
result = in1 + in2
end

def subtract(in1, in2)
result = in1 - in2
end

def sum(array_of_ins)
out = array_of_ins.inject(0) { |running_total, item| running_total + item }
end

def multiply(in1, in2)
out = in1 * in2
end

def power(input, to_power)
out = input ** to_power
end

def factorial(input)
if input == 0
out = 1
elsif input > 0
out = input * factorial(input - 1)
end
end
32 changes: 24 additions & 8 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(5,5)).to eq(25)
end

it "multiplies several numbers"
it "multiplies several numbers" do
expect(multiply(-2,9)).to eq(-18)
end

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(3,3)).to eq(27)
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
28 changes: 28 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def echo(input)
input
end

def shout(input)
out = input.upcase
end

def repeat(input, how_many_times=2)
out = input + ( " #{input}" * ( how_many_times - 1 ))
end

def start_of_word(string, index=0)
out = string[0..index-1]
end

def first_word(string)
array = string.split(" ")
out = array[0]
end

def titleize(string)
array = string.split(" ")
array = array.each_with_index { |item, index|
item.capitalize! unless item == "and" || ( item == "the" && index != 0 ) || item == "over"
}
out = array.join(" ")
end
30 changes: 30 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def translate(string)
array = string.split(" ")
vowels = ["a", "e", "i", "o", "u"]
double_consonants = ["ch", "tr", "ph", "gr", "br", "gl", "sh", "fr", "bl", "th", "sp", "qu"]
triple_consonants = ["thr", "spl", "shr", "chr", "str", "sch", "squ"]
array = array.each_with_index do |item, index|
if vowels.include?(item[0])
item = item << "ay"
elsif triple_consonants.include?(item[0..2])
item = item << item[0..2] << "ay"
item[0..2] = ""
elsif double_consonants.include?(item[0..1])
item = item << item[0..1] << "ay"
item[0..1] = ""
elsif item[0] == item[0].upcase
item = item << item[0].downcase << "ay"
item[1] = item[1].upcase
item[0] = ""
else
item = item << item[0] << "ay"
item[0] = ""
end
end
out = array.join(" ")
if out[-4] == "."
out[-4] = ""
out << "."
end
out
end
9 changes: 8 additions & 1 deletion 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@

# Test-driving bonus:
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
it "retains capitalization" do
s = translate("Meowing Monkey")
expect(s).to eq("Eowingmay Onkeymay")
end
# * retain the punctuation from the original phrase

it "retains punctuation" do
s = translate("You suck.")
expect(s).to eq("Ouyay ucksay.")
end
end
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
words = yield.split(" ")
words = words.each { |word| word.reverse! }
out = words.join(" ")
end

def adder(how_much=1)
out = yield + how_much
end

def repeater(how_much=1)
how_much.times { yield }
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.now
n.times { yield }
out = ( Time.now - start ) / 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(who=nil)
if who == nil
"Hello!"
else
"Hello, #{who}!"
end
end
end
14 changes: 14 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Book
attr_accessor :title
def title
articles = ["and", "in", "the", "of", "a", "an"]
words = @title.split(" ")
words = words.each_with_index { |word, index|
word.capitalize! unless articles.include? word
if index == 0
word.capitalize!
end
}
out = words.join(" ")
end
end
29 changes: 29 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Timer
attr_accessor :seconds
def initialize
@seconds = 0
end
def time_string
"#{padded(hours)}:#{padded(minutes)}:#{padded(seconds)}"
end
def seconds
seconds = @seconds % 60
end
def minutes
minutes = ( @seconds - ( hours * 3600 )) / 60
end
def hours
hours = @seconds / 3600
end
def padded(value)
value = value.to_s
digits = value.length
if digits == 0
"00"
elsif digits == 1
"0" + value
else
value
end
end
end
36 changes: 36 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Temperature
def initialize(opts = {})
@fahrenheit, @celsius = opts[:f], opts[:c]
if @fahrenheit != nil && @celsius == nil
@celsius = ( @fahrenheit - 32 ) * 5.0/9.0
elsif @celsius != nil && @fahrenheit == nil
@fahrenheit = (@celsius * 9.0/5.0) + 32
end
end
def in_fahrenheit
@fahrenheit
end
def in_celsius
@celsius
end
def self.from_celsius(celsius_value)
Temperature.new(c: celsius_value)
end
def self.from_fahrenheit(fahrenheit_value)
Temperature.new(f: fahrenheit_value)
end
end

class Celsius < Temperature
def initialize(value)
@celsius = value
@fahrenheit = ( value * 9.0 / 5.0 ) + 32
end
end

class Fahrenheit < Temperature
def initialize(value)
@fahrenheit = value
@celsius = ( value - 32 ) * 5.0/9.0
end
end
36 changes: 36 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Dictionary
def initialize
@d = {}
end
def entries
@d
end
def add(entry)
if entry.is_a?(Hash)
@d.merge!(entry)
elsif entry.is_a?(String)
@d[entry] = nil
end
end
def keywords
@d.keys.sort!
end
def include?(entry)
@d.keys.include?(entry)
end
def find(entry)
results = {}
@d.each { |key, value|
if key.start_with?(entry)
results[key] = value
end
}
results
end
def printable
output = @d.sort.map { |key, value|
"[#{key}] \"#{value}\""
}
output.join "\n"
end
end
Loading