Skip to content
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
"Hello!"
end

def greet(name)
"Hello, " + name + "!"
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.0) * 5.0/9.0
end

def ctof(c)
c * 9.0/5.0 + 32.0
end
32 changes: 32 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 24 additions & 8 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def echo(msg)
msg
end

def shout(msg)
echo(msg.upcase)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also leave msg.upcase, without echo

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
25 changes: 25 additions & 0 deletions 04_pig_latin/pig_latin.rb

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can simplify method, without using any loops. Only a if else block

Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def measure(n = 1)
time1 = Time.now

n.times { yield }

time2 = Time.now
(time2 - time1).to_f / n
end
11 changes: 11 additions & 0 deletions 07_hello_friend/friend.rb

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name could be asigned to nil, and simplified with using ternary operator

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Friend

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

end
end
25 changes: 25 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -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
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

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
45 changes: 45 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -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