Skip to content

Commit a72ab8b

Browse files
committed
updated lessons for two-day ruby
1 parent 5b57c9d commit a72ab8b

File tree

12 files changed

+1513
-0
lines changed

12 files changed

+1513
-0
lines changed
File renamed without changes.

01_class/calculator2_with_attr.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Calculator
2+
attr_reader :num_calculations
3+
attr_accessor :name
4+
5+
@@num_calculators = 0
6+
7+
def initialize
8+
@num_calculations = 0
9+
@name = "I am a really neat calculator!"
10+
@@num_calculators += 1
11+
end
12+
13+
def self.num_calculators
14+
@@num_calculators
15+
end
16+
17+
def add(a, b)
18+
@num_calculations = @num_calculations + 1
19+
a + b
20+
end
21+
end
22+

05_calculator/calculator_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,45 @@
2121
it "subtracts numbers" do
2222
@calculator.subtract(10,4).should == 6
2323
end
24+
25+
describe "#sum" do
26+
it "computes the sum of an empty array" do
27+
pending
28+
@calculator.sum([]).should == 0
29+
end
30+
31+
it "computes the sum of an array of one number" do
32+
pending
33+
@calculator.sum([7]).should == 7
34+
end
35+
36+
it "computes the sum of an array of two numbers" do
37+
pending
38+
@calculator.sum([7,11]).should == 18
39+
end
40+
41+
it "computes the sum of an array of many numbers" do
42+
pending
43+
@calculator.sum([1,3,5,7,9]).should == 25
44+
end
45+
end
46+
47+
# Test-Driving Bonus: once the above tests pass,
48+
# write tests and code for the following:
49+
50+
it "multiplies two numbers"
51+
52+
it "multiplies an array of numbers"
53+
54+
it "raises one number to the power of another number"
55+
56+
# http://en.wikipedia.org/wiki/Factorial
57+
describe "#factorial" do
58+
it "computes the factorial of 0"
59+
it "computes the factorial of 1"
60+
it "computes the factorial of 2"
61+
it "computes the factorial of 5"
62+
it "computes the factorial of 10"
63+
end
64+
2465
end

07_simon_says/simon_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
describe "Simon says" do
2+
3+
it "should echo hello" do
4+
echo("hello").should == "hello"
5+
end
6+
7+
it "should echo bye" do
8+
echo("bye").should == "bye"
9+
end
10+
11+
it "should shout hello" do
12+
shout("hello").should == "HELLO"
13+
end
14+
15+
it "should shout multiple words" do
16+
shout("hello world").should == "HELLO WORLD"
17+
end
18+
19+
it "should repeat" do
20+
repeat("hello").should == "hello hello"
21+
end
22+
23+
it "should repeat a number of times" do
24+
repeat("hello", 3).should == "hello hello hello"
25+
end
26+
27+
it "should return the first letter" do
28+
start_of_word("hello", 1).should == "h"
29+
end
30+
31+
it "should return the first two letters" do
32+
start_of_word("Bob", 2).should == "Bo"
33+
end
34+
35+
it "should tell us the first word of 'Hello World' is 'Hello'" do
36+
first_word("Hello World").should == "Hello"
37+
end
38+
39+
it "should tell us the first word of 'oh dear' is 'oh'" do
40+
first_word("Hello World").should == "oh"
41+
end
42+
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

14_web/cnn.html

+1,368
Large diffs are not rendered by default.

14_web/news_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'fakeweb'
2+
require 'news'
3+
4+
describe News do
5+
6+
before do
7+
f = File.new("cnn.html", "r")
8+
FakeWeb.register_uri(:get, "http://www.cnn.com", :body => f.read)
9+
@news = News.new
10+
end
11+
12+
it "should allow you to set the uri" do
13+
@news.uri = "http://www.cnn.com"
14+
@news.uri.should == "http://www.cnn.com"
15+
end
16+
17+
it "should fetch the page when you set the uri" do
18+
@news.uri = "http://www.cnn.com"
19+
@news.source_data.should == File.new("cnn.html", "r").read
20+
end
21+
22+
describe "#top_story" do
23+
it "should find the first list item after 'Latest news' heading" do
24+
FakeWeb.register_uri(:get, "http://www.test1.com", :body => "<h4>Latest news</h4><ul><li><a href=\"http://something.com\">Story Name</a>")
25+
@news.uri = "http://www.test1.com"
26+
@news.top_story.should ==
27+
{:url => "http://something.com",
28+
:title => "Story Name"}
29+
end
30+
31+
it "should return the first headlines with url listed under 'Latest News'" do
32+
@news.uri = "http://www.cnn.com"
33+
@news.top_story.should ==
34+
{:url => "/2010/WORLD/americas/01/31/haiti.us.airlifts/index.html?hpt=T2",
35+
:title => "Evacuations of Haitians to U.S. to resume"}
36+
end
37+
end
38+
39+
end
40+

0 commit comments

Comments
 (0)