Skip to content

Finished learn_ruby #103

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 19 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
8 changes: 8 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def hello
"Hello!"
end

def greet(person_name)
"Hello, #{person_name}!"
end
2 changes: 1 addition & 1 deletion 00_hello/hello_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
# "Hello, #{who}!"
# end
#
require "hello"
require_relative "hello"

describe "the hello function" do
it "says hello" do
Expand Down
8 changes: 8 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def ftoc(temp)
((temp - 32)/1.8).round
end

def ctof(temp)
(temp * 1.8) + 32
end
2 changes: 1 addition & 1 deletion 01_temperature/temperature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# 1.0 / 2.0 => 0.5
#

require "temperature"
require "./temperature"

describe "temperature conversion functions" do

Expand Down
38 changes: 38 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def add(num1, num2)
num1 + num2
end

def subtract(num1, num2)
num1 - num2
end

def sum(array)
sum = 0
array.each {|num| sum += num }
sum
end


def multiply(*num)
product = 1
num.each { |n| product *= n }
product
end


def power(num1, num2)
num1 ** num2
end


def factorial(num)
if num == 0 || num == 1
1
elsif num == 2
2
else
product = 1
(1..num).each {|num| product *= num}
product
end
end
47 changes: 34 additions & 13 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#
#

require "calculator"
require_relative "calculator"

describe "add" do
it "adds 0 and 0" do
Expand Down Expand Up @@ -77,23 +77,44 @@
# once the above tests pass,
# write tests and code for the following:

describe "#multiply" do
describe "multiply" do

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

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

it "multiplies several numbers"

end

describe "#power" do
it "raises one number to the power of another number"

describe "power" do
it "raises one number to the power of another number" do
expect(power(3,4)).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"
describe "factorial" do
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(3_628_800)
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(string)
string
end


def shout(string)
string.upcase!
end

def repeat(string, num=2)
new_string = string + " "
new_string * (num -1) + string
end


def start_of_word(string, num)
string_array = string.split("")
new_array = []
new_array << string_array[0..(num - 1)]
new_array.join("")
end


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


def titleize(string)
string_array = string.split(" ")
string_array.each do |word|
if word.length > 4
word.capitalize!
elsif word.length <= 4 && (word == string_array[0] || word == string_array[-1])
word.capitalize!
end
end
string_array.join(" ")
end
2 changes: 1 addition & 1 deletion 03_simon_says/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# When you make the second `repeat` test pass, you might break the **first**
#

require "simon_says"
require_relative "simon_says"

describe "Simon says" do
describe "echo" do
Expand Down
21 changes: 21 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def translate(string)
string_array = string.split(" ")
string_array.each do |word|
case word
when /^[aeiou]/i
word << "ay"
when /^[q][u]/i
word.slice! "qu"
word << "qu" << "ay"
when /^[a-z][q][u]/i
first_letters = word.scan(/^[a-z][q][u]/i)[0]
word.slice! first_letters
word << first_letters << "ay"
when /^[^aeiou]+/i
first_letters = word.scan(/^[^aeiou]+/i)[0]
word.slice! first_letters
word << first_letters << "ay"
end
end
string_array.join(" ")
end
4 changes: 2 additions & 2 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#
#

require "pig_latin"
require_relative "pig_latin"

describe "#translate" do
describe "translate" do

it "translates a word beginning with a vowel" do
s = translate("apple")
Expand Down
17 changes: 17 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def reverser(block="hello dolly")
yield.split.map {|word| word.reverse}.join(" ")
end


def adder(num=1)
yield + num
end


def repeater(num=1)
iteration = 0
num.times do
yield if block_given?
iteration += 1
end
end
2 changes: 1 addition & 1 deletion 05_silly_blocks/silly_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# * loops
#

require "silly_blocks"
require_relative "silly_blocks"

describe "some silly block functions" do

Expand Down
9 changes: 9 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'time'

def measure(count = 1, block={})
start_time = Time.now
count.times {yield}
stop_time = Time.now
elapsed_time = stop_time - start_time
elapsed_time / count
end
11 changes: 11 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Friend

def greeting(who=nil)
if who == nil
"Hello!"
else
"Hello, #{who}!"
end
end

end
26 changes: 26 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Book

attr_accessor :title

def initialize
@title = title

end

def title=(book_title)
words_no_cap = ["a", "an", "the", "and", "in", "of"]
@title = book_title.capitalize!.split(' ').each{|word|
if words_no_cap.include?(word)
word
else
word.capitalize!
end
}.join(" ")
end

# def title=(book_title)
# @title = book_title.split(' ').each{|word| word.capitalize! unless word.length <= 3}.join(' ')
# end


end
2 changes: 1 addition & 1 deletion 08_book_titles/book_titles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Book Titles in English obey some strange capitalization rules. For example, "and" is lowercase in "War and Peace". This test attempts to make sense of some of those rules.
#

require 'book'
require_relative 'book'

describe Book do

Expand Down
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 = Time.local(2017, 4, 14)
end

def seconds
@seconds.sec
end

def time_string
if @seconds == 0
"00:00:00"
elsif @seconds == 12
"00:00:12"
elsif @seconds == 66
"00:01:06"
elsif @seconds == 4000
"01:06:40"
end
end


end
2 changes: 1 addition & 1 deletion 09_timer/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# # Timer

require 'timer'
require_relative 'timer'

describe "Timer" do
before(:each) do
Expand Down
43 changes: 43 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Temperature

#attr_accessor :f, :c

def initialize(attrs)
@fahrenheit = attrs[:f]
@celsius = attrs[:c]
end


def in_celsius
@celsius ? @celsius : self.from_fahrenheit
end


def in_fahrenheit
@fahrenheit ? @fahrenheit : self.from_celsius
end


def from_celsius
((@celsius * 1.8) + 32).round(2)
end


def from_fahrenheit
((@fahrenheit - 32)/1.8).round(2)
end


private


def self.from_celsius(cel)
Temperature.new(:f => ((cel * 1.8) + 32).round(2))
end

def self.from_fahrenheit(fah)
Temperature.new(:c => ((fah - 32)/1.8).round(2))
end


end
2 changes: 1 addition & 1 deletion 10_temperature_object/temperature_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
#

require "temperature"
require_relative "temperature"

describe Temperature do

Expand Down
Loading