@@ -62,25 +62,25 @@ A new scope for a local variable is introduced in (1) the toplevel (main),
62
62
(2) a class (or module) definition, or (3) a method definition.
63
63
64
64
~~~
65
- i = 1 # (1)
65
+ var = 1 # (1)
66
66
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} "
71
71
end
72
- print "In class, i = ", i, "\n "
72
+ puts "in class: var = #{var} "
73
73
end
74
- print "At top level, i = ", i, "\n "
75
- Demo.new.meth
74
+ puts "at top level: var = #{var} "
75
+ Demo.new.method
76
76
~~~
77
77
78
78
Produces:
79
79
80
80
~~~
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
84
84
~~~
85
85
86
86
(Note that the class definition is executable code: the trace message it
98
98
a += i
99
99
b = i*i
100
100
end
101
- a # => 6
101
+ a # => 6
102
102
# b is not defined here
103
103
~~~
104
104
@@ -108,16 +108,16 @@ own copy of the variables local to the thread's block:
108
108
~~~
109
109
threads = []
110
110
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
114
114
a = 0
115
115
3.times do |i|
116
116
Thread.pass
117
117
a += i
118
- print localName, ": ", a, "\n "
118
+ puts "#{local_name}: #{a} "
119
119
end
120
- }
120
+ end
121
121
end
122
122
123
123
threads.each {|t| t.join }
@@ -148,30 +148,31 @@ parameters. To decide which is the case, Ruby looks for assignment statements.
148
148
If at some point in the source prior to the use of ` a ` it sees it being
149
149
assigned to, it decides to parse ` a ` as a variable, otherwise it treats it
150
150
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:
152
152
153
153
~~~
154
154
def a
155
- print "Function 'a' called\n"
155
+ puts "method `a' called"
156
+
156
157
99
157
158
end
158
159
159
- for i in 1..2
160
+ [1, 2].each do |i|
160
161
if i == 2
161
- print "a=", a, "\n "
162
+ puts "a = #{a} "
162
163
else
163
164
a = 1
164
- print "a=", a, "\n "
165
+ puts "a = #{a} "
165
166
end
166
167
end
167
168
~~~
168
169
169
170
Produces:
170
171
171
172
~~~
172
- a= 1
173
- Function ' a' called
174
- a= 99
173
+ a = 1
174
+ method ` a' called
175
+ a = 99
175
176
~~~
176
177
177
178
During the parse, Ruby sees the use of ` a ` in the first ` print ` statement
@@ -211,12 +212,13 @@ invoked.
211
212
(See [ assignment] ( #assignment ) for more on the semantics of assignment.)
212
213
213
214
~~~
214
- def addOne(n )
215
- n += 1
215
+ def add_one(number )
216
+ number += 1
216
217
end
218
+
217
219
a = 1
218
- addOne (a) # => 2
219
- a # => 1
220
+ add_one (a) # => 2
221
+ a # => 1
220
222
~~~
221
223
222
224
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.
226
228
def downer(string)
227
229
string.downcase!
228
230
end
229
- a = "HELLO" # => "HELLO"
230
- downer(a) # => "hello"
231
- a # => "hello"
231
+
232
+ a = "HELLO" # => "HELLO"
233
+ downer(a) # => "hello"
234
+ a # => "hello"
232
235
~~~
233
236
234
237
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.
254
257
255
258
~~~
256
259
def foo(prefix, *all)
257
- for e in all
258
- print prefix, e, " "
260
+ all.each do |element|
261
+ puts "#{prefix}#{element} "
259
262
end
260
263
end
261
264
262
- foo("val= ", 1, 2, 3)
265
+ foo("val = ", 1, 2, 3)
263
266
~~~
264
267
265
268
Produces:
266
269
267
270
~~~
268
- val=1 val=2 val=3
271
+ val = 1
272
+ val = 2
273
+ val = 3
269
274
~~~
270
275
271
276
When used in a method call, ` * ` expands an array, passing its individual
@@ -288,12 +293,12 @@ For example:
288
293
289
294
~~~
290
295
x, *y = [7, 8, 9]
291
- x # => 7
292
- y # => [8, 9]
296
+ x # => 7
297
+ y # => [8, 9]
293
298
x, = [7, 8, 9]
294
- x # => 7
299
+ x # => 7
295
300
x = [7, 8, 9]
296
- x # => [7, 8, 9]
301
+ x # => [7, 8, 9]
297
302
~~~
298
303
299
304
### 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.
307
312
The method may then use ` yield ` to call it.
308
313
309
314
~~~
310
- square = proc { |i| i*i }
311
-
312
315
def meth1(&b)
313
- print b.call(9), "\n"
316
+ puts b.call(9)
314
317
end
315
318
316
- meth1 { |i| i+ i }
319
+ meth1 {|i| i + i }
317
320
318
321
def meth2
319
- print yield(8), "\n"
322
+ puts yield(8)
320
323
end
321
324
322
- meth2 { |i| i+i }
325
+ square = proc {|i| i * i }
326
+
327
+ meth2 {|i| i + i }
323
328
meth2 &square
324
329
~~~
325
330
@@ -334,13 +339,13 @@ Produces:
334
339
### How can I specify a default value for a formal argument?
335
340
336
341
~~~
337
- def greet(p1=' hello' , p2=' world' )
338
- print "#{p1} #{p2}\n "
342
+ def greet(p1=" hello" , p2=" world" )
343
+ puts "#{p1} #{p2}"
339
344
end
340
345
341
346
greet
342
- greet "hi"
343
- greet "morning", "mom"
347
+ greet( "hi")
348
+ greet( "morning", "mom")
344
349
~~~
345
350
346
351
Produces:
@@ -360,7 +365,7 @@ The formal parameters of a block appear between vertical bars at the start
360
365
of the block:
361
366
362
367
~~~
363
- proc { |a, b| a <=> b }
368
+ proc {|a, b| a <=> b }
364
369
~~~
365
370
366
371
These parameters are actually local variables. If an existing local variable
@@ -374,9 +379,9 @@ calls `yield`), or by using the `Proc.call` method.
374
379
375
380
~~~
376
381
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"
380
385
~~~
381
386
382
387
Variables hold references to objects. The assignment ` A = a = b = "abc" ` puts
0 commit comments