1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
h2. Active Support Overview
Active Support is the Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff. It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Rails itself.
By referring to this guide you will learn:
* The extensions to the Ruby core modules and classes provided by Rails.
* The rest of fundamental libraries available in Rails.
endprologue.
h3. Extensions to All Objects
h4. +blank?+ and +present?+
The following values are considered to be blank in a Rails application:
* +nil+ and +false+,
* strings composed only of whitespace, i.e. matching +/\A\s*\z/+,
* empty arrays and hashes, and
* any other object that responds to +empty?+ and it is empty.
WARNING: Note that numbers are not mentioned, in particular 0 and 0.0 are *not* blank.
For example, this method from +ActionDispatch::Response+ uses +blank?+ to easily be robust to +nil+ and whitespace strings in one shot:
<ruby>
def charset
charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
charset.blank? ? nil : charset.strip.split("=")[1]
end
</ruby>
That's a typical use case for +blank?+.
Here, the method Rails runs to instantiate observers upon initialization has nothing to do if there are none:
<ruby>
def instantiate_observers
return if @observers.blank?
# ...
end
</ruby>
The method +present?+ is equivalent to +!blank?+:
<ruby>
assert @response.body.present? # same as !@response.body.blank?
</ruby>
h4. +duplicable?+
A few fundamental objects in Ruby are singletons. For example, in the whole live of a program the integer 1 refers always to the same instance:
<ruby>
1.object_id # => 3
Math.cos(0).to_i.object_id # => 3
</ruby>
Hence, there's no way these objects can be duplicated through +dup+ or +clone+:
<ruby>
true.dup # => TypeError: can't dup TrueClass
</ruby>
Some numbers which are not singletons are not duplicable either:
<ruby>
0.0.clone # => allocator undefined for Float
(2**1024).clone # => allocator undefined for Bignum
</ruby>
Active Support provides +duplicable?+ to programmatically query an object about this property:
<ruby>
"".duplicable? # => true
false.duplicable? # => false
</ruby>
By definition all objects are +duplicable?+ except +nil+, +false+, +true+, symbols, numbers, and class objects.
WARNING. Using +duplicable?+ is discouraged because it depends on a hard-coded list. Classes have means to disallow duplication like removing +dup+ and +clone+ or raising exceptions from them, only +rescue+ can tell.
h4. +tap+
+Object#tap+ exists in Ruby 1.8.7 and 1.9, and it is defined by Active Support for previous versions. This method yields its receiver to a block and returns it.
For example, the following class method from +ActionDispatch::TestResponse+ creates, initializes, and returns a new test response using +tap+:
<ruby>
def self.from_response(response)
new.tap do |resp|
resp.status = response.status
resp.headers = response.headers
resp.body = response.body
end
end
</ruby>
See also +Object#returning+ in "Extensions to All Objects FIX THIS LINK":FIXME.
h4. +try+
Sometimes you want to call a method provided the receiver object is not +nil+, which is something you usually check first.
For instance, note how this method of +ActiveRecord::ConnectionAdapters::AbstractAdapter+ checks if there's a +@logger+:
<ruby>
def log_info(sql, name, ms)
if @logger && @logger.debug?
name = '%s (%.1fms)' % [name || 'SQL', ms]
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
end
end
</ruby>
You can shorten that using +Object#try+. This method is a synonim for +Object#send+ except that it returns +nil+ if sent to +nil+. The previous example could then be rewritten as:
<ruby>
def log_info(sql, name, ms)
if @logger.try(:debug?)
name = '%s (%.1fms)' % [name || 'SQL', ms]
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
end
end
</ruby>
h4. +metaclass+
The method +metaclass+ returns the singleton class on any object:
<ruby>
String.metaclass # => #<Class:String>
String.new.metaclass # => #<Class:#<String:0x17a1d1c>>
</ruby>
h4. +class_eval(*args, &block)+
You can evaluate code in the context of any object's singleton class using +class_eval+:
<ruby>
class Proc
def bind(object)
block, time = self, Time.now
object.class_eval do
method_name = "__bind_#{time.to_i}_#{time.usec}"
define_method(method_name, &block)
method = instance_method(method_name)
remove_method(method_name)
method
end.bind(object)
end
end
</ruby>
h4. +acts_like?(duck)+
The method +acts_like+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines
<ruby>
def acts_like_string?
end
</ruby>
which is only a marker, its body or return value are irrelevant. Then, client code can query for duck-type-safeness this way:
<ruby>
some_klass.acts_like?(:string)
</ruby>
Rails has classes that act like +Date+ or +Time+ and follow this contract.
h4. +to_param+
All objects in Rails respond to the method +to_param+, which is meant to return something that represents them as values in a query string, or as a URL fragments.
By default +to_param+ just calls +to_s+:
<ruby>
7.to_param # => "7"
</ruby>
The return value of +to_param+ should *not* be escaped:
<ruby>
"Tom & Jerry".to_param # => "Tom & Jerry"
</ruby>
Several classes in Rails overwrite this method.
For example +nil+, +true+, and +false+ return themselves. +Array#to_param+ calls +to_param+ on the elements and joins the result with "/":
<ruby>
[0, true, String].to_param # => "0/true/String"
</ruby>
Notably, the Rails routing system calls +to_param+ on models to get a value for the +:id+ placeholder. +ActiveRecord::Base#to_param+ returns the +id+ of a model, but you can redefine that method in your models. For example, given
<ruby>
class User
def to_param
"#{id}-#{name.parameterize}"
end
end
</ruby>
we get:
<ruby>
user_path(@user) # => "/users/357-john-smith"
</ruby>
WARNING. Controllers need to be aware of any redifinition of +to_param+ because when a request like that comes in "357-john-smith" is the value of +params[:id]+.
h4. +to_query+
Except for hashes, given an unescaped +key+ this method constructs the part of a query string that would map such key to what +to_param+ returns. For example, given
<ruby>
class User
def to_param
"#{id}-#{name.parameterize}"
end
end
</ruby>
we get:
<ruby>
current_user.to_query('user') # => user=357-john-smith
</ruby>
This method escapes whatever is needed, both for the key and the value:
<ruby>
account.to_query('company[name]')
# => "company%5Bname%5D=Johnson+%26+Johnson"
</ruby>
so its output is ready to be used in a query string.
Arrays return the result of applying +to_query+ to each element with <tt>_key_[]</tt> as key, and join the result with "/":
<ruby>
[3.4, -45.6].to_query('sample')
# => "sample%5B%5D=3.4&sample%5B%5D=-45.6"
</ruby>
Hashes also respond to +to_query+ but with a different signature. If no argument is passed a call generates a sorted series of key/value assigments calling +to_query(key)+ on its values. Then it joins the result with "&":
<ruby>
{:c => 3, :b => 2, :a => 1}.to_query # => "a=1&b=2&c=3"
</ruby>
The method +Hash#to_query+ accepts an optional namespace for the keys:
<ruby>
{:id => 89, :name => "John Smith"}.to_query('user')
# => "user%5Bid%5D=89&user%5Bname%5D=John+Smith"
</ruby>
h4. Silencing Warnings, Streams, and Exceptions
The methods +silence_warnings+ and +enable_warnings+ change the value of +$VERBOSE+ accordingly for the duration of their block, and reset it afterwards:
<ruby>
silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
</ruby>
You can silence any stream while a block runs with +silence_stream+:
<ruby>
silence_stream(STDOUT) do
# STDOUT is silent here
end
</ruby>
Silencing exceptions is also possible with +suppress+. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is +kind_of?+ any of the arguments, +suppress+ captures it and returns silently. Otherwise the exception is reraised:
<ruby>
# If the user is locked the increment is lost, no big deal.
suppress(ActiveRecord::StaleObjectError) do
current_user.increment! :visits
end
</ruby>
h3. Extensions to +Module+
...
h3. Extensions to +Class+
h4. Class Attribute Accessors
The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it:
<ruby>
class MysqlAdapter < AbstractAdapter
# Generates class methods to access @@emulate_booleans.
cattr_accessor :emulate_booleans
self.emulate_booleans = true
end
</ruby>
Instance methods are created as well for convenience. For example given
<ruby>
module ActionController
class Base
cattr_accessor :logger
end
end
</ruby>
we can access +logger+ in actions. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
<ruby>
module ActiveRecord
class Base
# No pluralize_table_names= instance writer is generated.
cattr_accessor :pluralize_table_names, :instance_writer => false
end
end
</ruby>
h4. Class Inheritable Attributes
Class variables are shared down the inheritance tree. Class instance variables are not shared, but they are not inherited either. The macros +class_inheritable_reader+, +class_inheritable_writer+, and +class_inheritable_accessor+ provide accesors for class-level data which is inherited but not shared with children:
<ruby>
module ActionController
class Base
# FIXME: REVISE/SIMPLIFY THIS COMMENT.
# The value of allow_forgery_protection is inherited,
# but its value in a particular class does not affect
# the value in the rest of the controllers hierarchy.
class_inheritable_accessor :allow_forgery_protection
end
end
</ruby>
They accomplish this with class instance variables and cloning on subclassing, there are no class variables involved. Cloning is performed with +dup+ as long as the value is duplicable.
There are some variants specialised in arrays and hashes:
<ruby>
class_inheritable_array
class_inheritable_hash
</ruby>
Those writers take any inherited array or hash into account and extend them rather than overwrite them.
As with vanilla class attribute accessors these macros create convenience instance methods for reading and writing. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
<ruby>
module ActiveRecord
class Base
class_inheritable_accessor :default_scoping, :instance_writer => false
end
end
</ruby>
Since values are copied when a subclass is defined, if the base class changes the attribute after that, the subclass does not see the new value. That's the point.
There's a related macro called +superclass_delegating_accessor+, however, that does not copy the value when the base class is subclassed. Instead, it delegates reading to the superclass as long as the attribute is not set via its own writer. For example, +ActionMailer::Base+ defines +delivery_method+ this way:
<ruby>
module ActionMailer
class Base
superclass_delegating_accessor :delivery_method
self.delivery_method = :smtp
end
end
</ruby>
If for whatever reason an application loads the definition of a mailer class and after that sets +ActionMailer::Base.delivery_method+, the mailer class will still see the new value. In addition, the mailer class is able to change the +delivery_method+ without affecting the value in the parent using its own inherited class attribute writer.
h4. Subclasses
The +subclasses+ method returns the names of all subclasses of a given class as an array of strings. That comprises not only direct subclasses, but all descendants down the hierarchy:
<ruby>
class C; end
C.subclasses # => []
Integer.subclasses # => ["Bignum", "Fixnum"]
module M
class A; end
class B1 < A; end
class B2 < A; end
end
module N
class C < M::B1; end
end
M::A.subclasses # => ["N::C", "M::B2", "M::B1"]
</ruby>
The order in which these class names are returned is unspecified.
See also +Object#subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME.
h4. Class Removal
Roughly speaking, the +remove_class+ method removes the class objects passed as arguments:
<ruby>
Class.remove_class(Hash, Dir) # => [Hash, Dir]
Hash # => NameError: uninitialized constant Hash
Dir # => NameError: uninitialized constant Dir
</ruby>
More specifically, +remove_class+ attempts to remove constants with the same name as the passed class objects from their parent modules. So technically this method does not guarantee the class objects themselves are not still valid and alive somewhere after the method call:
<ruby>
module M
class A; end
class B < A; end
end
A2 = M::A
M::A.object_id # => 13053950
Class.remove_class(M::A)
M::B.superclass.object_id # => 13053950 (same object as before)
A2.name # => "M::A" (name is hard-coded in object)
</ruby>
WARNING: Removing fundamental classes like +String+ can result in really funky behaviour.
The method +remove_subclasses+ provides a shortcut for removing all descendants of a given class, where "removing" has the meaning explained above:
<ruby>
class A; end
class B1 < A; end
class B2 < A; end
class C < A; end
A.subclasses # => ["C", "B2", "B1"]
A.remove_subclasses
A.subclasses # => []
C # => NameError: uninitialized constant C
</ruby>
See also +Object#remove_subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME.
h3. Extensions to +NilClass+
...
h3. Extensions to +TrueClass+
...
h3. Extensions to +FalseClass+
...
h3. Extensions to +Symbol+
...
h3. Extensions to +String+
...
h3. Extensions to +Numeric+
...
h3. Extensions to +Integer+
...
h3. Extensions to +Float+
...
h3. Extensions to +BigDecimal+
...
h3. Extensions to +Enumerable+
...
h3. Extensions to +Array+
h4. Accessing
Active Support augments the API of arrays to ease certain ways of accessing them. For example, +to+ returns the subarray of elements up to the one at the passed index:
<ruby>
%w(a b c d).to(2) # => %w(a b c)
[].to(7) # => []
</ruby>
Similarly, +from+ returns the tail from the element at the passed index on:
<ruby>
%w(a b c d).from(2) # => %w(c d)
%w(a b c d).from(10) # => nil
[].from(0) # => nil
</ruby>
The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is builtin). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available.
You can pick a random element with +rand+:
<ruby>
shape_type = [Circle, Square, Triangle].rand
</ruby>
h4. Grouping
h5. +in_groups_of(number, fill_with = nil)+
The method +in_groups_of+ splits an array into consecutive groups of a certain size. It returns an array with the groups:
<ruby>
[1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
</ruby>
or yields them in turn if a block is passed:
<ruby>
<% sample.in_groups_of(3) do |a, b, c| %>
<tr>
<td><%=h a %></td>
<td><%=h b %></td>
<td><%=h c %></td>
</tr>
<% end %>
</ruby>
The first example shows +in_groups_of+ fills the last group with as many +nil+ elements as needed to have the requested size. You can change this padding value using the second optional argument:
<ruby>
[1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
</ruby>
And you can tell the method not to fill the last group passing +false+:
<ruby>
[1, 2, 3].in_groups_of(2, false) # => [[1, 2], [3]]
</ruby>
As a consequence +false+ can't be a used as a padding value.
h5. +in_groups(number, fill_with = nil)+
The method +in_groups+ splits an array into a certain number of groups. The method returns and array with the groups:
<ruby>
%w(1 2 3 4 5 6 7).in_groups(3)
# => [["1", "2", "3"], ["4", "5", nil], ["6", "7", nil]]
</ruby>
or yields them in turn if a block is passed:
<ruby>
%w(1 2 3 4 5 6 7).in_groups(3) {|group| p group}
["1", "2", "3"]
["4", "5", nil]
["6", "7", nil]
</ruby>
The examples above show that +in_groups+ fills some groups with a trailing +nil+ element as needed. A group can get at most one of these extra elements, the rightmost one if any. And the groups that have them are always the last ones.
You can change this padding value using the second optional argument:
<ruby>
%w(1 2 3 4 5 6 7).in_groups(3, "0")
# => [["1", "2", "3"], ["4", "5", "0"], ["6", "7", "0"]]
</ruby>
And you can tell the method not to fill the smaller groups passing +false+:
<ruby>
%w(1 2 3 4 5 6 7).in_groups(3, false)
# => [["1", "2", "3"], ["4", "5"], ["6", "7"]]
</ruby>
As a consequence +false+ can't be a used as a padding value.
h5. +split(value = nil)+
The method +split+ divides an array by a separator and returns the resulting chunks.
If a block is passed the separators are those elements of the array for which the block returns true:
<ruby>
(-5..5).to_a.split { |i| i.multiple_of?(4) }
# => [[-5], [-3, -2, -1], [1, 2, 3], [5]]
</ruby>
Otherwise, the value received as argument, which defaults to +nil+, is the separator:
<ruby>
[0, 1, -5, 1, 1, "foo", "bar"].split(1)
# => [[0], [-5], [], ["foo", "bar"]]
</ruby>
NOTE: Observe in the previous example that consecutive separators result in empty arrays.
h3. Extensions to +Hash+
...
h3. Extensions to +Range+
...
h3. Extensions to +Proc+
...
h3. Extensions to +Date+
...
h3. Extensions to +DateTime+
...
h3. Extensions to +Time+
...
h3. Extensions to +Process+
...
h3. Extensions to +Pathname+
...
h3. Extensions to +File+
...
h3. Extensions to +Exception+
...
h3. Extensions to +NameError+
...
h3. Extensions to +LoadError+
...
h3. Extensions to +CGI+
...
h3. Extensions to +Benchmark+
...
h3. Changelog
"Lighthouse ticket":https://rails.lighthouseapp.com/projects/16213/tickets/67
* April 18, 2009: Initial version by "Xavier Noria":credits.html#fxn
|