Skip to content

learn ruby #75

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 24 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
Binary file added .DS_Store
Binary file not shown.
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(f)
(f-32)*(5.0/9.0)
end

def ctof(c)
f=(c*(9.0/5.0))+32
end
23 changes: 23 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def add(x,y)
sum = x + y
end

def subtract(x,y)
subtract = x - y
end

def sum(array)
sum = 0
array.each do |x|
sum += x
end
sum
end

def multiply(num1, *nums)
product = num1
product.each do |x|
pruduct *= x
end
product
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(x)
x
end

def shout(x)
x.upcase
end

def repeat(word, n=2)
(word.split* n).join(" ")
end

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

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

def titleize(word)
word.capitalize!
words = word.split(" ")
words.each do |x|
x.capitalize! unless ["and", "over", "the"].include? x
end
words.join(" ")
end
73 changes: 73 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# def translate(arr)
# vowels = %w{a e i o u y}
# consonants = %w{b c d f g h j k l m n p q r s t v w x y z}

# a = arr.split.map do |word|
# if vowels.include?(word[0])
# word + "ay"
# elsif word[0..1] == "qu"
# word[2..-1] + "quay"
# elsif word[0..2] == "sch"
# word[3..-1] + "schay"
# elsif word[0..2] == "squ"
# word[3..-1] + "squay"
# elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2])
# word[3..-1] + word[0..2] + "ay"
# elsif consonants.include?(word[0]) && consonants.include?(word[1])
# word[2..-1] + word[0..1] + "ay"
# elsif consonants.include?(word[0])
# word[1..-1] + word[0] + "ay"
# end
# end

# a.join(" ")
# end


# Word_array = Word.split

def translate(arr)

alpha = ('a'..'z').to_a
vowels = %w[a e i o u]
consonants = alpha - vowels

a = arr.split.map do |word|
if vowels.include?(word[0])
word + 'ay'
elsif word[0..1] == 'qu'
word[2..-1] + 'quay'
elsif word[0..2] == 'squ'
word[3..-1] + 'squay'
elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2])
word[3..-1] + word[0..2] + 'ay'
elsif consonants.include?(word[0]) && consonants.include?(word[1])
word[2..-1] + word[0..1] + 'ay'
else consonants.include?(word[0])
word[1..-1] + word[0] + 'ay'
end
end

a.join(" ")
end





# def consonant?(character)
# Consonants.include?(character)
# end



# def translate(word)
# if Vowels.include?(word[0])
# word + 'ay'
# elsif Consonants.include?(word[0]) &&
# Consonants.include?(word[1])
# word[2..-1] + word[0..1] + 'ay'
# else Consonants.include?(word[0])
# word[1..-1] + word[0..0] + 'ay'
# end
# end
20 changes: 20 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def reverser

words = yield
output = []

words.split.map do |word|
output << word.reverse
end
output.join(' ')
end

def adder(n=1)
yield + n
end

def repeater(n=1)
n.times do
yield
end
end
10 changes: 10 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def measure(n=1)
total = 0
n.times do
start_time = Time.now
yield
end_time = Time.now
total += end_time - start_time
end
total / 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
39 changes: 39 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# class Book

# attr_accessor :title

# def title=(some_word)
# # word_arr = some_word.split(' ')

# # word_arr.map do |e|
# # e[0].capitalize
# @title = some_word.capitalize
# end

# end

class Book
def title
@title
end

def title=(title)
special_words = %w(and in the of a an)
formatted_title = []

@title = title.split.each_with_index do |w,i|
case
when i == 0
formatted_title << w.capitalize

when i > 0 && !special_words.include?(w)
formatted_title << w.capitalize

when special_words.include?(w)
formatted_title << w
end
end

@title = formatted_title.join(" ")
end
end
26 changes: 26 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Timer
attr_accessor :seconds
def initialize
@seconds = 0
end

def time_string
[hours, minutes, m_seconds].map(&method(:padded)).join(":")
end

def hours
seconds / 3600
end

def minutes
(seconds % 3600)/60
end

def m_seconds
(seconds % 60)
end

def padded(n)
"#{n}".rjust(2, '0')
end
end
24 changes: 12 additions & 12 deletions 09_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
# describe 'padded' do
# it 'pads zero' do
# expect(@timer.padded(0)).to eq('00')
# end
# it 'pads one' do
# expect(@timer.padded(1)).to eq('01')
# end
# it "doesn't pad a two-digit number" do
# expect(@timer.padded(12)).to eq('12')
# end
# end

describe 'padded' do
it 'pads zero' do
expect(@timer.padded(0)).to eq('00')
end
it 'pads one' do
expect(@timer.padded(1)).to eq('01')
end
it "doesn't pad a two-digit number" do
expect(@timer.padded(12)).to eq('12')
end
end

end
59 changes: 59 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Did not know what an options was or how to implement it. Found the use case on stack overflow and was able to implement it here. STill a little confusing but I get the general idea to have the option to use a has as an argument in your fuction. I was also able to re-use code from the previous temperature section.
class Temperature
attr_accessor :f, :c

def initialize (options={}) #Utilized options hash for fahrenheit/celsius methods
@f = options[:f]
@c = options[:c]

if f
@c = Temperature.ftoc(@f) #Converts fahrenheit to celsius using the Temperature.ftoc. Reused
else
@f = Temperature.ctof(@c) #Converts celcius to fahrenheit using the Temperature.ctof. Reused
end
end

def in_fahrenheit
@f
end

def in_celsius
@c
end

# method to convert fahrenheit to celsius where 1 degree F is 5/9 of 1 degree C. Re-used this code from the first exercise. Took some trial and error to get *self to work but was able to implement. I understand it's useful when there are instance methods, class methods and variables in order to distinguish your class method. Still not 100% clear on it but makes sense in this example.
def self.ftoc(f)
(f-32) * (5.0/9)
end

# method to convert celcius to fahrenheit
def self.ctof(c)
(c * (9/5.0)) + 32
end

# Factory Methods
def self.from_fahrenheit(temp)
self.new(:f => temp) #called on the Temperature class
end

def self.from_celsius(temp)
self.new(:c => temp) #called on the Temperature class
end
end

class Fahrenheit < Temperature #Fahrenheit subclass

def initialize(temp)
@f = temp
@c = Temperature.ftoc(@f)
end
end

class Celsius < Temperature #Celsius subclass

def initialize(temp)
@c = temp
@f = Temperature.ctof(@c)
end

end
Loading