Skip to content

Commit ec1b30b

Browse files
committed
Rework code examples in FAQ
* update to Ruby 2.3 * fix code style * apply various improvements
1 parent 4af1f7e commit ec1b30b

File tree

9 files changed

+270
-265
lines changed

9 files changed

+270
-265
lines changed

en/documentation/faq/1/index.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,33 @@ code by creating a few people and examining them.
7272
~~~
7373
class Person
7474
attr_accessor :name, :age
75+
7576
def initialize(name, age)
7677
@name = name
7778
@age = age.to_i
7879
end
80+
7981
def inspect
80-
"#@name (#@age)"
82+
"#{name} (#{age})"
8183
end
8284
end
8385
84-
p1 = Person.new('elmo', 4)
85-
p2 = Person.new('zoe', 7)
86+
p1 = Person.new("Elmo", 4)
87+
p2 = Person.new("Zoe", 7)
8688
87-
p1 # => elmo (4)
88-
p2 # => zoe (7)
89+
p1 # => Elmo (4)
90+
p2 # => Zoe (7)
8991
~~~
9092

9193
Now let's populate an array of people by reading their names and ages from a
92-
file containing lines like:
94+
file `ages` containing lines like:
9395

9496
~~~
95-
bert: 8
96-
cookie: 11
97-
elmo: 4
98-
ernie: 8
99-
zoe: 7
97+
Bert: 8
98+
Cookie: 11
99+
Elmo: 4
100+
Ernie: 8
101+
Zoe: 7
100102
~~~
101103

102104
The code uses regular expressions to parse successive lines from the input
@@ -106,31 +108,31 @@ end of the array `people`.
106108
~~~
107109
people = Array.new
108110
109-
File.foreach("ages") { |l|
110-
people << Person.new($1, $2) if l =~ /(.*):\s+(\d+)/
111-
}
111+
File.foreach("ages") do |line|
112+
people << Person.new($1, $2) if line =~ /(.*):\s+(\d+)/
113+
end
112114
113-
people # => [bert (8), cookie (11), elmo (4), ernie (8), zoe (7)]
115+
people # => [Bert (8), Cookie (11), Elmo (4), Ernie (8), Zoe (7)]
114116
~~~
115117

116118
Now, let's sort the result based on the person's age. There are many ways to
117119
do this. We can define a sort block, which tells Ruby how to do the comparison
118120
of two people:
119121

120122
~~~
121-
sorted = people.sort do |a,b| a.age <=> b.age end
122-
sorted # => [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
123+
sorted = people.sort {|a, b| a.age <=> b.age }
124+
sorted # => [Elmo (4), Zoe (7), Bert (8), Ernie (8), Cookie (11)]
123125
~~~
124126

125127
Another way would be to change the comparison method for class `Person`:
126128

127129
~~~
128130
class Person
129131
def <=>(other)
130-
@age <=> other.age
132+
age <=> other.age
131133
end
132134
end
133-
people.sort # => [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
135+
people.sort # => [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
134136
~~~
135137

136138
### Why the name “Ruby”?

en/documentation/faq/11/index.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ It's the same as saying `if a then b else c end`.
4343

4444
### How can I count the number of lines in a file?
4545

46-
Assuming that the file ends in a newline, the following code may give the
47-
fastest result.
46+
The following code may give the fastest result.
4847

4948
~~~
50-
open("example").read.count("\n") # => 3
49+
File.readlines("example").size # => 3
5150
~~~
5251

5352
### What do `begin` and `end` of `MatchData` return?
@@ -66,11 +65,10 @@ method inject, so we will too:
6665
~~~
6766
module Enumerable
6867
69-
# inject(n) { |n, i| ...}
68+
# inject(n) {|n, i| ...}
7069
def inject(n)
71-
each { |i|
72-
n = yield(n, i)
73-
}
70+
each {|i| n = yield(n, i) }
71+
7472
n
7573
end
7674
end

en/documentation/faq/4/index.md

+59-54
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,25 @@ A new scope for a local variable is introduced in (1) the toplevel (main),
6262
(2) a class (or module) definition, or (3) a method definition.
6363

6464
~~~
65-
i = 1 # (1)
65+
var = 1 # (1)
6666
class Demo
67-
i = 2 # (2)
68-
def meth
69-
i = 3 # (3)
70-
print "In method, i = ", i, "\n"
67+
var = 2 # (2)
68+
def method
69+
var = 3 # (3)
70+
puts "in method: var = #{var}"
7171
end
72-
print "In class, i = ", i, "\n"
72+
puts "in class: var = #{var}"
7373
end
74-
print "At top level, i = ", i, "\n"
75-
Demo.new.meth
74+
puts "at top level: var = #{var}"
75+
Demo.new.method
7676
~~~
7777

7878
Produces:
7979

8080
~~~
81-
In class, i = 2
82-
At top level, i = 1
83-
In method, i = 3
81+
in class: var = 2
82+
at top level: var = 1
83+
in method: var = 3
8484
~~~
8585

8686
(Note that the class definition is executable code: the trace message it
@@ -98,7 +98,7 @@ a = 0
9898
a += i
9999
b = i*i
100100
end
101-
a # => 6
101+
a # => 6
102102
# b is not defined here
103103
~~~
104104

@@ -108,16 +108,16 @@ own copy of the variables local to the thread's block:
108108
~~~
109109
threads = []
110110
111-
for name in ['one', 'two'] do
112-
threads << Thread.new {
113-
localName = name
111+
["one", "two"].each do |name|
112+
threads << Thread.new do
113+
local_name = name
114114
a = 0
115115
3.times do |i|
116116
Thread.pass
117117
a += i
118-
print localName, ": ", a, "\n"
118+
puts "#{local_name}: #{a}"
119119
end
120-
}
120+
end
121121
end
122122
123123
threads.each {|t| t.join }
@@ -148,30 +148,31 @@ parameters. To decide which is the case, Ruby looks for assignment statements.
148148
If at some point in the source prior to the use of `a` it sees it being
149149
assigned to, it decides to parse `a` as a variable, otherwise it treats it
150150
as a method. As a somewhat pathological case of this, consider this code
151-
fragment, submitted by Clemens Hintze:
151+
fragment, originally submitted by Clemens Hintze:
152152

153153
~~~
154154
def a
155-
print "Function 'a' called\n"
155+
puts "method `a' called"
156+
156157
99
157158
end
158159
159-
for i in 1..2
160+
[1, 2].each do |i|
160161
if i == 2
161-
print "a=", a, "\n"
162+
puts "a = #{a}"
162163
else
163164
a = 1
164-
print "a=", a, "\n"
165+
puts "a = #{a}"
165166
end
166167
end
167168
~~~
168169

169170
Produces:
170171

171172
~~~
172-
a=1
173-
Function 'a' called
174-
a=99
173+
a = 1
174+
method `a' called
175+
a = 99
175176
~~~
176177

177178
During the parse, Ruby sees the use of `a` in the first `print` statement
@@ -211,12 +212,13 @@ invoked.
211212
(See [assignment](#assignment) for more on the semantics of assignment.)
212213

213214
~~~
214-
def addOne(n)
215-
n += 1
215+
def add_one(number)
216+
number += 1
216217
end
218+
217219
a = 1
218-
addOne(a) # => 2
219-
a # => 1
220+
add_one(a) # => 2
221+
a # => 1
220222
~~~
221223

222224
As you are passing object references, it is possible that a method may modify
@@ -226,9 +228,10 @@ the contents of a mutable object passed into it.
226228
def downer(string)
227229
string.downcase!
228230
end
229-
a = "HELLO" # => "HELLO"
230-
downer(a) # => "hello"
231-
a # => "hello"
231+
232+
a = "HELLO" # => "HELLO"
233+
downer(a) # => "hello"
234+
a # => "hello"
232235
~~~
233236

234237
There is no equivalent of other language's pass-by-reference semantics.
@@ -254,18 +257,20 @@ array, and assigning that array to the starred parameter.
254257

255258
~~~
256259
def foo(prefix, *all)
257-
for e in all
258-
print prefix, e, " "
260+
all.each do |element|
261+
puts "#{prefix}#{element}"
259262
end
260263
end
261264
262-
foo("val=", 1, 2, 3)
265+
foo("val = ", 1, 2, 3)
263266
~~~
264267

265268
Produces:
266269

267270
~~~
268-
val=1 val=2 val=3
271+
val = 1
272+
val = 2
273+
val = 3
269274
~~~
270275

271276
When used in a method call, `*` expands an array, passing its individual
@@ -288,12 +293,12 @@ For example:
288293

289294
~~~
290295
x, *y = [7, 8, 9]
291-
x # => 7
292-
y # => [8, 9]
296+
x # => 7
297+
y # => [8, 9]
293298
x, = [7, 8, 9]
294-
x # => 7
299+
x # => 7
295300
x = [7, 8, 9]
296-
x # => [7, 8, 9]
301+
x # => [7, 8, 9]
297302
~~~
298303

299304
### What does `&` prepended to an argument mean?
@@ -307,19 +312,19 @@ you can precede its name with an ampersand to convert in into a block.
307312
The method may then use `yield` to call it.
308313

309314
~~~
310-
square = proc { |i| i*i }
311-
312315
def meth1(&b)
313-
print b.call(9), "\n"
316+
puts b.call(9)
314317
end
315318
316-
meth1 { |i| i+i }
319+
meth1 {|i| i + i }
317320
318321
def meth2
319-
print yield(8), "\n"
322+
puts yield(8)
320323
end
321324
322-
meth2 { |i| i+i }
325+
square = proc {|i| i * i }
326+
327+
meth2 {|i| i + i }
323328
meth2 &square
324329
~~~
325330

@@ -334,13 +339,13 @@ Produces:
334339
### How can I specify a default value for a formal argument?
335340

336341
~~~
337-
def greet(p1='hello', p2='world')
338-
print "#{p1} #{p2}\n"
342+
def greet(p1="hello", p2="world")
343+
puts "#{p1} #{p2}"
339344
end
340345
341346
greet
342-
greet "hi"
343-
greet "morning", "mom"
347+
greet("hi")
348+
greet("morning", "mom")
344349
~~~
345350

346351
Produces:
@@ -360,7 +365,7 @@ The formal parameters of a block appear between vertical bars at the start
360365
of the block:
361366

362367
~~~
363-
proc { |a, b| a <=> b }
368+
proc {|a, b| a <=> b }
364369
~~~
365370

366371
These parameters are actually local variables. If an existing local variable
@@ -374,9 +379,9 @@ calls `yield`), or by using the `Proc.call` method.
374379

375380
~~~
376381
A = a = b = "abc"
377-
b.concat("d") # => "abcd"
378-
a # => "abcd"
379-
A # => "abcd"
382+
b.concat("d") # => "abcd"
383+
a # => "abcd"
384+
A # => "abcd"
380385
~~~
381386

382387
Variables hold references to objects. The assignment `A = a = b = "abc"` puts

0 commit comments

Comments
 (0)