diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..a018bb7f0 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(name) + "Hello, #{name}!" +end \ No newline at end of file diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..35c8aa440 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,11 @@ +def ftoc(f) + c = (f-32) * (5.0/9) + return c +end + +ftoc(32) + +def ctof(c) + f = (c * 9 / 5.0) + 32 + return f +end \ No newline at end of file diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..9d7881cb4 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,37 @@ +def add(num1, num2) + num1 + num2 +end + +def subtract(num1, num2) + num1 - num2 +end + +# trying out inject but will save it for another time +# def sum(array) +# array.inject {|total, num| total + num} +# if array == nil +# puts 0 +# else +# puts array +# end +# end + +def sum(array) + total = 0 + array.each {|x, y| total += x } + return total +end + +#come back to do the bonus. + + + + + + + + + + + + diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..d041cead8 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,44 @@ +def echo(string) + string +end + +def shout(string) + string.upcase +end + +def repeat(string, num = 2) + repeated = (string + " ")*num + repeated.strip +end + +def start_of_word(string, num = 1) + string[0..num-1] +end + +def first_word(string) + string.split(" ")[0] +end + +# def titleize(string) +# lowercase_words = %w{a an the and but or for nor of over} +# string.split.each_with_index.map{|x, index| lowercase_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ") +# end + +def titleize(string) + lowercase_words = %w{a an the and but or for nor of over} + word = string.split(" ") + first_word = word.shift.capitalize + word.each do |word| + if lowercase_words.include?(word) + word + else + word.capitalize! + end + end + word.unshift(first_word) + word.join(" ") +end + + + + diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..1258fb79d --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,13 @@ +def translate(string) + vowels = %w{a e i o u} + if vowels.include?(string[0]) + string + 'ay' + elsif !(vowels.include?(string[0])) && !(vowels.include?(string[1])) + string[2..-1] + string[0..1] + 'ay' + # elsif consonant.include?(word[0]) && consonant.include?(word[1] && consonant.include?(word[2] + # word[3..-1] + word[0..2] + 'ay' + else !(vowels.include?(string[0])) + string[1..-1] + string[0] + 'ay' + end +end + diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..dd00ef146 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,16 @@ +def reverser + array = yield.split(" ") + array.each do |word| + word.reverse! + end + return array.join(' ') +end + +def adder(value = 1) + num = yield + num += value +end + +def repeater(num_times = 1) + num_times.times {yield} +end diff --git a/06_hello_friend/friend.rb b/06_hello_friend/friend.rb new file mode 100644 index 000000000..21bed0e1a --- /dev/null +++ b/06_hello_friend/friend.rb @@ -0,0 +1,5 @@ +class Friend + def greet (name = "") + "Hello #{name}!" + end +end diff --git a/07_book_titles/book.rb b/07_book_titles/book.rb new file mode 100644 index 000000000..ead4bbd0c --- /dev/null +++ b/07_book_titles/book.rb @@ -0,0 +1,26 @@ +class Book + attr_accessor :title + + # def initialize(title) + # @title = title + # end + + def title=(string) + @title = titleize(string) + end + + def titleize(string) + lowercase_words = %w{a an in the and but or for nor of over} + word = string.split(" ") + first_word = word.shift.capitalize + word.each do |word| + if lowercase_words.include?(word) + word + else + word.capitalize! + end + end + word.unshift(first_word) + word.join(" ") + end +end diff --git a/08_timer/timer.rb b/08_timer/timer.rb new file mode 100644 index 000000000..f366ba4e0 --- /dev/null +++ b/08_timer/timer.rb @@ -0,0 +1,30 @@ +# class Timer +# attr_accessor :timer + +# def initialize(timer = 0) +# @timer = timer +# end + +# def time_string +# seconds = @timer +# minutes = seconds/60 +# hours = minutes/60 +# '%02d:%02d:%02d' % [hours, minutes, seconds] +# end +# end + +# got help with this one + +class Timer + attr_accessor :seconds + def initialize(seconds=0) + @seconds = seconds + end + + def time_string + hours = (@seconds / (60 * 60)).to_i + minutes = ((@seconds - hours * 60 * 60) / 60).to_i + seconds = (@seconds - hours * 60 * 60 - minutes * 60) + '%02d:%02d:%02d' % [hours, minutes, seconds] + end +end \ No newline at end of file diff --git a/08_timer/timer_spec.rb b/08_timer/timer_spec.rb index 95d2e3176..c9b14f5bc 100644 --- a/08_timer/timer_spec.rb +++ b/08_timer/timer_spec.rb @@ -12,7 +12,7 @@ end it "should initialize to 0 seconds" do - @timer.seconds.to eq 0 + expect(@timer.seconds).to eq 0 end describe 'time_string' do diff --git a/09_array_extensions/array_extensions.rb b/09_array_extensions/array_extensions.rb new file mode 100644 index 000000000..56d584e60 --- /dev/null +++ b/09_array_extensions/array_extensions.rb @@ -0,0 +1,15 @@ +class Array + def sum + self.inject(0) {|result, value| result + value } + end + + def square + self.map {|value| value**2} + end + + def square! + self.map! {|value| value **2} + end +end + + diff --git a/09_array_extensions/array_extensions_spec.rb b/09_array_extensions/array_extensions_spec.rb index dbb111ec3..f07dfa519 100755 --- a/09_array_extensions/array_extensions_spec.rb +++ b/09_array_extensions/array_extensions_spec.rb @@ -26,7 +26,7 @@ end it "should add all of the elements" do - expect([1,2,4]).sum.to eq(7) + expect([1,2,4].sum).to eq(7) end end @@ -36,7 +36,7 @@ end it "returns a new array containing the squares of each element" do - expect([1,2,3]).square.to eq([1,4,9]) + expect([1,2,3].square).to eq([1,4,9]) end end diff --git a/09_array_extensions/untitled.rb b/09_array_extensions/untitled.rb new file mode 100644 index 000000000..4dbec9ab1 --- /dev/null +++ b/09_array_extensions/untitled.rb @@ -0,0 +1,38 @@ +# coffee and code + +#Step Four: combining arrays + +deck = [] +numbers = %w{2 3 4 5 6 7 8 9 10 J Q K A} +suits = %w{spades hearts diamonds clubs} + +# suits.each do |suit| +# numbers.each do |number| +# deck << [[number, suit]] +# end +# end + +#functional code below +# suits.each do |suit| +# numbers.each do |number| +# new_array << |number,suit| +# end +# end + +# puts deck + +#another solution +# results = suits.product(numbers) +# result.each {|card| card.reverse!} +# print result + +new_array = suits.inject([]) do |memo, suit| + numbers.each do |number| + memo << [number, suit] + end + memo +end + +print new_array + + diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..a155d839b --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,13 @@ +# to complete tomorrow + +class Temperature + def initialize(options) + @f = options(:f) + @c = options(:c) + end + + def fahrenheit(temp) + Temperature.new(:f => temp) + end + +end \ No newline at end of file diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index 7079265af..d2f0c35cc 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -13,7 +13,7 @@ describe "in degrees fahrenheit" do it "at 50 degrees" do # A hash with the key "f" is being passed in. - Temperature.new({:f => 50}).to_fahrenheit.to eq 50 + expect(Temperature.new({:f => 50}).to_fahrenheit).to eq 50 # Remember, new is synonymous with initialize. # An example might help: #