Skip to content

learn_ruby #101

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
return "Hello!"
end

def greet(name)
return "Hello, #{name}!"
end
1 change: 1 addition & 0 deletions 00_hello/hello_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@
expect(greet("Bob")).to eq("Hello, Bob!")
end
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.0)
end

def ctof(temp)
(temp * 9.0/5.0) + 32
end
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(x,y)

(x + y)

end

def subtract(x,y)

(x - y)

end

def sum(arr)

arr.inject(0) {|sum, x| sum + x}

end

def multiply(numbers)

numbers.inject(1) {|sum, number| sum + number}

end

def power (x, y)

x ** y

end

def factiorial(x)

1 if x == 0 || x == 1
result = 1
number.downto(1) { |n| result *= n}
result

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

def shout(str)
str.upcase
end

def repeat(str, n = 2)
([str] * n).join(' ')
end

def start_of_word(str, n)
str.slice(0..(n-1))
end

def first_word(str)
word_array = []
word_array = str.split(" ")
return word_array[0]
end

def titleize(str)
word_array = []
new_array = []
little_word = ["the", "and"]
word_array = str.split(" ")
word_array.each do |word|
if little_word.include?(word)
new_array << word
else
new_array << word[0].upcase + word[1..-1]
end
end
puts new_array.join(" ")
end

titleize("Hello and world how are you")
13 changes: 13 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def translate(str)
vowels = %w(a e i o u)
words = str.split(" ")
translated_array = []
words.each do |word|
if vowels.include?(word[0])
translated_array << word + "ay"
translated_array.join("")
end
end

end

15 changes: 15 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def reverser
yield.split.map {|word| word.reverse}.join(" ")

end

def adder(num=1)
yield + num
end

def repeater(loop=1)
loop.times do
yield
end
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(num=1)
start = Time.now
num.times {yield}
finish = Time.now
result = (finish-start) / num
return result
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(name = nil)
name.nil? ? "Hello!": "Hello, #{name}!"
end
end
Empty file added 07_hello_friend/hello_friend.rb
Empty file.
18 changes: 18 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Timer
attr_accessor :seconds
def initialize
@seconds = 0
end

def time_string
hrs = @seconds/ 3600
mins = @seconds % 3600 / 60
secs = @seconds % 3600 % 60
seconds = secs.to_s.rjust(2, "0")
minutes = mins.to_s.rjust(2, "0")
hours = hrs.to_s.rjust(2, "0")
"#{hours}:#{minutes}:#{seconds}"
end


end
15 changes: 15 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Temperature
def initialize(temp)
@f = temp[:f]
@c = temp[:c]
end

def in_fahrenheit
@f
end

def in_celsius
(@f - 32) * (5.0/9)
end
end

22 changes: 22 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Dictionary
attr_reader :entries
def initialize
@hash = {}
end

def add(defs)
defs.each do |word, definition|
@hash[word] = definition
end

end

def entries
@hash
end

def keywords
@hash.keys
end
end

34 changes: 34 additions & 0 deletions 12_rpn_calculator/rpn_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class RPNCalculator
def initialize
@stack = []

end
def push(num)
@stack.push(num)

end
def plus
@stack[-2] = @stack[-2] + @stack[-1]
@stack.pop
end

def minus
@stack[-2] = @stack[-2] - @stack[-1]
@stack.pop
end

def times
@stack[-2] = @stack[-2] * @stack[-1]
@stack.pop
end

def divide
@stack[-2] = @stack[-2].to_f / @stack[-1].to_f
@stack.pop
end
def value
@stack[-1]
end


end
19 changes: 19 additions & 0 deletions 14_array_extensions/array_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Array
def initialize(array)
@array = array
end
def sum
self.inject(0) { |sum, x| sum + x}
end

def square
self.map { |n| n ** 2}
end

def square!
self.map! { |n| n ** 2}
end


end

5 changes: 5 additions & 0 deletions 15_in_words/in_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Fixnum
def in_words
return "zero"
end
end