Skip to content

Commit 43887df

Browse files
Jason ReevesJason Reeves
authored andcommitted
add lesson 2.2
1 parent 633c22e commit 43887df

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed

examples/2.2.0.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
monk = Object.new
2+
def monk.nirvana?
3+
@count ||= 0
4+
@count += 1
5+
@count > 3
6+
end
7+
8+
def monk.meditate
9+
puts "meditating"
10+
end
11+
12+
# you have to change the following statement (into multiple ones if needed)
13+
# so that the monk meditates till monk.nirvana? becomes true.
14+
monk.meditate unless monk.nirvana?

examples/2.2.1.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
bell = Object.new
2+
def bell.ring
3+
puts "ring"
4+
end
5+
6+
# add a loop inside this method to ring the bell 'n' times
7+
def ring(bell, n)
8+
bell.ring
9+
end
10+
11+
ring(bell, 3)

examples_spec/2.2.0.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rspec'
2+
3+
describe "example code" do
4+
let(:output) { `#{RbConfig.ruby} __TMPFILE__`.chomp }
5+
6+
it "should be a string" do
7+
expect(output.is_a?(String)).to be(true)
8+
end
9+
10+
it "should equal 'meditating\\nmeditating\\nmeditating\\nmeditating'" do
11+
expect(output).to eq("meditating\nmeditating\nmeditating\nmeditating")
12+
end
13+
end

examples_spec/2.2.1.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rspec'
2+
3+
describe "example code" do
4+
let(:output) { `#{RbConfig.ruby} __TMPFILE__`.chomp }
5+
6+
it "should be a string" do
7+
expect(output.is_a?(String)).to be(true)
8+
end
9+
10+
it "should equal 'ring\\nring\\nring'" do
11+
expect(output).to eq("ring\nring\nring")
12+
end
13+
end

lessons/2.2.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"number": "2.2",
3+
"name": "Loops in Ruby",
4+
"subsections": [
5+
{
6+
"name": "Loops in Ruby",
7+
"items": [
8+
{
9+
"type": "paragraph",
10+
"value": "Loops are programming constructs that help you repeat an action an arbitrary number of times."
11+
},
12+
{
13+
"type": "paragraph",
14+
"value": "The methods `Array#each`, `Array#select`, etc. are the most frequently used loops since the primary use of loops is to iterate over or transform a collection, something that we'll learn in the chapter on \"Arrays in Ruby.\""
15+
},
16+
{
17+
"type": "paragraph",
18+
"value": "Here we will cover two basic looping constructs you can use to solve most other looping requirements that may come up."
19+
},
20+
{
21+
"type": "subheader",
22+
"value": "Infinite Loops"
23+
},
24+
{
25+
"type": "paragraph",
26+
"value": "Infinite loops keep running till you explicitly ask them to stop. They are syntactically the simplest to write. Here goes one:"
27+
},
28+
{
29+
"type": "paragraph",
30+
"value": "```\nloop do \n puts \"this line will be executed for an infinite amount of time\" \nend\n```"
31+
},
32+
{
33+
"type": "paragraph",
34+
"value": "The example above does not have a termination condition and hence will run till the process is stopped. A loop can be halted from within using the `break` command."
35+
},
36+
{
37+
"type": "paragraph",
38+
"value": "Now write an infinite loop where the monk will meditate till he achieves Nirvana. Use the `break` statement once Nirvana is reached."
39+
},
40+
{
41+
"type": "test_example",
42+
"value": "2.2.0"
43+
},
44+
{
45+
"type": "subheader",
46+
"value": "Run A Block of Code N Times"
47+
},
48+
{
49+
"type": "paragraph",
50+
"value": "Say `N` is `5`, let us imagine how it might look."
51+
},
52+
{
53+
"type": "paragraph",
54+
"value": "```\n5.times do\n # do the stuff that needs to be done\nend \n```"
55+
},
56+
{
57+
"type": "paragraph",
58+
"value": "Well, we imagined it right. It is that simple!"
59+
},
60+
{
61+
"type": "test_example",
62+
"value": "2.2.1"
63+
},
64+
{
65+
"type": "paragraph",
66+
"value": "We've only scratched the surface of the various ways in which Ruby lets you write loops. However, this should be enough for the most common use cases. Let us know if you need more!"
67+
}
68+
]
69+
}
70+
]
71+
}

lessons/toc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"1.1.json",
88
"1.2.json",
99
"2.0.json",
10-
"2.1.json"
10+
"2.1.json",
11+
"2.2.json"
1112
]
1213
}

0 commit comments

Comments
 (0)