diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..c471326d8 --- /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..5d17c0ac7 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f - 32.0) * 5.0/9.0 +end + +def ctof(c) + c * 9.0/5.0 + 32.0 +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..b56d426cc --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,32 @@ +def add(a,b) + a + b +end + +def subtract(a,b) + a - b +end + +def sum(a) + rez = 0 + a.each { |i| rez += i} + rez +end + + +def mult(a) + rez = 1 + a.each { |i| rez *= i } + rez +end + +def pow(a,b) + a ** b +end + +def fact(a) + rez = 1 + for i in 1..a do + rez *= i + end + rez +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index 29c91f64e..1a142251b 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,37 @@ describe "#multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + mult([1,2]).should == 2 + end - it "multiplies several numbers" + it "multiplies several numbers" do + mult([1,2,3]).should == 6 + end end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + pow(2,2).should == 4 + 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" + it "computes the factorial of 0" do + fact(0).should == 1 + end + it "computes the factorial of 1" do + fact(1).should == 1 + end + it "computes the factorial of 2" do + fact(2).should == 2 + end + it "computes the factorial of 5" do + fact(5).should == 120 + end + it "computes the factorial of 10" do + fact(10).should == 3628800 + end end diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..ff26c4003 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,33 @@ +def echo(msg) + msg +end + +def shout(msg) + echo(msg.upcase) +end + +def repeat(msg,n = 2) + ((echo(msg) + " ") * n).chop +end + +def start_of_word(word,n = 1) + word[0,n] +end + +def first_word(word) + word.split(" ").first +end + +def titleize(msg) + ltwrds = ["and","the", "over", "in","of", "a", "an", "to", "for", "with"] + ix = 0 + wrds = msg.split(' ') + wrds.each do |w| + if !ltwrds.include?(w) or ix == 0 + w.capitalize! + end + ix += 1 + w + end + wrds.join(" ") +end \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..1d1e42d69 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,25 @@ +def translate(text) + vowels = "aeiou" + words = text.split(" ") + + words.map! do |word| + if vowels.include?(word[0]) + word + "ay" + else + if word.start_with?("qu") + word = word[2..-1] + "qu" + elsif word[1..2] == "qu" + word = word[3..-1] + word[0..2] + else + while !vowels.include?(word[0]) do + word = word[1..-1] + word[0] + end + end + word + "ay" + end + end + + result = words.join(" ") + puts result + result +end diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..ff231d935 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,18 @@ +def reverser + msg = yield + wrds = msg.split(' ') + wrds.map{|w| w.reverse!} + + wrds.join(' ') +end + +def adder(n = 1) + yield + n +end + + +def repeater (n = 1) + for i in 1..n do + yield + end +end \ No newline at end of file diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..636288f32 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,8 @@ +def measure(n = 1) + time1 = Time.now + + n.times { yield } + + time2 = Time.now + (time2 - time1).to_f / n +end diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..2b3523298 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,11 @@ +class Friend + + def greeting(name = "") + if name == "" + "Hello!" + else + "Hello, #{name}!" + end + + end +end \ No newline at end of file diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..f600788b1 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,25 @@ +class Book + + def titleize(msg) + ltwrds = ["and","the", "over", "in","of", "a", "an", "to", "for", "with"] + ix = 0 + wrds = msg.split(' ') + wrds.each do |w| + if !ltwrds.include?(w) or ix == 0 + w.capitalize! + end + ix += 1 + w + end + wrds.join(" ") + end + + + def title=(title) + @title = titleize(title) + end + + def title + @title + end +end \ No newline at end of file diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..fb58f8c27 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,18 @@ +class Timer + + def seconds + @seconds ||= 0 + end + + def seconds=(seconds) + @seconds = seconds + end + + def time_string + h = seconds / 3600 + m = (seconds % 3600) / 60 + s = seconds % 60 + + format("%02d:%02d:%02d", h, m, s) + end +end \ No newline at end of file diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..5abb8a9e8 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,45 @@ +class Temperature + def initialize(temp = {}) + if temp[:f] + @in_fahrenheit = temp[:f] + @in_celsius = Temperature.ftoc(temp[:f]) + elsif temp[:c] + @in_fahrenheit = Temperature.ctof(temp[:c])#temp[:c] * 9.0/5.0 + 32.0 + @in_celsius = temp[:c] + end + end + + def self.ftoc(f) + (f - 32.0) * 5.0/9.0 + end + + def self.ctof(c) + c * 9.0/5.0 + 32.0 + end + + def self.from_celsius(ctmp) + Temperature.new(:c => ctmp) + end + def self.from_fahrenheit(ftmp) + Temperature.new(:f => ftmp) + end + def in_fahrenheit() + @in_fahrenheit ||= 0 + end + + def in_celsius() + @in_celsius ||= 0 + end +end + +class Celsius < Temperature + def initialize(c) + super(:c => c) + end +end + +class Fahrenheit < Temperature + def initialize(f) + super(:f => f) + end +end \ No newline at end of file