Skip to content
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

Robert Wayne's submission for the "Test First Ruby" assignment #99

Open
wants to merge 16 commits 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
2 changes: 1 addition & 1 deletion 00_hello/hello_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Open up `hello.rb` in a text editor. Save it. Run the test again.
#
# rake
#
#ra
# ## Watch it fail
#
# Now you should see an error like this:
Expand Down
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(f_temp)
c_temp = (f_temp - 32) * 5 / 9
end

def ctof(c_temp)
f_temp = c_temp.to_f * 9 / 5 + 32.0
end
17 changes: 17 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def add(a, b)
a + b
end

def subtract(a, b)
a - b
end

def sum(arr)
sum = 0

arr.each do |i|
sum += i
end

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

def shout(s)
s.upcase
end

def repeat(s, n_times = 2)
repeat_s = s
repeat_s += " #{s}" * (n_times - 1)
end

def start_of_word(word, characters)
partial_word = ""

characters.times do |c|
partial_word += word[c]
end

partial_word
end

def first_word(words)
first_word = ""

i = 0
until words[i] == " " do
first_word += words[i]
i += 1
end

first_word
end


def titleize(phrase)
word = ""
words = []

phrase.length.times do |i|
if phrase[i] == " "
words.push(word.downcase)
word = ""
else i == phrase.length - 1
word += phrase[i]
end
words.push(word.downcase) if i == phrase.length - 1
end

words.each_with_index do |word, i|
words[i][0] = word[0].upcase unless word == "the" || word == "and" || word == "over"
words[i][0] = word[0].upcase if i == 0
end

words = words.join(" ")
end
26 changes: 26 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def translate(phrase)
words = phrase.split(' ')

words.each_with_index do |word, i|
translated_word = word.downcase

case translated_word[0..2]
when /thr/, /sch/, /[^aeiou]qu/
translated_word += translated_word[0..2]
translated_word = translated_word[3..-1]
when /ch./, /qu./, /th./, /br./
translated_word += translated_word[0..1]
translated_word = translated_word[2..-1]
when /[^aeiou]../
translated_word += translated_word[0]
translated_word = translated_word[1..-1]
else

end

translated_word += "ay"
words[i] = translated_word
end

words = words.join(" ")
end
29 changes: 29 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def word_reverse(string)
reverse_string = ""

i = -1
string.length.times do
reverse_string += string[i]
i -= 1
end

reverse_string
end

def reverser
text = yield.to_s.split

text.each_with_index do |word, i|
text[i] = word_reverse(word).downcase
end

text = text.join(" ")
end

def adder(n = 1)
yield + n
end

def repeater(n = 1)
n.times { yield }
end
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(number_of_times = 1)
start = Time.now
number_of_times.times { yield }
finish = Time.now
duration = finish - start
average_duration = duration / number_of_times
end
5 changes: 5 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Friend
def greeting(someone = false)
someone ? "Hello, #{someone}!" : "Hello!"
end
end
44 changes: 44 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Book
def initialize
@title = ""
end

def title
@title
end

def title=(new_title)
@title = titleize(new_title)
end

def titleize(phrase)
word = ""
words = []

phrase.length.times do |i|
if phrase[i] == " "
words.push(word.downcase)
word = ""
else i == phrase.length - 1
word += phrase[i]
end
words.push(word.downcase) if i == phrase.length - 1
end

words.each_with_index do |word, i|
words[i][0] = word[0].upcase unless lowercase_word?(word)
words[i][0] = word[0].upcase if i == 0
end

words = words.join(" ")
end

def lowercase_word?(word)
lowercase_words = ["the", "and", "over", "under", "of", "in", "a", "an"]
lowercase_words.each do |lowercase_word|
return true if word.downcase == lowercase_word
end

return false
end
end
19 changes: 19 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Timer
def initialize
@seconds = 0
end

attr_accessor :seconds

def time_string
hours = @seconds / 60 / 60
minutes = (@seconds / 60) - (hours * 60)
seconds = @seconds - (hours * 60 * 60) - (minutes * 60)

hours_string = hours < 10 ? "0#{hours}" : hours.to_s
minutes_string = minutes < 10 ? "0#{minutes}" : minutes.to_s
seconds_string = seconds < 10 ? "0#{seconds}" : seconds.to_s

"#{hours_string}:#{minutes_string}:#{seconds_string}"
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)
@in_celsius = options[:c] ? options[:c] : ftoc(options[:f])
@in_fahrenheit = options[:f] ? options[:f] : ctof(options[:c])
end

attr_reader :in_celsius, :in_fahrenheit

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

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

def ftoc(in_fahrenheit)
in_celsius = (in_fahrenheit - 32) * 5 / 9
end

def ctof(in_celsius)
in_fahrenheit = in_celsius.to_f * 9 / 5 + 32.0
end
end

class Celsius < Temperature
def initialize(in_celsius)
@in_celsius = in_celsius
@in_fahrenheit = ctof(in_celsius)
end
end

class Fahrenheit < Temperature
def initialize(in_fahrenheit)
@in_celsius = ftoc(in_fahrenheit)
@in_fahrenheit = in_fahrenheit
end
end



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

def initialize
@entries = {}
end

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

if entry.is_a?(Hash)
entry.each_key { |key| @entries[key.to_s] = entry[key].to_s }
end
end

def include?(keyword)
@entries.each_key do |entry_key|
return true if entry_key == keyword
end

return false
end

def find(keyword)
entry = {}

@entries.each_key do |key|
entry[key] = @entries[key] if key.include?(keyword)
end

return entry
end

def keywords
keywords = @entries.keys.to_a
keywords.sort
end

def printable
printable_text = []

keywords.each do |key|
printable_text.push("[#{key}] \"#{@entries[key]}\"")
end

return printable_text.join("\n")
end

end
Loading