active_support_core_extensions.md 115.7 KB
Newer Older
1 2
Active Support Core Extensions
==============================
3

X
Xavier Noria 已提交
4
Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.
5

X
Xavier Noria 已提交
6 7
It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Ruby on Rails itself.

8
After reading this guide, you will know:
9

10 11 12
* What Core Extensions are.
* How to load all extensions.
* How to cherry-pick just the extensions you want.
Y
Yves Senn 已提交
13
* What extensions Active Support provides.
14

15
--------------------------------------------------------------------------------
16

17 18
How to Load Core Extensions
---------------------------
19

20
### Stand-Alone Active Support
21

22
In order to have a near-zero default footprint, Active Support does not load anything by default. It is broken in small pieces so that you can load just what you need, and also has some convenience entry points to load related extensions in one shot, even everything.
23 24 25

Thus, after a simple require like:

26
```ruby
27
require 'active_support'
28
```
29

30
objects do not even respond to `blank?`. Let's see how to load its definition.
31

32
#### Cherry-picking a Definition
33

34
The most lightweight way to get `blank?` is to cherry-pick the file that defines it.
35

36
For every single method defined as a core extension this guide has a note that says where such a method is defined. In the case of `blank?` the note reads:
37

38
NOTE: Defined in `active_support/core_ext/object/blank.rb`.
39

40
That means that you can require it like this:
41

42
```ruby
43
require 'active_support'
44
require 'active_support/core_ext/object/blank'
45
```
46 47 48

Active Support has been carefully revised so that cherry-picking a file loads only strictly needed dependencies, if any.

49
#### Loading Grouped Core Extensions
50

51
The next level is to simply load all extensions to `Object`. As a rule of thumb, extensions to `SomeClass` are available in one shot by loading `active_support/core_ext/some_class`.
52

53
Thus, to load all extensions to `Object` (including `blank?`):
54

55
```ruby
56
require 'active_support'
57
require 'active_support/core_ext/object'
58
```
59

60
#### Loading All Core Extensions
61 62 63

You may prefer just to load all core extensions, there is a file for that:

64
```ruby
65
require 'active_support'
66
require 'active_support/core_ext'
67
```
68

69
#### Loading All Active Support
70 71 72

And finally, if you want to have all Active Support available just issue:

73
```ruby
74
require 'active_support/all'
75
```
76

77
That does not even put the entire Active Support in memory upfront indeed, some stuff is configured via `autoload`, so it is only loaded if used.
78

79
### Active Support Within a Ruby on Rails Application
80

81
A Ruby on Rails application loads all Active Support unless `config.active_support.bare` is true. In that case, the application will only load what the framework itself cherry-picks for its own needs, and can still cherry-pick itself at any granularity level, as explained in the previous section.
82

83 84
Extensions to All Objects
-------------------------
85

86
### `blank?` and `present?`
87 88 89

The following values are considered to be blank in a Rails application:

90
* `nil` and `false`,
91

92
* strings composed only of whitespace (see note below),
93 94 95

* empty arrays and hashes, and

96
* any other object that responds to `empty?` and is empty.
97

98
INFO: The predicate for strings uses the Unicode-aware character class `[:space:]`, so for example U+2029 (paragraph separator) is considered to be whitespace.
99

A
Agis Anastasopoulos 已提交
100
WARNING: Note that numbers are not mentioned. In particular, 0 and 0.0 are **not** blank.
101

102
For example, this method from `ActionController::HttpAuthentication::Token::ControllerMethods` uses `blank?` for checking whether a token is present:
103

104
```ruby
105 106 107 108
def authenticate(controller, &login_procedure)
  token, options = token_and_options(controller.request)
  unless token.blank?
    login_procedure.call(token, options)
X
Xavier Noria 已提交
109
  end
110
end
111
```
112

113
The method `present?` is equivalent to `!blank?`. This example is taken from `ActionDispatch::Http::Cache::Response`:
114

115
```ruby
X
Xavier Noria 已提交
116 117 118
def set_conditional_cache_control!
  return if self["Cache-Control"].present?
  ...
119
end
120
```
121

122
NOTE: Defined in `active_support/core_ext/object/blank.rb`.
123

124
### `presence`
X
Xavier Noria 已提交
125

126
The `presence` method returns its receiver if `present?`, and `nil` otherwise. It is useful for idioms like this:
X
Xavier Noria 已提交
127

128
```ruby
X
Xavier Noria 已提交
129
host = config[:host].presence || 'localhost'
130
```
X
Xavier Noria 已提交
131

132
NOTE: Defined in `active_support/core_ext/object/blank.rb`.
133

134
### `duplicable?`
135

136
A few fundamental objects in Ruby are singletons. For example, in the whole life of a program the integer 1 refers always to the same instance:
137

138
```ruby
139 140
1.object_id                 # => 3
Math.cos(0).to_i.object_id  # => 3
141
```
142

143
Hence, there's no way these objects can be duplicated through `dup` or `clone`:
144

145
```ruby
146
true.dup  # => TypeError: can't dup TrueClass
147
```
148 149 150

Some numbers which are not singletons are not duplicable either:

151
```ruby
152 153
0.0.clone        # => allocator undefined for Float
(2**1024).clone  # => allocator undefined for Bignum
154
```
155

156
Active Support provides `duplicable?` to programmatically query an object about this property:
157

158
```ruby
A
Agis Anastasopoulos 已提交
159
"foo".duplicable? # => true
160
"".duplicable?    # => true
A
Agis Anastasopoulos 已提交
161
0.0.duplicable?   # => false
162
false.duplicable? # => false
163
```
164

165
By definition all objects are `duplicable?` except `nil`, `false`, `true`, symbols, numbers, class, and module objects.
166

167
WARNING: Any class can disallow duplication by removing `dup` and `clone` or raising exceptions from them. Thus only `rescue` can tell whether a given arbitrary object is duplicable. `duplicable?` depends on the hard-coded list above, but it is much faster than `rescue`. Use it only if you know the hard-coded list is enough in your use case.
168

169
NOTE: Defined in `active_support/core_ext/object/duplicable.rb`.
170

171
### `deep_dup`
A
Alexey Gaziev 已提交
172

P
Prathamesh Sonpatki 已提交
173
The `deep_dup` method returns deep copy of a given object. Normally, when you `dup` an object that contains other objects, Ruby does not `dup` them, so it creates a shallow copy of the object. If you have an array with a string, for example, it will look like this:
A
Alexey Gaziev 已提交
174

175
```ruby
176 177 178 179 180
array     = ['string']
duplicate = array.dup

duplicate.push 'another-string'

A
Agis Anastasopoulos 已提交
181
# the object was duplicated, so the element was added only to the duplicate
A
Alex Johnson 已提交
182 183
array     # => ['string']
duplicate # => ['string', 'another-string']
184 185 186

duplicate.first.gsub!('string', 'foo')

A
Agis Anastasopoulos 已提交
187
# first element was not duplicated, it will be changed in both arrays
A
Alex Johnson 已提交
188 189
array     # => ['foo']
duplicate # => ['foo', 'another-string']
190
```
A
Alexey Gaziev 已提交
191

A
Agis Anastasopoulos 已提交
192
As you can see, after duplicating the `Array` instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since `dup` does not make deep copy, the string inside the array is still the same object.
A
Alexey Gaziev 已提交
193

194
If you need a deep copy of an object, you should use `deep_dup`. Here is an example:
A
Alexey Gaziev 已提交
195

196
```ruby
197
array     = ['string']
198
duplicate = array.deep_dup
199 200 201

duplicate.first.gsub!('string', 'foo')

A
Alex Johnson 已提交
202 203
array     # => ['string']
duplicate # => ['foo']
204
```
A
Alexey Gaziev 已提交
205

A
Agis Anastasopoulos 已提交
206
If the object is not duplicable, `deep_dup` will just return it:
A
Alexey Gaziev 已提交
207

208
```ruby
A
Alexey Gaziev 已提交
209
number = 1
A
Agis Anastasopoulos 已提交
210 211
duplicate = number.deep_dup
number.object_id == duplicate.object_id   # => true
212
```
A
Alexey Gaziev 已提交
213

214
NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
A
Alexey Gaziev 已提交
215

216
### `try`
217

218
When you want to call a method on an object only if it is not `nil`, the simplest way to achieve it is with conditional statements, adding unnecessary clutter. The alternative is to use `try`. `try` is like `Object#send` except that it returns `nil` if sent to `nil`.
219 220

Here is an example:
V
Vijay Dev 已提交
221

222
```ruby
223 224 225 226 227 228 229
# without try
unless @number.nil?
  @number.next
end

# with try
@number.try(:next)
230
```
231

232
Another example is this code from `ActiveRecord::ConnectionAdapters::AbstractAdapter` where `@logger` could be `nil`. You can see that the code uses `try` and avoids an unnecessary check.
233

234
```ruby
235 236 237 238 239 240
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
241
```
242

243
`try` can also be called without arguments but a block, which will only be executed if the object is not nil:
J
José Valim 已提交
244

245
```ruby
J
José Valim 已提交
246
@person.try { |p| "#{p.first_name} #{p.last_name}" }
247
```
J
José Valim 已提交
248

249
NOTE: Defined in `active_support/core_ext/object/try.rb`.
250

251
### `class_eval(*args, &block)`
252

253
You can evaluate code in the context of any object's singleton class using `class_eval`:
254

255
```ruby
256 257
class Proc
  def bind(object)
258
    block, time = self, Time.current
259 260 261 262 263 264 265 266 267
    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
268
```
269

270
NOTE: Defined in `active_support/core_ext/kernel/singleton_class.rb`.
271

272
### `acts_like?(duck)`
273

274
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
275

276
```ruby
277 278
def acts_like_string?
end
279
```
280 281 282

which is only a marker, its body or return value are irrelevant. Then, client code can query for duck-type-safeness this way:

283
```ruby
284
some_klass.acts_like?(:string)
285
```
286

287
Rails has classes that act like `Date` or `Time` and follow this contract.
288

289
NOTE: Defined in `active_support/core_ext/object/acts_like.rb`.
290

291
### `to_param`
292

293
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 URL fragments.
294

295
By default `to_param` just calls `to_s`:
296

297
```ruby
298
7.to_param # => "7"
299
```
300

301
The return value of `to_param` should **not** be escaped:
302

303
```ruby
304
"Tom & Jerry".to_param # => "Tom & Jerry"
305
```
306 307 308

Several classes in Rails overwrite this method.

309
For example `nil`, `true`, and `false` return themselves. `Array#to_param` calls `to_param` on the elements and joins the result with "/":
310

311
```ruby
312
[0, true, String].to_param # => "0/true/String"
313
```
314

315
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
316

317
```ruby
318 319 320 321 322
class User
  def to_param
    "#{id}-#{name.parameterize}"
  end
end
323
```
324 325 326

we get:

327
```ruby
328
user_path(@user) # => "/users/357-john-smith"
329
```
330

331
WARNING. Controllers need to be aware of any redefinition of `to_param` because when a request like that comes in "357-john-smith" is the value of `params[:id]`.
332

333
NOTE: Defined in `active_support/core_ext/object/to_param.rb`.
334

335
### `to_query`
336

337
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
338

339
```ruby
340 341 342 343 344
class User
  def to_param
    "#{id}-#{name.parameterize}"
  end
end
345
```
346 347 348

we get:

349
```ruby
350
current_user.to_query('user') # => user=357-john-smith
351
```
352 353 354

This method escapes whatever is needed, both for the key and the value:

355
```ruby
356
account.to_query('company[name]')
357
# => "company%5Bname%5D=Johnson+%26+Johnson"
358
```
359 360 361

so its output is ready to be used in a query string.

362
Arrays return the result of applying `to_query` to each element with `_key_[]` as key, and join the result with "&":
363

364
```ruby
365 366
[3.4, -45.6].to_query('sample')
# => "sample%5B%5D=3.4&sample%5B%5D=-45.6"
367
```
368

369
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 assignments calling `to_query(key)` on its values. Then it joins the result with "&":
370

371
```ruby
372
{c: 3, b: 2, a: 1}.to_query # => "a=1&b=2&c=3"
373
```
374

375
The method `Hash#to_query` accepts an optional namespace for the keys:
376

377
```ruby
378
{id: 89, name: "John Smith"}.to_query('user')
379
# => "user%5Bid%5D=89&user%5Bname%5D=John+Smith"
380
```
381

382
NOTE: Defined in `active_support/core_ext/object/to_query.rb`.
383

384
### `with_options`
385

386
The method `with_options` provides a way to factor out common options in a series of method calls.
387

388
Given a default options hash, `with_options` yields a proxy object to a block. Within the block, methods called on the proxy are forwarded to the receiver with their options merged. For example, you get rid of the duplication in:
389

390
```ruby
391
class Account < ActiveRecord::Base
392 393 394 395
  has_many :customers, dependent: :destroy
  has_many :products,  dependent: :destroy
  has_many :invoices,  dependent: :destroy
  has_many :expenses,  dependent: :destroy
396
end
397
```
398 399 400

this way:

401
```ruby
402
class Account < ActiveRecord::Base
403
  with_options dependent: :destroy do |assoc|
404 405 406 407 408 409
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end
410
```
411 412 413

That idiom may convey _grouping_ to the reader as well. For example, say you want to send a newsletter whose language depends on the user. Somewhere in the mailer you could group locale-dependent bits like this:

414
```ruby
415
I18n.with_options locale: user.locale, scope: "newsletter" do |i18n|
416
  subject i18n.t :subject
417
  body    i18n.t :body, user_name: user.name
418
end
419
```
420

421
TIP: Since `with_options` forwards calls to its receiver they can be nested. Each nesting level will merge inherited defaults in addition to their own.
422

423
NOTE: Defined in `active_support/core_ext/object/with_options.rb`.
424

425 426
### JSON support

427
Active Support provides a better implementation of `to_json` than the `json` gem ordinarily provides for Ruby objects. This is because some classes, like `Hash`, `OrderedHash` and `Process::Status` need special handling in order to provide a proper JSON representation.
428

429
NOTE: Defined in `active_support/core_ext/object/json.rb`.
430

431
### Instance Variables
432 433 434

Active Support provides several methods to ease access to instance variables.

435
#### `instance_values`
436

437
The method `instance_values` returns a hash that maps instance variable names without "@" to their
438
corresponding values. Keys are strings:
439

440
```ruby
441 442 443 444 445 446 447
class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
448
```
449

450
NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
#### `instance_variable_names`

The method `instance_variable_names` returns an array.  Each name includes the "@" sign.

```ruby
class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_variable_names # => ["@x", "@y"]
```

NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.

468
### Silencing Warnings, Streams, and Exceptions
469

470
The methods `silence_warnings` and `enable_warnings` change the value of `$VERBOSE` accordingly for the duration of their block, and reset it afterwards:
471

472
```ruby
473
silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
474
```
475

476
You can silence any stream while a block runs with `silence_stream`:
477

478
```ruby
479 480 481
silence_stream(STDOUT) do
  # STDOUT is silent here
end
482
```
483

484
The `quietly` method addresses the common use case where you want to silence STDOUT and STDERR, even in subprocesses:
485

486
```ruby
487
quietly { system 'bundle install' }
488
```
489 490 491

For example, the railties test suite uses that one in a few places to prevent command messages from being echoed intermixed with the progress status.

492
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:
493

494
```ruby
495 496 497 498
# If the user is locked the increment is lost, no big deal.
suppress(ActiveRecord::StaleObjectError) do
  current_user.increment! :visits
end
499
```
500

501
NOTE: Defined in `active_support/core_ext/kernel/reporting.rb`.
502

503
### `in?`
504

505
The predicate `in?` tests if an object is included in another object. An `ArgumentError` exception will be raised if the argument passed does not respond to `include?`.
506

507
Examples of `in?`:
508

509
```ruby
V
Vijay Dev 已提交
510 511 512
1.in?([1,2])        # => true
"lo".in?("hello")   # => true
25.in?(30..50)      # => false
513
1.in?(1)            # => ArgumentError
514
```
515

516
NOTE: Defined in `active_support/core_ext/object/inclusion.rb`.
517

518
Extensions to `Module`
519
----------------------
520

521
### `alias_method_chain`
522 523 524

Using plain Ruby you can wrap methods with other methods, that's called _alias chaining_.

525
For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap `ActionController::TestCase#process` this way in `test/test_helper.rb`:
526

527
```ruby
528 529 530 531 532 533 534 535 536 537
ActionController::TestCase.class_eval do
  # save a reference to the original process method
  alias_method :original_process, :process

  # now redefine process and delegate to original_process
  def process(action, params=nil, session=nil, flash=nil, http_method='GET')
    params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
    original_process(action, params, session, flash, http_method)
  end
end
538
```
539

540
That's the method `get`, `post`, etc., delegate the work to.
541

542
That technique has a risk, it could be the case that `:original_process` was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about:
543

544
```ruby
545 546 547 548 549 550 551 552
ActionController::TestCase.class_eval do
  def process_with_stringified_params(...)
    params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
    process_without_stringified_params(action, params, session, flash, http_method)
  end
  alias_method :process_without_stringified_params, :process
  alias_method :process, :process_with_stringified_params
end
553
```
554

555
The method `alias_method_chain` provides a shortcut for that pattern:
556

557
```ruby
558 559 560 561 562 563 564
ActionController::TestCase.class_eval do
  def process_with_stringified_params(...)
    params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
    process_without_stringified_params(action, params, session, flash, http_method)
  end
  alias_method_chain :process, :stringified_params
end
565
```
566

567
Rails uses `alias_method_chain` all over the code base. For example validations are added to `ActiveRecord::Base#save` by wrapping the method that way in a separate module specialized in validations.
568

569
NOTE: Defined in `active_support/core_ext/module/aliasing.rb`.
570

571
### Attributes
572

573
#### `alias_attribute`
574

V
Vijay Dev 已提交
575
Model attributes have a reader, a writer, and a predicate. You can alias a model attribute having the corresponding three methods defined for you in one shot. As in other aliasing methods, the new name is the first argument, and the old name is the second (my mnemonic is they go in the same order as if you did an assignment):
576

577
```ruby
578 579
class User < ActiveRecord::Base
  # let me refer to the email column as "login",
580
  # possibly meaningful for authentication code
581 582
  alias_attribute :login, :email
end
583
```
584

585
NOTE: Defined in `active_support/core_ext/module/aliasing.rb`.
586

587
#### Internal Attributes
588

R
comma  
rpq 已提交
589
When you are defining an attribute in a class that is meant to be subclassed, name collisions are a risk. That's remarkably important for libraries.
590

591
Active Support defines the macros `attr_internal_reader`, `attr_internal_writer`, and `attr_internal_accessor`. They behave like their Ruby built-in `attr_*` counterparts, except they name the underlying instance variable in a way that makes collisions less likely.
592

593
The macro `attr_internal` is a synonym for `attr_internal_accessor`:
594

595
```ruby
596 597 598 599 600 601 602 603 604
# library
class ThirdPartyLibrary::Crawler
  attr_internal :log_level
end

# client code
class MyCrawler < ThirdPartyLibrary::Crawler
  attr_accessor :log_level
end
605
```
606

607
In the previous example it could be the case that `:log_level` does not belong to the public interface of the library and it is only used for development. The client code, unaware of the potential conflict, subclasses and defines its own `:log_level`. Thanks to `attr_internal` there's no collision.
608

609
By default the internal instance variable is named with a leading underscore, `@_log_level` in the example above. That's configurable via `Module.attr_internal_naming_format` though, you can pass any `sprintf`-like format string with a leading `@` and a `%s` somewhere, which is where the name will be placed. The default is `"@_%s"`.
610 611 612

Rails uses internal attributes in a few spots, for examples for views:

613
```ruby
614 615 616 617 618 619 620
module ActionView
  class Base
    attr_internal :captures
    attr_internal :request, :layout
    attr_internal :controller, :template
  end
end
621
```
622

623
NOTE: Defined in `active_support/core_ext/module/attr_internal.rb`.
624

625
#### Module Attributes
626

627
The macros `mattr_reader`, `mattr_writer`, and `mattr_accessor` are the same as the `cattr_*` macros defined for class. In fact, the `cattr_*` macros are just aliases for the `mattr_*` macros. Check [Class Attributes](#class-attributes).
628 629 630

For example, the dependencies mechanism uses them:

631
```ruby
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
module ActiveSupport
  module Dependencies
    mattr_accessor :warnings_on_first_load
    mattr_accessor :history
    mattr_accessor :loaded
    mattr_accessor :mechanism
    mattr_accessor :load_paths
    mattr_accessor :load_once_paths
    mattr_accessor :autoloaded_constants
    mattr_accessor :explicitly_unloadable_constants
    mattr_accessor :logger
    mattr_accessor :log_activity
    mattr_accessor :constant_watch_stack
    mattr_accessor :constant_watch_stack_mutex
  end
end
648
```
649

650
NOTE: Defined in `active_support/core_ext/module/attribute_accessors.rb`.
651

652
### Parents
653

654
#### `parent`
655

656
The `parent` method on a nested named module returns the module that contains its corresponding constant:
657

658
```ruby
659 660 661 662 663 664 665 666 667 668
module X
  module Y
    module Z
    end
  end
end
M = X::Y::Z

X::Y::Z.parent # => X::Y
M.parent       # => X::Y
669
```
670

671
If the module is anonymous or belongs to the top-level, `parent` returns `Object`.
672

673
WARNING: Note that in that case `parent_name` returns `nil`.
674

675
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
676

677
#### `parent_name`
678

679
The `parent_name` method on a nested named module returns the fully-qualified name of the module that contains its corresponding constant:
680

681
```ruby
682 683 684 685 686 687 688 689 690 691
module X
  module Y
    module Z
    end
  end
end
M = X::Y::Z

X::Y::Z.parent_name # => "X::Y"
M.parent_name       # => "X::Y"
692
```
693

694
For top-level or anonymous modules `parent_name` returns `nil`.
695

696
WARNING: Note that in that case `parent` returns `Object`.
697

698
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
699

700
#### `parents`
701

702
The method `parents` calls `parent` on the receiver and upwards until `Object` is reached. The chain is returned in an array, from bottom to top:
703

704
```ruby
705 706 707 708 709 710 711 712 713 714
module X
  module Y
    module Z
    end
  end
end
M = X::Y::Z

X::Y::Z.parents # => [X::Y, X, Object]
M.parents       # => [X::Y, X, Object]
715
```
716

717
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
718

719
### Constants
720

721
The method `local_constants` returns the names of the constants that have been
722
defined in the receiver module:
723

724
```ruby
725 726 727 728 729 730 731 732 733
module X
  X1 = 1
  X2 = 2
  module Y
    Y1 = :y1
    X1 = :overrides_X1_above
  end
end

734 735
X.local_constants    # => [:X1, :X2, :Y]
X::Y.local_constants # => [:Y1, :X1]
736
```
737

738
The names are returned as symbols.
739

740
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
741

742
#### Qualified Constant Names
743

744
The standard methods `const_defined?`, `const_get` , and `const_set` accept
745
bare constant names. Active Support extends this API to be able to pass
746
relative qualified constant names.
747

748 749
The new methods are `qualified_const_defined?`, `qualified_const_get`, and
`qualified_const_set`. Their arguments are assumed to be qualified constant
750 751
names relative to their receiver:

752
```ruby
753 754 755
Object.qualified_const_defined?("Math::PI")       # => true
Object.qualified_const_get("Math::PI")            # => 3.141592653589793
Object.qualified_const_set("Math::Phi", 1.618034) # => 1.618034
756
```
757 758 759

Arguments may be bare constant names:

760
```ruby
761
Math.qualified_const_get("E") # => 2.718281828459045
762
```
763 764

These methods are analogous to their builtin counterparts. In particular,
765
`qualified_constant_defined?` accepts an optional second argument to be
766
able to say whether you want the predicate to look in the ancestors.
767 768 769 770 771
This flag is taken into account for each constant in the expression while
walking down the path.

For example, given

772
```ruby
773 774 775 776 777 778 779 780 781
module M
  X = 1
end

module N
  class C
    include M
  end
end
782
```
783

784
`qualified_const_defined?` behaves this way:
785

786
```ruby
787 788 789
N.qualified_const_defined?("C::X", false) # => false
N.qualified_const_defined?("C::X", true)  # => true
N.qualified_const_defined?("C::X")        # => true
790
```
791

792
As the last example implies, the second argument defaults to true,
793
as in `const_defined?`.
794 795

For coherence with the builtin methods only relative paths are accepted.
796
Absolute qualified constant names like `::Math::PI` raise `NameError`.
797

798
NOTE: Defined in `active_support/core_ext/module/qualified_const.rb`.
799

800
### Reachable
801

802
A named module is reachable if it is stored in its corresponding constant. It means you can reach the module object via the constant.
803

804
That is what ordinarily happens, if a module is called "M", the `M` constant exists and holds it:
805

806
```ruby
807 808 809 810
module M
end

M.reachable? # => true
811
```
812 813 814

But since constants and modules are indeed kind of decoupled, module objects can become unreachable:

815
```ruby
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
module M
end

orphan = Object.send(:remove_const, :M)

# The module object is orphan now but it still has a name.
orphan.name # => "M"

# You cannot reach it via the constant M because it does not even exist.
orphan.reachable? # => false

# Let's define a module called "M" again.
module M
end

# The constant M exists now again, and it stores a module
# object called "M", but it is a new instance.
orphan.reachable? # => false
834
```
835

836
NOTE: Defined in `active_support/core_ext/module/reachable.rb`.
837

838
### Anonymous
839 840 841

A module may or may not have a name:

842
```ruby
843 844 845 846 847 848 849
module M
end
M.name # => "M"

N = Module.new
N.name # => "N"

850
Module.new.name # => nil
851
```
852

853
You can check whether a module has a name with the predicate `anonymous?`:
854

855
```ruby
856 857 858 859 860
module M
end
M.anonymous? # => false

Module.new.anonymous? # => true
861
```
862 863 864

Note that being unreachable does not imply being anonymous:

865
```ruby
866 867 868 869 870 871 872
module M
end

m = Object.send(:remove_const, :M)

m.reachable? # => false
m.anonymous? # => false
873
```
874 875 876

though an anonymous module is unreachable by definition.

877
NOTE: Defined in `active_support/core_ext/module/anonymous.rb`.
878

879
### Method Delegation
880

881
The macro `delegate` offers an easy way to forward methods.
882

883
Let's imagine that users in some application have login information in the `User` model but name and other data in a separate `Profile` model:
884

885
```ruby
886 887 888
class User < ActiveRecord::Base
  has_one :profile
end
889
```
890

891
With that configuration you get a user's name via their profile, `user.profile.name`, but it could be handy to still be able to access such attribute directly:
892

893
```ruby
894 895 896 897 898 899 900
class User < ActiveRecord::Base
  has_one :profile

  def name
    profile.name
  end
end
901
```
902

903
That is what `delegate` does for you:
904

905
```ruby
906 907 908
class User < ActiveRecord::Base
  has_one :profile

909
  delegate :name, to: :profile
910
end
911
```
912

913 914
It is shorter, and the intention more obvious.

915 916
The method must be public in the target.

917
The `delegate` macro accepts several methods:
918

919
```ruby
920
delegate :name, :age, :address, :twitter, to: :profile
921
```
922

923
When interpolated into a string, the `:to` option should become an expression that evaluates to the object the method is delegated to. Typically a string or symbol. Such an expression is evaluated in the context of the receiver:
924

925
```ruby
926
# delegates to the Rails constant
927
delegate :logger, to: :Rails
928 929

# delegates to the receiver's class
930
delegate :table_name, to: :class
931
```
932

933
WARNING: If the `:prefix` option is `true` this is less generic, see below.
934

935
By default, if the delegation raises `NoMethodError` and the target is `nil` the exception is propagated. You can ask that `nil` is returned instead with the `:allow_nil` option:
936

937
```ruby
938
delegate :name, to: :profile, allow_nil: true
939
```
940

941
With `:allow_nil` the call `user.name` returns `nil` if the user has no profile.
942

943
The option `:prefix` adds a prefix to the name of the generated method. This may be handy for example to get a better name:
944

945
```ruby
946
delegate :street, to: :address, prefix: true
947
```
948

949
The previous example generates `address_street` rather than `street`.
950

951
WARNING: Since in this case the name of the generated method is composed of the target object and target method names, the `:to` option must be a method name.
952 953 954

A custom prefix may also be configured:

955
```ruby
956
delegate :size, to: :attachment, prefix: :avatar
957
```
958

959
In the previous example the macro generates `avatar_size` rather than `size`.
960

961
NOTE: Defined in `active_support/core_ext/module/delegation.rb`
962

963
### Redefining Methods
964

965
There are cases where you need to define a method with `define_method`, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.
966

967
The method `redefine_method` prevents such a potential warning, removing the existing method before if needed. Rails uses it in a few places, for instance when it generates an association's API:
968

969
```ruby
970 971 972 973 974 975 976 977 978 979
redefine_method("#{reflection.name}=") do |new_value|
  association = association_instance_get(reflection.name)

  if association.nil? || association.target != new_value
    association = association_proxy_class.new(self, reflection)
  end

  association.replace(new_value)
  association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
980
```
981

982
NOTE: Defined in `active_support/core_ext/module/remove_method.rb`
983

984
Extensions to `Class`
985
---------------------
986

987
### Class Attributes
988

989
#### `class_attribute`
990

991
The method `class_attribute` declares one or more inheritable class attributes that can be overridden at any level down the hierarchy.
992

993
```ruby
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
class A
  class_attribute :x
end

class B < A; end

class C < B; end

A.x = :a
B.x # => :a
C.x # => :a

B.x = :b
A.x # => :a
C.x # => :b

C.x = :c
A.x # => :a
B.x # => :b
1013
```
1014

1015
For example `ActionMailer::Base` defines:
1016

1017
```ruby
1018 1019
class_attribute :default_params
self.default_params = {
1020 1021 1022 1023
  mime_version: "1.0",
  charset: "UTF-8",
  content_type: "text/plain",
  parts_order: [ "text/plain", "text/enriched", "text/html" ]
1024
}.freeze
1025
```
1026

1027
They can be also accessed and overridden at the instance level.
1028

1029
```ruby
1030 1031 1032 1033 1034 1035 1036 1037
A.x = 1

a1 = A.new
a2 = A.new
a2.x = 2

a1.x # => 1, comes from A
a2.x # => 2, overridden in a2
1038
```
1039

1040
The generation of the writer instance method can be prevented by setting the option `:instance_writer` to `false`.
1041

1042
```ruby
V
Vijay Dev 已提交
1043
module ActiveRecord
1044
  class Base
1045
    class_attribute :table_name_prefix, instance_writer: false
1046 1047 1048
    self.table_name_prefix = ""
  end
end
1049
```
1050

1051 1052
A model may find that option useful as a way to prevent mass-assignment from setting the attribute.

1053
The generation of the reader instance method can be prevented by setting the option `:instance_reader` to `false`.
1054

1055
```ruby
1056
class A
1057
  class_attribute :x, instance_reader: false
1058 1059
end

1060
A.new.x = 1 # NoMethodError
1061
```
1062

1063
For convenience `class_attribute` also defines an instance predicate which is the double negation of what the instance reader returns. In the examples above it would be called `x?`.
1064

1065
When `:instance_reader` is `false`, the instance predicate returns a `NoMethodError` just like the reader method.
1066

1067
If you do not want the instance predicate, pass `instance_predicate: false` and it will not be defined.
1068

1069
NOTE: Defined in `active_support/core_ext/class/attribute.rb`
1070

1071
#### `cattr_reader`, `cattr_writer`, and `cattr_accessor`
1072

1073
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:
1074

1075
```ruby
1076 1077 1078 1079 1080
class MysqlAdapter < AbstractAdapter
  # Generates class methods to access @@emulate_booleans.
  cattr_accessor :emulate_booleans
  self.emulate_booleans = true
end
1081
```
1082

1083
Instance methods are created as well for convenience, they are just proxies to the class attribute. So, instances can change the class attribute, but cannot override it as it happens with `class_attribute` (see above). For example given
1084

1085
```ruby
1086
module ActionView
1087
  class Base
1088 1089
    cattr_accessor :field_error_proc
    @@field_error_proc = Proc.new{ ... }
1090 1091
  end
end
1092
```
1093

1094
we can access `field_error_proc` in views.
1095

1096 1097 1098 1099 1100 1101 1102 1103 1104
Also, you can pass a block to `cattr_*` to set up the attribute with a default value:

```ruby
class MysqlAdapter < AbstractAdapter
  # Generates class methods to access @@emulate_booleans with default value of true.
  cattr_accessor(:emulate_booleans) { true }
end
```

1105
The generation of the reader instance method can be prevented by setting `:instance_reader` to `false` and the generation of the writer instance method can be prevented by setting `:instance_writer` to `false`. Generation of both methods can be prevented by setting `:instance_accessor` to `false`. In all cases, the value must be exactly `false` and not any false value.
1106

1107
```ruby
1108 1109 1110
module A
  class B
    # No first_name instance reader is generated.
1111
    cattr_accessor :first_name, instance_reader: false
1112
    # No last_name= instance writer is generated.
1113
    cattr_accessor :last_name, instance_writer: false
1114
    # No surname instance reader or surname= writer is generated.
1115
    cattr_accessor :surname, instance_accessor: false
1116 1117
  end
end
1118
```
1119

1120
A model may find it useful to set `:instance_accessor` to `false` as a way to prevent mass-assignment from setting the attribute.
1121

1122
NOTE: Defined in `active_support/core_ext/module/attribute_accessors.rb`. `active_support/core_ext/class/attribute_accessors.rb` is deprecated and will be removed in Ruby on Rails 4.2.
1123

1124
### Subclasses & Descendants
1125

1126
#### `subclasses`
1127

1128
The `subclasses` method returns the subclasses of the receiver:
1129

1130
```ruby
1131
class C; end
X
Xavier Noria 已提交
1132
C.subclasses # => []
1133

1134
class B < C; end
X
Xavier Noria 已提交
1135
C.subclasses # => [B]
1136

1137
class A < B; end
X
Xavier Noria 已提交
1138
C.subclasses # => [B]
1139

1140
class D < C; end
X
Xavier Noria 已提交
1141
C.subclasses # => [B, D]
1142
```
1143

X
Xavier Noria 已提交
1144
The order in which these classes are returned is unspecified.
1145

1146
NOTE: Defined in `active_support/core_ext/class/subclasses.rb`.
X
Xavier Noria 已提交
1147

1148
#### `descendants`
X
Xavier Noria 已提交
1149

1150
The `descendants` method returns all classes that are `<` than its receiver:
1151

1152
```ruby
1153
class C; end
X
Xavier Noria 已提交
1154
C.descendants # => []
1155 1156

class B < C; end
X
Xavier Noria 已提交
1157
C.descendants # => [B]
1158 1159

class A < B; end
X
Xavier Noria 已提交
1160
C.descendants # => [B, A]
1161 1162

class D < C; end
X
Xavier Noria 已提交
1163
C.descendants # => [B, A, D]
1164
```
1165

X
Xavier Noria 已提交
1166
The order in which these classes are returned is unspecified.
1167

1168
NOTE: Defined in `active_support/core_ext/class/subclasses.rb`.
1169

1170
Extensions to `String`
1171
----------------------
1172

1173
### Output Safety
1174

1175
#### Motivation
1176

1177
Inserting data into HTML templates needs extra care. For example, you can't just interpolate `@review.title` verbatim into an HTML page. For one thing, if the review title is "Flanagan & Matz rules!" the output won't be well-formed because an ampersand has to be escaped as "&amp;amp;". What's more, depending on the application, that may be a big security hole because users can inject malicious HTML setting a hand-crafted review title. Check out the section about cross-site scripting in the [Security guide](security.html#cross-site-scripting-xss) for further information about the risks.
1178

1179
#### Safe Strings
1180

1181
Active Support has the concept of <i>(html) safe</i> strings. A safe string is one that is marked as being insertable into HTML as is. It is trusted, no matter whether it has been escaped or not.
1182 1183 1184

Strings are considered to be <i>unsafe</i> by default:

1185
```ruby
1186
"".html_safe? # => false
1187
```
1188

1189
You can obtain a safe string from a given one with the `html_safe` method:
1190

1191
```ruby
1192 1193
s = "".html_safe
s.html_safe? # => true
1194
```
1195

1196
It is important to understand that `html_safe` performs no escaping whatsoever, it is just an assertion:
1197

1198
```ruby
1199 1200 1201
s = "<script>...</script>".html_safe
s.html_safe? # => true
s            # => "<script>...</script>"
1202
```
1203

1204
It is your responsibility to ensure calling `html_safe` on a particular string is fine.
1205

1206
If you append onto a safe string, either in-place with `concat`/`<<`, or with `+`, the result is a safe string. Unsafe arguments are escaped:
1207

1208
```ruby
1209
"".html_safe + "<" # => "&lt;"
1210
```
1211 1212 1213

Safe arguments are directly appended:

1214
```ruby
1215
"".html_safe + "<".html_safe # => "<"
1216
```
1217

1218
These methods should not be used in ordinary views. Unsafe values are automatically escaped:
1219

1220
```erb
1221
<%= @review.title %> <%# fine, escaped if needed %>
1222
```
1223

1224
To insert something verbatim use the `raw` helper rather than calling `html_safe`:
1225

1226
```erb
1227
<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>
1228
```
X
Xavier Noria 已提交
1229

1230
or, equivalently, use `<%==`:
X
Xavier Noria 已提交
1231

1232
```erb
X
Xavier Noria 已提交
1233
<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
1234
```
1235

1236
The `raw` helper calls `html_safe` for you:
1237

1238
```ruby
1239 1240 1241
def raw(stringish)
  stringish.to_s.html_safe
end
1242
```
1243

1244
NOTE: Defined in `active_support/core_ext/string/output_safety.rb`.
1245

1246
#### Transformation
1247

1248
As a rule of thumb, except perhaps for concatenation as explained above, any method that may change a string gives you an unsafe string. These are `downcase`, `gsub`, `strip`, `chomp`, `underscore`, etc.
1249

1250
In the case of in-place transformations like `gsub!` the receiver itself becomes unsafe.
1251 1252 1253

INFO: The safety bit is lost always, no matter whether the transformation actually changed something.

1254
#### Conversion and Coercion
1255

1256
Calling `to_s` on a safe string returns a safe string, but coercion with `to_str` returns an unsafe string.
1257

1258
#### Copying
1259

1260
Calling `dup` or `clone` on safe strings yields safe strings.
1261

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
### `remove`

The method `remove` will remove all occurrences of the pattern:

```ruby
"Hello World".remove(/Hello /) => "World"
```

There's also the destructive version `String#remove!`.

NOTE: Defined in `active_support/core_ext/string/filters.rb`.
R
Rashmi Yadav 已提交
1273

1274
### `squish`
1275

1276
The method `squish` strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:
1277

1278
```ruby
1279
" \n  foo\n\r \t bar \n".squish # => "foo bar"
1280
```
1281

1282
There's also the destructive version `String#squish!`.
1283

1284 1285
Note that it handles both ASCII and Unicode whitespace like mongolian vowel separator (U+180E).

1286
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
1287

1288
### `truncate`
1289

1290
The method `truncate` returns a copy of its receiver truncated after a given `length`:
1291

1292
```ruby
1293 1294
"Oh dear! Oh dear! I shall be late!".truncate(20)
# => "Oh dear! Oh dear!..."
1295
```
1296

1297
Ellipsis can be customized with the `:omission` option:
1298

1299
```ruby
1300
"Oh dear! Oh dear! I shall be late!".truncate(20, omission: '&hellip;')
1301
# => "Oh dear! Oh &hellip;"
1302
```
1303 1304 1305

Note in particular that truncation takes into account the length of the omission string.

1306
Pass a `:separator` to truncate the string at a natural break:
1307

1308
```ruby
1309
"Oh dear! Oh dear! I shall be late!".truncate(18)
1310
# => "Oh dear! Oh dea..."
1311
"Oh dear! Oh dear! I shall be late!".truncate(18, separator: ' ')
1312
# => "Oh dear! Oh..."
1313
```
1314

1315
The option `:separator` can be a regexp:
A
Alexey Gaziev 已提交
1316

1317
```ruby
1318
"Oh dear! Oh dear! I shall be late!".truncate(18, separator: /\s/)
A
Alexey Gaziev 已提交
1319
# => "Oh dear! Oh..."
1320
```
1321

1322
In above examples "dear" gets cut first, but then `:separator` prevents it.
1323

1324
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
1325

1326
### `inquiry`
1327

1328
The `inquiry` method converts a string into a `StringInquirer` object making equality checks prettier.
1329

1330
```ruby
1331 1332
"production".inquiry.production? # => true
"active".inquiry.inactive?       # => false
1333
```
1334

1335
### `starts_with?` and `ends_with?`
1336

1337
Active Support defines 3rd person aliases of `String#start_with?` and `String#end_with?`:
1338

1339
```ruby
1340 1341
"foo".starts_with?("f") # => true
"foo".ends_with?("o")   # => true
1342
```
1343

1344
NOTE: Defined in `active_support/core_ext/string/starts_ends_with.rb`.
1345

1346
### `strip_heredoc`
X
Xavier Noria 已提交
1347

1348
The method `strip_heredoc` strips indentation in heredocs.
X
Xavier Noria 已提交
1349 1350 1351

For example in

1352
```ruby
X
Xavier Noria 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361
if options[:usage]
  puts <<-USAGE.strip_heredoc
    This command does such and such.

    Supported options are:
      -h         This message
      ...
  USAGE
end
1362
```
X
Xavier Noria 已提交
1363 1364 1365 1366 1367 1368

the user would see the usage message aligned against the left margin.

Technically, it looks for the least indented line in the whole string, and removes
that amount of leading whitespace.

1369
NOTE: Defined in `active_support/core_ext/string/strip.rb`.
X
Xavier Noria 已提交
1370

1371
### `indent`
1372 1373 1374

Indents the lines in the receiver:

1375
```ruby
1376 1377 1378 1379 1380 1381 1382 1383 1384
<<EOS.indent(2)
def some_method
  some_code
end
EOS
# =>
  def some_method
    some_code
  end
1385
```
1386

1387
The second argument, `indent_string`, specifies which indent string to use. The default is `nil`, which tells the method to make an educated guess peeking at the first indented line, and fallback to a space if there is none.
1388

1389
```ruby
1390 1391 1392
"  foo".indent(2)        # => "    foo"
"foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
"foo".indent(2, "\t")    # => "\t\tfoo"
1393
```
1394

V
Vipul A M 已提交
1395
While `indent_string` is typically one space or tab, it may be any string.
1396

1397
The third argument, `indent_empty_lines`, is a flag that says whether empty lines should be indented. Default is false.
1398

1399
```ruby
1400 1401
"foo\n\nbar".indent(2)            # => "  foo\n\n  bar"
"foo\n\nbar".indent(2, nil, true) # => "  foo\n  \n  bar"
1402
```
1403

1404
The `indent!` method performs indentation in-place.
1405

1406 1407
NOTE: Defined in `active_support/core_ext/string/indent.rb`.

1408
### Access
1409

1410
#### `at(position)`
1411

1412
Returns the character of the string at position `position`:
1413

1414
```ruby
1415 1416 1417
"hello".at(0)  # => "h"
"hello".at(4)  # => "o"
"hello".at(-1) # => "o"
1418
"hello".at(10) # => nil
1419
```
1420

1421
NOTE: Defined in `active_support/core_ext/string/access.rb`.
1422

1423
#### `from(position)`
1424

1425
Returns the substring of the string starting at position `position`:
1426

1427
```ruby
1428 1429 1430 1431
"hello".from(0)  # => "hello"
"hello".from(2)  # => "llo"
"hello".from(-2) # => "lo"
"hello".from(10) # => "" if < 1.9, nil in 1.9
1432
```
1433

1434
NOTE: Defined in `active_support/core_ext/string/access.rb`.
1435

1436
#### `to(position)`
1437

1438
Returns the substring of the string up to position `position`:
1439

1440
```ruby
1441 1442 1443 1444
"hello".to(0)  # => "h"
"hello".to(2)  # => "hel"
"hello".to(-2) # => "hell"
"hello".to(10) # => "hello"
1445
```
1446

1447
NOTE: Defined in `active_support/core_ext/string/access.rb`.
1448

1449
#### `first(limit = 1)`
1450

1451
The call `str.first(n)` is equivalent to `str.to(n-1)` if `n` > 0, and returns an empty string for `n` == 0.
1452

1453
NOTE: Defined in `active_support/core_ext/string/access.rb`.
1454

1455
#### `last(limit = 1)`
1456

1457
The call `str.last(n)` is equivalent to `str.from(-n)` if `n` > 0, and returns an empty string for `n` == 0.
1458

1459
NOTE: Defined in `active_support/core_ext/string/access.rb`.
1460

1461
### Inflections
1462

1463
#### `pluralize`
1464

1465
The method `pluralize` returns the plural of its receiver:
1466

1467
```ruby
1468 1469 1470
"table".pluralize     # => "tables"
"ruby".pluralize      # => "rubies"
"equipment".pluralize # => "equipment"
1471
```
1472

1473
As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Built-in rules can be extended in `config/initializers/inflections.rb`. That file is generated by the `rails` command and has instructions in comments.
1474

1475
`pluralize` can also take an optional `count` parameter. If `count == 1` the singular form will be returned. For any other value of `count` the plural form will be returned:
1476

1477
```ruby
1478 1479 1480
"dude".pluralize(0) # => "dudes"
"dude".pluralize(1) # => "dude"
"dude".pluralize(2) # => "dudes"
1481
```
1482

1483 1484
Active Record uses this method to compute the default table name that corresponds to a model:

1485
```ruby
1486
# active_record/model_schema.rb
1487 1488
def undecorated_table_name(class_name = base_class.name)
  table_name = class_name.to_s.demodulize.underscore
1489
  pluralize_table_names ? table_name.pluralize : table_name
1490
end
1491
```
1492

1493
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1494

1495
#### `singularize`
1496

1497
The inverse of `pluralize`:
1498

1499
```ruby
1500 1501 1502
"tables".singularize    # => "table"
"rubies".singularize    # => "ruby"
"equipment".singularize # => "equipment"
1503
```
1504 1505 1506

Associations compute the name of the corresponding default associated class using this method:

1507
```ruby
1508 1509 1510 1511 1512 1513
# active_record/reflection.rb
def derive_class_name
  class_name = name.to_s.camelize
  class_name = class_name.singularize if collection?
  class_name
end
1514
```
1515

1516
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1517

1518
#### `camelize`
1519

1520
The method `camelize` returns its receiver in camel case:
1521

1522
```ruby
1523 1524
"product".camelize    # => "Product"
"admin_user".camelize # => "AdminUser"
1525
```
1526 1527 1528

As a rule of thumb you can think of this method as the one that transforms paths into Ruby class or module names, where slashes separate namespaces:

1529
```ruby
1530
"backoffice/session".camelize # => "Backoffice::Session"
1531
```
1532 1533 1534

For example, Action Pack uses this method to load the class that provides a certain session store:

1535
```ruby
1536 1537
# action_controller/metal/session_management.rb
def session_store=(store)
1538 1539 1540
  @@session_store = store.is_a?(Symbol) ?
    ActionDispatch::Session.const_get(store.to_s.camelize) :
    store
1541
end
1542
```
1543

1544
`camelize` accepts an optional argument, it can be `:upper` (default), or `:lower`. With the latter the first letter becomes lowercase:
1545

1546
```ruby
1547
"visual_effect".camelize(:lower) # => "visualEffect"
1548
```
1549 1550 1551

That may be handy to compute method names in a language that follows that convention, for example JavaScript.

1552
INFO: As a rule of thumb you can think of `camelize` as the inverse of `underscore`, though there are cases where that does not hold: `"SSLError".underscore.camelize` gives back `"SslError"`. To support cases such as this, Active Support allows you to specify acronyms in `config/initializers/inflections.rb`:
1553

1554
```ruby
1555 1556 1557 1558
ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'SSL'
end

A
Alex Johnson 已提交
1559
"SSLError".underscore.camelize # => "SSLError"
1560
```
1561

1562
`camelize` is aliased to `camelcase`.
1563

1564
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1565

1566
#### `underscore`
1567

1568
The method `underscore` goes the other way around, from camel case to paths:
1569

1570
```ruby
1571 1572
"Product".underscore   # => "product"
"AdminUser".underscore # => "admin_user"
1573
```
1574 1575 1576

Also converts "::" back to "/":

1577
```ruby
1578
"Backoffice::Session".underscore # => "backoffice/session"
1579
```
1580 1581 1582

and understands strings that start with lowercase:

1583
```ruby
1584
"visualEffect".underscore # => "visual_effect"
1585
```
1586

1587
`underscore` accepts no argument though.
1588

1589
Rails class and module autoloading uses `underscore` to infer the relative path without extension of a file that would define a given missing constant:
1590

1591
```ruby
1592 1593 1594 1595 1596 1597 1598
# active_support/dependencies.rb
def load_missing_constant(from_mod, const_name)
  ...
  qualified_name = qualified_name_for from_mod, const_name
  path_suffix = qualified_name.underscore
  ...
end
1599
```
1600

1601
INFO: As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold. For example, `"SSLError".underscore.camelize` gives back `"SslError"`.
1602

1603
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1604

1605
#### `titleize`
1606

1607
The method `titleize` capitalizes the words in the receiver:
1608

1609
```ruby
1610 1611
"alice in wonderland".titleize # => "Alice In Wonderland"
"fermat's enigma".titleize     # => "Fermat's Enigma"
1612
```
1613

1614
`titleize` is aliased to `titlecase`.
1615

1616
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1617

1618
#### `dasherize`
1619

1620
The method `dasherize` replaces the underscores in the receiver with dashes:
1621

1622
```ruby
1623 1624
"name".dasherize         # => "name"
"contact_data".dasherize # => "contact-data"
1625
```
1626 1627 1628

The XML serializer of models uses this method to dasherize node names:

1629
```ruby
1630 1631 1632 1633 1634
# active_model/serializers/xml.rb
def reformat_name(name)
  name = name.camelize if camelize?
  dasherize? ? name.dasherize : name
end
1635
```
1636

1637
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1638

1639
#### `demodulize`
1640

1641
Given a string with a qualified constant name, `demodulize` returns the very constant name, that is, the rightmost part of it:
1642

1643
```ruby
1644 1645 1646
"Product".demodulize                        # => "Product"
"Backoffice::UsersController".demodulize    # => "UsersController"
"Admin::Hotel::ReservationUtils".demodulize # => "ReservationUtils"
1647
```
1648 1649 1650

Active Record for example uses this method to compute the name of a counter cache column:

1651
```ruby
1652 1653 1654 1655 1656 1657 1658 1659
# active_record/reflection.rb
def counter_cache_column
  if options[:counter_cache] == true
    "#{active_record.name.demodulize.underscore.pluralize}_count"
  elsif options[:counter_cache]
    options[:counter_cache]
  end
end
1660
```
1661

1662
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1663

1664
#### `deconstantize`
1665

1666
Given a string with a qualified constant reference expression, `deconstantize` removes the rightmost segment, generally leaving the name of the constant's container:
1667

1668
```ruby
1669 1670 1671
"Product".deconstantize                        # => ""
"Backoffice::UsersController".deconstantize    # => "Backoffice"
"Admin::Hotel::ReservationUtils".deconstantize # => "Admin::Hotel"
1672
```
1673

1674
Active Support for example uses this method in `Module#qualified_const_set`:
1675

1676
```ruby
1677 1678 1679 1680 1681 1682 1683 1684
def qualified_const_set(path, value)
  QualifiedConstUtils.raise_if_absolute(path)

  const_name = path.demodulize
  mod_name = path.deconstantize
  mod = mod_name.empty? ? self : qualified_const_get(mod_name)
  mod.const_set(const_name, value)
end
1685
```
1686

1687
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1688

1689
#### `parameterize`
1690

1691
The method `parameterize` normalizes its receiver in a way that can be used in pretty URLs.
1692

1693
```ruby
1694 1695
"John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"
1696
```
1697

1698
In fact, the result string is wrapped in an instance of `ActiveSupport::Multibyte::Chars`.
1699

1700
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1701

1702
#### `tableize`
1703

1704
The method `tableize` is `underscore` followed by `pluralize`.
1705

1706
```ruby
1707 1708
"Person".tableize      # => "people"
"Invoice".tableize     # => "invoices"
1709
"InvoiceLine".tableize # => "invoice_lines"
1710
```
1711

1712
As a rule of thumb, `tableize` returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight `tableize` indeed, because it also demodulizes the class name and checks a few options that may affect the returned string.
1713

1714
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1715

1716
#### `classify`
1717

1718
The method `classify` is the inverse of `tableize`. It gives you the class name corresponding to a table name:
1719

1720
```ruby
1721 1722 1723
"people".classify        # => "Person"
"invoices".classify      # => "Invoice"
"invoice_lines".classify # => "InvoiceLine"
1724
```
1725 1726 1727

The method understands qualified table names:

1728
```ruby
1729
"highrise_production.companies".classify # => "Company"
1730
```
1731

1732
Note that `classify` returns a class name as a string. You can get the actual class object invoking `constantize` on it, explained next.
1733

1734
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1735

1736
#### `constantize`
1737

1738
The method `constantize` resolves the constant reference expression in its receiver:
1739

1740
```ruby
1741 1742 1743 1744 1745 1746
"Fixnum".constantize # => Fixnum

module M
  X = 1
end
"M::X".constantize # => 1
1747
```
1748

1749
If the string evaluates to no known constant, or its content is not even a valid constant name, `constantize` raises `NameError`.
1750

1751
Constant name resolution by `constantize` starts always at the top-level `Object` even if there is no leading "::".
1752

1753
```ruby
1754 1755 1756 1757 1758 1759 1760 1761
X = :in_Object
module M
  X = :in_M

  X                 # => :in_M
  "::X".constantize # => :in_Object
  "X".constantize   # => :in_Object (!)
end
1762
```
1763 1764 1765

So, it is in general not equivalent to what Ruby would do in the same spot, had a real constant be evaluated.

1766
Mailer test cases obtain the mailer being tested from the name of the test class using `constantize`:
1767

1768
```ruby
1769 1770 1771 1772 1773 1774
# action_mailer/test_case.rb
def determine_default_mailer(name)
  name.sub(/Test$/, '').constantize
rescue NameError => e
  raise NonInferrableMailerError.new(name)
end
1775
```
1776

1777
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1778

1779
#### `humanize`
1780

1781
The method `humanize` gives you a sensible name for display out of an attribute name. To do so it replaces underscores with spaces, removes any "_id" suffix, and capitalizes the first word:
1782

1783
```ruby
1784 1785 1786
"name".humanize           # => "Name"
"author_id".humanize      # => "Author"
"comments_count".humanize # => "Comments count"
1787
```
1788

1789 1790 1791 1792 1793 1794
The capitalization of the first word can be turned off by setting the optional parameter `capitalize` to false:

```ruby
"author_id".humanize(capitalize: false) # => "author"
```

1795
The helper method `full_messages` uses `humanize` as a fallback to include attribute names:
1796

1797
```ruby
1798 1799 1800 1801 1802 1803
def full_messages
  full_messages = []

  each do |attribute, messages|
    ...
    attr_name = attribute.to_s.gsub('.', '_').humanize
1804
    attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
1805 1806 1807 1808 1809
    ...
  end

  full_messages
end
1810
```
1811

1812
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1813

1814
#### `foreign_key`
1815

1816
The method `foreign_key` gives a foreign key column name from a class name. To do so it demodulizes, underscores, and adds "_id":
1817

1818
```ruby
1819 1820 1821
"User".foreign_key           # => "user_id"
"InvoiceLine".foreign_key    # => "invoice_line_id"
"Admin::Session".foreign_key # => "session_id"
1822
```
1823 1824 1825

Pass a false argument if you do not want the underscore in "_id":

1826
```ruby
1827
"User".foreign_key(false) # => "userid"
1828
```
1829

1830
Associations use this method to infer foreign keys, for example `has_one` and `has_many` do this:
1831

1832
```ruby
1833 1834
# active_record/associations.rb
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
1835
```
1836

1837
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
1838

1839
### Conversions
1840

1841
#### `to_date`, `to_time`, `to_datetime`
1842

1843
The methods `to_date`, `to_time`, and `to_datetime` are basically convenience wrappers around `Date._parse`:
1844

1845
```ruby
1846 1847
"2010-07-27".to_date              # => Tue, 27 Jul 2010
"2010-07-27 23:37:00".to_time     # => Tue Jul 27 23:37:00 UTC 2010
1848
"2010-07-27 23:37:00".to_datetime # => Tue, 27 Jul 2010 23:37:00 +0000
1849
```
1850

1851
`to_time` receives an optional argument `:utc` or `:local`, to indicate which time zone you want the time in:
1852

1853
```ruby
1854 1855
"2010-07-27 23:42:00".to_time(:utc)   # => Tue Jul 27 23:42:00 UTC 2010
"2010-07-27 23:42:00".to_time(:local) # => Tue Jul 27 23:42:00 +0200 2010
1856
```
1857

1858
Default is `:utc`.
1859

1860
Please refer to the documentation of `Date._parse` for further details.
1861

1862
INFO: The three of them return `nil` for blank receivers.
1863

1864
NOTE: Defined in `active_support/core_ext/string/conversions.rb`.
1865

1866
Extensions to `Numeric`
1867
-----------------------
1868

1869
### Bytes
1870 1871 1872

All numbers respond to these methods:

1873
```ruby
1874 1875 1876 1877 1878 1879 1880
bytes
kilobytes
megabytes
gigabytes
terabytes
petabytes
exabytes
1881
```
1882 1883 1884

They return the corresponding amount of bytes, using a conversion factor of 1024:

1885
```ruby
1886 1887 1888 1889
2.kilobytes   # => 2048
3.megabytes   # => 3145728
3.5.gigabytes # => 3758096384
-4.exabytes   # => -4611686018427387904
1890
```
1891 1892 1893

Singular forms are aliased so you are able to say:

1894
```ruby
X
Xavier Noria 已提交
1895
1.megabyte # => 1048576
1896
```
1897

1898
NOTE: Defined in `active_support/core_ext/numeric/bytes.rb`.
1899

1900
### Time
A
Alexey Gaziev 已提交
1901

1902
Enables the use of time calculations and declarations, like `45.minutes + 2.hours + 4.years`.
A
Alexey Gaziev 已提交
1903 1904 1905 1906

These methods use Time#advance for precise date calculations when using from_now, ago, etc.
as well as adding or subtracting their results from a Time object. For example:

1907
```ruby
1908
# equivalent to Time.current.advance(months: 1)
A
Alexey Gaziev 已提交
1909 1910
1.month.from_now

1911
# equivalent to Time.current.advance(years: 2)
A
Alexey Gaziev 已提交
1912 1913
2.years.from_now

1914
# equivalent to Time.current.advance(months: 4, years: 5)
A
Alexey Gaziev 已提交
1915
(4.months + 5.years).from_now
1916
```
A
Alexey Gaziev 已提交
1917 1918 1919 1920 1921

While these methods provide precise calculation when used as in the examples above, care
should be taken to note that this is not true if the result of `months', `years', etc is
converted before use:

1922
```ruby
A
Alexey Gaziev 已提交
1923 1924 1925 1926 1927
# equivalent to 30.days.to_i.from_now
1.month.to_i.from_now

# equivalent to 365.25.days.to_f.from_now
1.year.to_f.from_now
1928
```
A
Alexey Gaziev 已提交
1929

1930 1931
In such cases, Ruby's core [Date](http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html) and
[Time](http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html) should be used for precision
A
Alexey Gaziev 已提交
1932 1933
date and time arithmetic.

1934
NOTE: Defined in `active_support/core_ext/numeric/time.rb`.
A
Alexey Gaziev 已提交
1935

1936
### Formatting
1937 1938 1939 1940

Enables the formatting of numbers in a variety of ways.

Produce a string representation of a number as a telephone number:
1941

1942
```ruby
V
Vijay Dev 已提交
1943 1944 1945 1946
5551234.to_s(:phone)
# => 555-1234
1235551234.to_s(:phone)
# => 123-555-1234
1947
1235551234.to_s(:phone, area_code: true)
V
Vijay Dev 已提交
1948
# => (123) 555-1234
1949
1235551234.to_s(:phone, delimiter: " ")
V
Vijay Dev 已提交
1950
# => 123 555 1234
1951
1235551234.to_s(:phone, area_code: true, extension: 555)
V
Vijay Dev 已提交
1952
# => (123) 555-1234 x 555
1953
1235551234.to_s(:phone, country_code: 1)
V
Vijay Dev 已提交
1954
# => +1-123-555-1234
1955
```
1956 1957

Produce a string representation of a number as currency:
1958

1959
```ruby
1960 1961
1234567890.50.to_s(:currency)                 # => $1,234,567,890.50
1234567890.506.to_s(:currency)                # => $1,234,567,890.51
1962
1234567890.506.to_s(:currency, precision: 3)  # => $1,234,567,890.506
1963
```
1964 1965

Produce a string representation of a number as a percentage:
1966

1967
```ruby
V
Vijay Dev 已提交
1968 1969
100.to_s(:percentage)
# => 100.000%
1970
100.to_s(:percentage, precision: 0)
V
Vijay Dev 已提交
1971
# => 100%
1972
1000.to_s(:percentage, delimiter: '.', separator: ',')
V
Vijay Dev 已提交
1973
# => 1.000,000%
1974
302.24398923423.to_s(:percentage, precision: 5)
V
Vijay Dev 已提交
1975
# => 302.24399%
1976
```
1977 1978

Produce a string representation of a number in delimited form:
1979

1980
```ruby
1981 1982
12345678.to_s(:delimited)                     # => 12,345,678
12345678.05.to_s(:delimited)                  # => 12,345,678.05
1983 1984 1985
12345678.to_s(:delimited, delimiter: ".")     # => 12.345.678
12345678.to_s(:delimited, delimiter: ",")     # => 12,345,678
12345678.05.to_s(:delimited, separator: " ")  # => 12,345,678 05
1986
```
1987 1988

Produce a string representation of a number rounded to a precision:
1989

1990
```ruby
1991
111.2345.to_s(:rounded)                     # => 111.235
1992 1993 1994 1995
111.2345.to_s(:rounded, precision: 2)       # => 111.23
13.to_s(:rounded, precision: 5)             # => 13.00000
389.32314.to_s(:rounded, precision: 0)      # => 389
111.2345.to_s(:rounded, significant: true)  # => 111
1996
```
1997 1998

Produce a string representation of a number as a human-readable number of bytes:
1999

2000
```ruby
V
Vijay Dev 已提交
2001 2002 2003 2004 2005 2006
123.to_s(:human_size)            # => 123 Bytes
1234.to_s(:human_size)           # => 1.21 KB
12345.to_s(:human_size)          # => 12.1 KB
1234567.to_s(:human_size)        # => 1.18 MB
1234567890.to_s(:human_size)     # => 1.15 GB
1234567890123.to_s(:human_size)  # => 1.12 TB
2007
```
2008 2009

Produce a string representation of a number in human-readable words:
2010

2011
```ruby
V
Vijay Dev 已提交
2012 2013 2014 2015 2016 2017 2018
123.to_s(:human)               # => "123"
1234.to_s(:human)              # => "1.23 Thousand"
12345.to_s(:human)             # => "12.3 Thousand"
1234567.to_s(:human)           # => "1.23 Million"
1234567890.to_s(:human)        # => "1.23 Billion"
1234567890123.to_s(:human)     # => "1.23 Trillion"
1234567890123456.to_s(:human)  # => "1.23 Quadrillion"
2019
```
2020

R
Rashmi Yadav 已提交
2021
NOTE: Defined in `active_support/core_ext/numeric/conversions.rb`.
2022

2023
Extensions to `Integer`
2024
-----------------------
2025

2026
### `multiple_of?`
2027

2028
The method `multiple_of?` tests whether an integer is multiple of the argument:
2029

2030
```ruby
2031 2032
2.multiple_of?(1) # => true
1.multiple_of?(2) # => false
2033
```
2034

2035
NOTE: Defined in `active_support/core_ext/integer/multiple.rb`.
2036

2037
### `ordinal`
2038

2039
The method `ordinal` returns the ordinal suffix string corresponding to the receiver integer:
2040

2041
```ruby
2042 2043 2044 2045 2046 2047
1.ordinal    # => "st"
2.ordinal    # => "nd"
53.ordinal   # => "rd"
2009.ordinal # => "th"
-21.ordinal  # => "st"
-134.ordinal # => "th"
2048
```
2049

2050
NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.
2051

2052
### `ordinalize`
2053

2054
The method `ordinalize` returns the ordinal string corresponding to the receiver integer. In comparison, note that the `ordinal` method returns **only** the suffix string.
2055

2056
```ruby
2057 2058 2059 2060
1.ordinalize    # => "1st"
2.ordinalize    # => "2nd"
53.ordinalize   # => "53rd"
2009.ordinalize # => "2009th"
2061 2062
-21.ordinalize  # => "-21st"
-134.ordinalize # => "-134th"
2063
```
2064

2065
NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.
2066

2067
Extensions to `BigDecimal`
2068
--------------------------
2069
### `to_s`
V
Vijay Dev 已提交
2070

2071 2072 2073 2074 2075 2076 2077
The method `to_s` is aliased to `to_formatted_s`. This provides a convenient way to display a BigDecimal value in floating-point notation:

```ruby
BigDecimal.new(5.00, 6).to_s  # => "5.0"
```

### `to_formatted_s`
V
Vijay Dev 已提交
2078

2079
Te method `to_formatted_s` provides a default specifier of "F".  This means that a simple call to `to_formatted_s` or `to_s` will result in floating point representation instead of engineering notation:
2080 2081 2082 2083 2084 2085

```ruby
BigDecimal.new(5.00, 6).to_formatted_s  # => "5.0"
```

and that symbol specifiers are also supported:
2086

2087 2088 2089 2090 2091 2092 2093 2094 2095
```ruby
BigDecimal.new(5.00, 6).to_formatted_s(:db)  # => "5.0"
```

Engineering notation is still supported:

```ruby
BigDecimal.new(5.00, 6).to_formatted_s("e")  # => "0.5E1"
```
2096

2097
Extensions to `Enumerable`
2098
--------------------------
2099

2100
### `sum`
2101

2102
The method `sum` adds the elements of an enumerable:
2103

2104
```ruby
2105 2106
[1, 2, 3].sum # => 6
(1..100).sum  # => 5050
2107
```
2108

2109
Addition only assumes the elements respond to `+`:
2110

2111
```ruby
2112 2113
[[1, 2], [2, 3], [3, 4]].sum    # => [1, 2, 2, 3, 3, 4]
%w(foo bar baz).sum             # => "foobarbaz"
2114
{a: 1, b: 2, c: 3}.sum # => [:b, 2, :c, 3, :a, 1]
2115
```
2116 2117 2118

The sum of an empty collection is zero by default, but this is customizable:

2119
```ruby
2120 2121
[].sum    # => 0
[].sum(1) # => 1
2122
```
2123

2124
If a block is given, `sum` becomes an iterator that yields the elements of the collection and sums the returned values:
2125

2126
```ruby
2127 2128
(1..5).sum {|n| n * 2 } # => 30
[2, 4, 6, 8, 10].sum    # => 30
2129
```
2130 2131 2132

The sum of an empty receiver can be customized in this form as well:

2133
```ruby
2134
[].sum(1) {|n| n**3} # => 1
2135
```
2136

2137
NOTE: Defined in `active_support/core_ext/enumerable.rb`.
2138

2139
### `index_by`
2140

2141
The method `index_by` generates a hash with the elements of an enumerable indexed by some key.
2142 2143 2144

It iterates through the collection and passes each element to a block. The element will be keyed by the value returned by the block:

2145
```ruby
2146 2147
invoices.index_by(&:number)
# => {'2009-032' => <Invoice ...>, '2009-008' => <Invoice ...>, ...}
2148
```
2149 2150 2151

WARNING. Keys should normally be unique. If the block returns the same value for different elements no collection is built for that key. The last item will win.

2152
NOTE: Defined in `active_support/core_ext/enumerable.rb`.
2153

2154
### `many?`
2155

2156
The method `many?` is shorthand for `collection.size > 1`:
2157

2158
```erb
2159 2160 2161
<% if pages.many? %>
  <%= pagination_links %>
<% end %>
2162
```
2163

2164
If an optional block is given, `many?` only takes into account those elements that return true:
2165

2166
```ruby
2167
@see_more = videos.many? {|video| video.category == params[:category]}
2168
```
2169

2170
NOTE: Defined in `active_support/core_ext/enumerable.rb`.
2171

2172
### `exclude?`
2173

2174
The predicate `exclude?` tests whether a given object does **not** belong to the collection. It is the negation of the built-in `include?`:
2175

2176
```ruby
2177
to_visit << node if visited.exclude?(node)
2178
```
2179

2180
NOTE: Defined in `active_support/core_ext/enumerable.rb`.
2181

2182
Extensions to `Array`
2183
---------------------
2184

2185
### Accessing
2186

2187
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:
2188

2189
```ruby
2190 2191
%w(a b c d).to(2) # => %w(a b c)
[].to(7)          # => []
2192
```
2193

2194
Similarly, `from` returns the tail from the element at the passed index to the end. If the index is greater than the length of the array, it returns an empty array.
2195

2196
```ruby
2197
%w(a b c d).from(2)  # => %w(c d)
2198
%w(a b c d).from(10) # => []
X
Xavier Noria 已提交
2199
[].from(0)           # => []
2200
```
2201

2202
The methods `second`, `third`, `fourth`, and `fifth` return the corresponding element (`first` is built-in). Thanks to social wisdom and positive constructiveness all around, `forty_two` is also available.
2203

2204
```ruby
2205 2206
%w(a b c d).third # => c
%w(a b c d).fifth # => nil
2207
```
2208

2209
NOTE: Defined in `active_support/core_ext/array/access.rb`.
2210

2211
### Adding Elements
2212

2213
#### `prepend`
2214

2215
This method is an alias of `Array#unshift`.
2216

2217
```ruby
2218 2219
%w(a b c d).prepend('e')  # => %w(e a b c d)
[].prepend(10)            # => [10]
2220
```
2221

2222
NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
2223

2224
#### `append`
2225

2226
This method is an alias of `Array#<<`.
2227

2228
```ruby
2229 2230
%w(a b c d).append('e')  # => %w(a b c d e)
[].append([1,2])         # => [[1,2]]
2231
```
2232

2233
NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
2234

2235
### Options Extraction
2236

2237
When the last argument in a method call is a hash, except perhaps for a `&block` argument, Ruby allows you to omit the brackets:
2238

2239
```ruby
2240
User.exists?(email: params[:email])
2241
```
2242 2243 2244

That syntactic sugar is used a lot in Rails to avoid positional arguments where there would be too many, offering instead interfaces that emulate named parameters. In particular it is very idiomatic to use a trailing hash for options.

2245
If a method expects a variable number of arguments and uses `*` in its declaration, however, such an options hash ends up being an item of the array of arguments, where it loses its role.
2246

2247
In those cases, you may give an options hash a distinguished treatment with `extract_options!`. This method checks the type of the last item of an array. If it is a hash it pops it and returns it, otherwise it returns an empty hash.
2248

2249
Let's see for example the definition of the `caches_action` controller macro:
2250

2251
```ruby
2252 2253 2254 2255 2256
def caches_action(*actions)
  return unless cache_configured?
  options = actions.extract_options!
  ...
end
2257
```
2258

2259
This method receives an arbitrary number of action names, and an optional hash of options as last argument. With the call to `extract_options!` you obtain the options hash and remove it from `actions` in a simple and explicit way.
2260

2261
NOTE: Defined in `active_support/core_ext/array/extract_options.rb`.
2262

2263
### Conversions
2264

2265
#### `to_sentence`
2266

2267
The method `to_sentence` turns an array into a string containing a sentence that enumerates its items:
2268

2269
```ruby
2270 2271 2272 2273
%w().to_sentence                # => ""
%w(Earth).to_sentence           # => "Earth"
%w(Earth Wind).to_sentence      # => "Earth and Wind"
%w(Earth Wind Fire).to_sentence # => "Earth, Wind, and Fire"
2274
```
2275 2276 2277

This method accepts three options:

2278 2279 2280
* `:two_words_connector`: What is used for arrays of length 2. Default is " and ".
* `:words_connector`: What is used to join the elements of arrays with 3 or more elements, except for the last two. Default is ", ".
* `:last_word_connector`: What is used to join the last items of an array with 3 or more elements. Default is ", and ".
2281

P
Prathamesh Sonpatki 已提交
2282
The defaults for these options can be localized, their keys are:
2283

2284 2285
| Option                 | I18n key                            |
| ---------------------- | ----------------------------------- |
2286 2287 2288
| `:two_words_connector` | `support.array.two_words_connector` |
| `:words_connector`     | `support.array.words_connector`     |
| `:last_word_connector` | `support.array.last_word_connector` |
2289

2290
NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
2291

2292
#### `to_formatted_s`
2293

2294
The method `to_formatted_s` acts like `to_s` by default.
2295

Y
Yves Senn 已提交
2296 2297 2298
If the array contains items that respond to `id`, however, the symbol
`:db` may be passed as argument. That's typically used with
collections of Active Record objects. Returned strings are:
2299

2300
```ruby
2301 2302 2303
[].to_formatted_s(:db)            # => "null"
[user].to_formatted_s(:db)        # => "8456"
invoice.lines.to_formatted_s(:db) # => "23,567,556,12"
2304
```
2305

2306
Integers in the example above are supposed to come from the respective calls to `id`.
2307

2308
NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
2309

2310
#### `to_xml`
2311

2312
The method `to_xml` returns a string containing an XML representation of its receiver:
2313

2314
```ruby
2315
Contributor.limit(2).order(:rank).to_xml
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <contributors type="array">
#   <contributor>
#     <id type="integer">4356</id>
#     <name>Jeremy Kemper</name>
#     <rank type="integer">1</rank>
#     <url-id>jeremy-kemper</url-id>
#   </contributor>
#   <contributor>
#     <id type="integer">4404</id>
#     <name>David Heinemeier Hansson</name>
#     <rank type="integer">2</rank>
#     <url-id>david-heinemeier-hansson</url-id>
#   </contributor>
# </contributors>
2332
```
2333

2334
To do so it sends `to_xml` to every item in turn, and collects the results under a root node. All items must respond to `to_xml`, an exception is raised otherwise.
2335

2336
By default, the name of the root element is the underscorized and dasherized plural of the name of the class of the first item, provided the rest of elements belong to that type (checked with `is_a?`) and they are not hashes. In the example above that's "contributors".
2337

A
Alexey Gaziev 已提交
2338
If there's any element that does not belong to the type of the first one the root node becomes "objects":
2339

2340
```ruby
2341 2342 2343
[Contributor.first, Commit.first].to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
A
Alexey Gaziev 已提交
2344 2345
# <objects type="array">
#   <object>
2346 2347 2348 2349
#     <id type="integer">4583</id>
#     <name>Aaron Batalion</name>
#     <rank type="integer">53</rank>
#     <url-id>aaron-batalion</url-id>
A
Alexey Gaziev 已提交
2350 2351
#   </object>
#   <object>
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
#     <author>Joshua Peek</author>
#     <authored-timestamp type="datetime">2009-09-02T16:44:36Z</authored-timestamp>
#     <branch>origin/master</branch>
#     <committed-timestamp type="datetime">2009-09-02T16:44:36Z</committed-timestamp>
#     <committer>Joshua Peek</committer>
#     <git-show nil="true"></git-show>
#     <id type="integer">190316</id>
#     <imported-from-svn type="boolean">false</imported-from-svn>
#     <message>Kill AMo observing wrap_with_notifications since ARes was only using it</message>
#     <sha1>723a47bfb3708f968821bc969a9a3fc873a3ed58</sha1>
A
Alexey Gaziev 已提交
2362 2363
#   </object>
# </objects>
2364
```
2365

A
Alexey Gaziev 已提交
2366
If the receiver is an array of hashes the root element is by default also "objects":
2367

2368
```ruby
2369
[{a: 1, b: 2}, {c: 3}].to_xml
2370 2371
# =>
# <?xml version="1.0" encoding="UTF-8"?>
A
Alexey Gaziev 已提交
2372 2373
# <objects type="array">
#   <object>
2374 2375
#     <b type="integer">2</b>
#     <a type="integer">1</a>
A
Alexey Gaziev 已提交
2376 2377
#   </object>
#   <object>
2378
#     <c type="integer">3</c>
A
Alexey Gaziev 已提交
2379 2380
#   </object>
# </objects>
2381
```
2382

2383
WARNING. If the collection is empty the root element is by default "nil-classes". That's a gotcha, for example the root element of the list of contributors above would not be "contributors" if the collection was empty, but "nil-classes". You may use the `:root` option to ensure a consistent root element.
2384

2385
The name of children nodes is by default the name of the root node singularized. In the examples above we've seen "contributor" and "object". The option `:children` allows you to set these node names.
2386

2387
The default XML builder is a fresh instance of `Builder::XmlMarkup`. You can configure your own builder via the `:builder` option. The method also accepts options like `:dasherize` and friends, they are forwarded to the builder:
2388

2389
```ruby
2390
Contributor.limit(2).order(:rank).to_xml(skip_types: true)
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <contributors>
#   <contributor>
#     <id>4356</id>
#     <name>Jeremy Kemper</name>
#     <rank>1</rank>
#     <url-id>jeremy-kemper</url-id>
#   </contributor>
#   <contributor>
#     <id>4404</id>
#     <name>David Heinemeier Hansson</name>
#     <rank>2</rank>
#     <url-id>david-heinemeier-hansson</url-id>
#   </contributor>
# </contributors>
2407
```
2408

2409
NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
2410

2411
### Wrapping
2412

2413
The method `Array.wrap` wraps its argument in an array unless it is already an array (or array-like).
2414 2415 2416

Specifically:

2417 2418
* If the argument is `nil` an empty list is returned.
* Otherwise, if the argument responds to `to_ary` it is invoked, and if the value of `to_ary` is not `nil`, it is returned.
2419
* Otherwise, an array with the argument as its single element is returned.
2420

2421
```ruby
2422 2423 2424
Array.wrap(nil)       # => []
Array.wrap([1, 2, 3]) # => [1, 2, 3]
Array.wrap(0)         # => [0]
2425
```
2426

2427
This method is similar in purpose to `Kernel#Array`, but there are some differences:
2428

2429 2430 2431
* If the argument responds to `to_ary` the method is invoked. `Kernel#Array` moves on to try `to_a` if the returned value is `nil`, but `Array.wrap` returns `nil` right away.
* If the returned value from `to_ary` is neither `nil` nor an `Array` object, `Kernel#Array` raises an exception, while `Array.wrap` does not, it just returns the value.
* It does not call `to_a` on the argument, though special-cases `nil` to return an empty array.
2432

2433
The last point is particularly worth comparing for some enumerables:
2434

2435
```ruby
2436
Array.wrap(foo: :bar) # => [{:foo=>:bar}]
2437
Array(foo: :bar)      # => [[:foo, :bar]]
2438
```
2439

2440 2441
There's also a related idiom that uses the splat operator:

2442
```ruby
2443
[*object]
2444
```
2445

2446
which in Ruby 1.8 returns `[nil]` for `nil`, and calls to `Array(object)` otherwise. (Please if you know the exact behavior in 1.9 contact fxn.)
2447

2448
Thus, in this case the behavior is different for `nil`, and the differences with `Kernel#Array` explained above apply to the rest of `object`s.
2449

2450
NOTE: Defined in `active_support/core_ext/array/wrap.rb`.
2451

2452
### Duplicating
A
Alexey Gaziev 已提交
2453

Y
Yves Senn 已提交
2454 2455
The method `Array.deep_dup` duplicates itself and all objects inside
recursively with Active Support method `Object#deep_dup`. It works like `Array#map` with sending `deep_dup` method to each object inside.
A
Alexey Gaziev 已提交
2456

2457
```ruby
A
Alexey Gaziev 已提交
2458 2459 2460 2461
array = [1, [2, 3]]
dup = array.deep_dup
dup[1][2] = 4
array[1][2] == nil   # => true
2462
```
A
Alexey Gaziev 已提交
2463

R
Rashmi Yadav 已提交
2464
NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
A
Alexey Gaziev 已提交
2465

2466
### Grouping
2467

2468
#### `in_groups_of(number, fill_with = nil)`
2469

2470
The method `in_groups_of` splits an array into consecutive groups of a certain size. It returns an array with the groups:
2471

2472
```ruby
2473
[1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
2474
```
2475 2476 2477

or yields them in turn if a block is passed:

2478
```html+erb
2479 2480
<% sample.in_groups_of(3) do |a, b, c| %>
  <tr>
2481 2482 2483
    <td><%= a %></td>
    <td><%= b %></td>
    <td><%= c %></td>
2484 2485
  </tr>
<% end %>
2486
```
2487

2488
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:
2489

2490
```ruby
2491
[1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
2492
```
2493

2494
And you can tell the method not to fill the last group passing `false`:
2495

2496
```ruby
2497
[1, 2, 3].in_groups_of(2, false) # => [[1, 2], [3]]
2498
```
2499

2500
As a consequence `false` can't be a used as a padding value.
2501

2502
NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
2503

2504
#### `in_groups(number, fill_with = nil)`
2505

2506
The method `in_groups` splits an array into a certain number of groups. The method returns an array with the groups:
2507

2508
```ruby
2509 2510
%w(1 2 3 4 5 6 7).in_groups(3)
# => [["1", "2", "3"], ["4", "5", nil], ["6", "7", nil]]
2511
```
2512 2513 2514

or yields them in turn if a block is passed:

2515
```ruby
2516 2517 2518 2519
%w(1 2 3 4 5 6 7).in_groups(3) {|group| p group}
["1", "2", "3"]
["4", "5", nil]
["6", "7", nil]
2520
```
2521

2522
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.
2523 2524 2525

You can change this padding value using the second optional argument:

2526
```ruby
2527 2528
%w(1 2 3 4 5 6 7).in_groups(3, "0")
# => [["1", "2", "3"], ["4", "5", "0"], ["6", "7", "0"]]
2529
```
2530

2531
And you can tell the method not to fill the smaller groups passing `false`:
2532

2533
```ruby
2534 2535
%w(1 2 3 4 5 6 7).in_groups(3, false)
# => [["1", "2", "3"], ["4", "5"], ["6", "7"]]
2536
```
2537

2538
As a consequence `false` can't be a used as a padding value.
2539

2540
NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
2541

2542
#### `split(value = nil)`
2543

2544
The method `split` divides an array by a separator and returns the resulting chunks.
2545 2546 2547

If a block is passed the separators are those elements of the array for which the block returns true:

2548
```ruby
2549 2550
(-5..5).to_a.split { |i| i.multiple_of?(4) }
# => [[-5], [-3, -2, -1], [1, 2, 3], [5]]
2551
```
2552

2553
Otherwise, the value received as argument, which defaults to `nil`, is the separator:
2554

2555
```ruby
2556 2557
[0, 1, -5, 1, 1, "foo", "bar"].split(1)
# => [[0], [-5], [], ["foo", "bar"]]
2558
```
2559

2560 2561
TIP: Observe in the previous example that consecutive separators result in empty arrays.

2562
NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
2563

2564
Extensions to `Hash`
2565
--------------------
2566

2567
### Conversions
2568

2569
#### `to_xml`
2570

2571
The method `to_xml` returns a string containing an XML representation of its receiver:
2572

2573
```ruby
2574 2575 2576 2577 2578 2579 2580
{"foo" => 1, "bar" => 2}.to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <hash>
#   <foo type="integer">1</foo>
#   <bar type="integer">2</bar>
# </hash>
2581
```
2582

2583
To do so, the method loops over the pairs and builds nodes that depend on the _values_. Given a pair `key`, `value`:
2584

2585
* If `value` is a hash there's a recursive call with `key` as `:root`.
2586

2587
* If `value` is an array there's a recursive call with `key` as `:root`, and `key` singularized as `:children`.
2588

2589
* If `value` is a callable object it must expect one or two arguments. Depending on the arity, the callable is invoked with the `options` hash as first argument with `key` as `:root`, and `key` singularized as second argument. Its return value becomes a new node.
2590

2591
* If `value` responds to `to_xml` the method is invoked with `key` as `:root`.
2592

2593
* Otherwise, a node with `key` as tag is created with a string representation of `value` as text node. If `value` is `nil` an attribute "nil" set to "true" is added. Unless the option `:skip_types` exists and is true, an attribute "type" is added as well according to the following mapping:
2594

2595
```ruby
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
XML_TYPE_NAMES = {
  "Symbol"     => "symbol",
  "Fixnum"     => "integer",
  "Bignum"     => "integer",
  "BigDecimal" => "decimal",
  "Float"      => "float",
  "TrueClass"  => "boolean",
  "FalseClass" => "boolean",
  "Date"       => "date",
  "DateTime"   => "datetime",
  "Time"       => "datetime"
}
2608
```
2609

2610
By default the root node is "hash", but that's configurable via the `:root` option.
2611

2612
The default XML builder is a fresh instance of `Builder::XmlMarkup`. You can configure your own builder with the `:builder` option. The method also accepts options like `:dasherize` and friends, they are forwarded to the builder.
2613

2614
NOTE: Defined in `active_support/core_ext/hash/conversions.rb`.
2615

2616
### Merging
2617

2618
Ruby has a built-in method `Hash#merge` that merges two hashes:
2619

2620
```ruby
2621
{a: 1, b: 1}.merge(a: 0, c: 2)
2622
# => {:a=>0, :b=>1, :c=>2}
2623
```
2624 2625 2626

Active Support defines a few more ways of merging hashes that may be convenient.

2627
#### `reverse_merge` and `reverse_merge!`
2628

2629
In case of collision the key in the hash of the argument wins in `merge`. You can support option hashes with default values in a compact way with this idiom:
2630

2631
```ruby
2632
options = {length: 30, omission: "..."}.merge(options)
2633
```
2634

2635
Active Support defines `reverse_merge` in case you prefer this alternative notation:
2636

2637
```ruby
2638
options = options.reverse_merge(length: 30, omission: "...")
2639
```
2640

2641
And a bang version `reverse_merge!` that performs the merge in place:
2642

2643
```ruby
2644
options.reverse_merge!(length: 30, omission: "...")
2645
```
2646

2647
WARNING. Take into account that `reverse_merge!` may change the hash in the caller, which may or may not be a good idea.
2648

2649
NOTE: Defined in `active_support/core_ext/hash/reverse_merge.rb`.
2650

2651
#### `reverse_update`
2652

2653
The method `reverse_update` is an alias for `reverse_merge!`, explained above.
2654

2655
WARNING. Note that `reverse_update` has no bang.
2656

2657
NOTE: Defined in `active_support/core_ext/hash/reverse_merge.rb`.
2658

2659
#### `deep_merge` and `deep_merge!`
2660 2661 2662

As you can see in the previous example if a key is found in both hashes the value in the one in the argument wins.

2663
Active Support defines `Hash#deep_merge`. In a deep merge, if a key is found in both hashes and their values are hashes in turn, then their _merge_ becomes the value in the resulting hash:
2664

2665
```ruby
2666
{a: {b: 1}}.deep_merge(a: {c: 2})
2667
# => {:a=>{:b=>1, :c=>2}}
2668
```
2669

2670
The method `deep_merge!` performs a deep merge in place.
2671

2672
NOTE: Defined in `active_support/core_ext/hash/deep_merge.rb`.
2673

2674
### Deep duplicating
A
Alexey Gaziev 已提交
2675

Y
Yves Senn 已提交
2676 2677
The method `Hash.deep_dup` duplicates itself and all keys and values
inside recursively with Active Support method `Object#deep_dup`. It works like `Enumerator#each_with_object` with sending `deep_dup` method to each pair inside.
A
Alexey Gaziev 已提交
2678

2679
```ruby
2680
hash = { a: 1, b: { c: 2, d: [3, 4] } }
A
Alexey Gaziev 已提交
2681 2682 2683 2684 2685 2686 2687

dup = hash.deep_dup
dup[:b][:e] = 5
dup[:b][:d] << 5

hash[:b][:e] == nil      # => true
hash[:b][:d] == [3, 4]   # => true
2688
```
A
Alexey Gaziev 已提交
2689

R
Rashmi Yadav 已提交
2690
NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
2691

2692
### Working with Keys
2693

2694
#### `except` and `except!`
2695

2696
The method `except` returns a hash with the keys in the argument list removed, if present:
2697

2698
```ruby
2699
{a: 1, b: 2}.except(:a) # => {:b=>2}
2700
```
2701

2702
If the receiver responds to `convert_key`, the method is called on each of the arguments. This allows `except` to play nice with hashes with indifferent access for instance:
2703

2704
```ruby
2705 2706
{a: 1}.with_indifferent_access.except(:a)  # => {}
{a: 1}.with_indifferent_access.except("a") # => {}
2707
```
2708

2709
There's also the bang variant `except!` that removes keys in the very receiver.
2710

2711
NOTE: Defined in `active_support/core_ext/hash/except.rb`.
2712

2713
#### `transform_keys` and `transform_keys!`
2714

2715
The method `transform_keys` accepts a block and returns a hash that has applied the block operations to each of the keys in the receiver:
2716

2717
```ruby
2718
{nil => nil, 1 => 1, a: :a}.transform_keys { |key| key.to_s.upcase }
2719
# => {"" => nil, "A" => :a, "1" => 1}
2720
```
2721

2722
In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash:
2723

2724
```ruby
2725
{"a" => 1, a: 2}.transform_keys { |key| key.to_s.upcase }
2726 2727 2728 2729
# The result could either be
# => {"A"=>2}
# or
# => {"A"=>1}
2730
```
2731

2732
This method may be useful for example to build specialized conversions. For instance `stringify_keys` and `symbolize_keys` use `transform_keys` to perform their key conversions:
2733

2734
```ruby
2735
def stringify_keys
2736
  transform_keys { |key| key.to_s }
2737 2738 2739
end
...
def symbolize_keys
2740
  transform_keys { |key| key.to_sym rescue key }
2741
end
2742
```
2743

2744
There's also the bang variant `transform_keys!` that applies the block operations to keys in the very receiver.
2745

2746
Besides that, one can use `deep_transform_keys` and `deep_transform_keys!` to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
2747

2748
```ruby
2749
{nil => nil, 1 => 1, nested: {a: 3, 5 => 5}}.deep_transform_keys { |key| key.to_s.upcase }
2750
# => {""=>nil, "1"=>1, "NESTED"=>{"A"=>3, "5"=>5}}
2751
```
2752

2753
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2754

2755
#### `stringify_keys` and `stringify_keys!`
2756

2757
The method `stringify_keys` returns a hash that has a stringified version of the keys in the receiver. It does so by sending `to_s` to them:
2758

2759
```ruby
2760
{nil => nil, 1 => 1, a: :a}.stringify_keys
2761
# => {"" => nil, "a" => :a, "1" => 1}
2762
```
2763

2764
In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash:
2765

2766
```ruby
2767
{"a" => 1, a: 2}.stringify_keys
2768 2769 2770 2771
# The result could either be
# => {"a"=>2}
# or
# => {"a"=>1}
2772
```
2773

2774
This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionView::Helpers::FormHelper` defines:
2775

2776
```ruby
2777 2778 2779 2780 2781
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
  options = options.stringify_keys
  options["type"] = "checkbox"
  ...
end
2782
```
2783

2784
The second line can safely access the "type" key, and let the user to pass either `:type` or "type".
2785

2786
There's also the bang variant `stringify_keys!` that stringifies keys in the very receiver.
2787

2788
Besides that, one can use `deep_stringify_keys` and `deep_stringify_keys!` to stringify all the keys in the given hash and all the hashes nested into it. An example of the result is:
2789

2790
```ruby
2791
{nil => nil, 1 => 1, nested: {a: 3, 5 => 5}}.deep_stringify_keys
2792
# => {""=>nil, "1"=>1, "nested"=>{"a"=>3, "5"=>5}}
2793
```
2794

2795
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2796

2797
#### `symbolize_keys` and `symbolize_keys!`
2798

2799
The method `symbolize_keys` returns a hash that has a symbolized version of the keys in the receiver, where possible. It does so by sending `to_sym` to them:
2800

2801
```ruby
2802
{nil => nil, 1 => 1, "a" => "a"}.symbolize_keys
2803
# => {1=>1, nil=>nil, :a=>"a"}
2804
```
2805 2806 2807

WARNING. Note in the previous example only one key was symbolized.

2808
In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash:
2809

2810
```ruby
2811
{"a" => 1, a: 2}.symbolize_keys
2812 2813 2814 2815
# The result could either be
# => {:a=>2}
# or
# => {:a=>1}
2816
```
2817

2818
This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionController::UrlRewriter` defines
2819

2820
```ruby
2821 2822 2823 2824 2825
def rewrite_path(options)
  options = options.symbolize_keys
  options.update(options[:params].symbolize_keys) if options[:params]
  ...
end
2826
```
2827

2828
The second line can safely access the `:params` key, and let the user to pass either `:params` or "params".
2829

2830
There's also the bang variant `symbolize_keys!` that symbolizes keys in the very receiver.
2831

2832
Besides that, one can use `deep_symbolize_keys` and `deep_symbolize_keys!` to symbolize all the keys in the given hash and all the hashes nested into it. An example of the result is:
2833

2834
```ruby
2835
{nil => nil, 1 => 1, "nested" => {"a" => 3, 5 => 5}}.deep_symbolize_keys
2836
# => {nil=>nil, 1=>1, nested:{a:3, 5=>5}}
2837
```
2838

2839
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2840

2841
#### `to_options` and `to_options!`
2842

2843
The methods `to_options` and `to_options!` are respectively aliases of `symbolize_keys` and `symbolize_keys!`.
2844

2845
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2846

2847
#### `assert_valid_keys`
2848

2849
The method `assert_valid_keys` receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does `ArgumentError` is raised.
2850

2851
```ruby
2852 2853
{a: 1}.assert_valid_keys(:a)  # passes
{a: 1}.assert_valid_keys("a") # ArgumentError
2854
```
2855

2856
Active Record does not accept unknown options when building associations, for example. It implements that control via `assert_valid_keys`.
2857

2858
NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
2859

2860
### Slicing
2861

2862
Ruby has built-in support for taking slices out of strings and arrays. Active Support extends slicing to hashes:
2863

2864
```ruby
2865
{a: 1, b: 2, c: 3}.slice(:a, :c)
2866
# => {:c=>3, :a=>1}
2867

2868
{a: 1, b: 2, c: 3}.slice(:b, :X)
2869
# => {:b=>2} # non-existing keys are ignored
2870
```
2871

2872
If the receiver responds to `convert_key` keys are normalized:
2873

2874
```ruby
2875
{a: 1, b: 2}.with_indifferent_access.slice("a")
2876
# => {:a=>1}
2877
```
2878 2879 2880

NOTE. Slicing may come in handy for sanitizing option hashes with a white list of keys.

2881
There's also `slice!` which in addition to perform a slice in place returns what's removed:
2882

2883
```ruby
2884
hash = {a: 1, b: 2}
2885 2886
rest = hash.slice!(:a) # => {:b=>2}
hash                   # => {:a=>1}
2887
```
2888

2889
NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
2890

2891
### Extracting
S
Sebastian Martinez 已提交
2892

2893
The method `extract!` removes and returns the key/value pairs matching the given keys.
S
Sebastian Martinez 已提交
2894

2895
```ruby
2896
hash = {a: 1, b: 2}
2897 2898
rest = hash.extract!(:a) # => {:a=>1}
hash                     # => {:b=>2}
2899 2900 2901 2902 2903
```

The method `extract!` returns the same subclass of Hash, that the receiver is.

```ruby
2904
hash = {a: 1, b: 2}.with_indifferent_access
2905 2906
rest = hash.extract!(:a).class
# => ActiveSupport::HashWithIndifferentAccess
2907
```
S
Sebastian Martinez 已提交
2908

2909
NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
S
Sebastian Martinez 已提交
2910

2911
### Indifferent Access
2912

2913
The method `with_indifferent_access` returns an `ActiveSupport::HashWithIndifferentAccess` out of its receiver:
2914

2915
```ruby
2916
{a: 1}.with_indifferent_access["a"] # => 1
2917
```
2918

2919
NOTE: Defined in `active_support/core_ext/hash/indifferent_access.rb`.
2920

2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
### Compacting

The methods `compact` and `compact!` return a Hash without items with `nil` value. 

```ruby
{a: 1, b: 2, c: nil}.compact # => {a: 1, b: 2}
```

NOTE: Defined in `active_support/core_ext/hash/compact.rb`.

2931
Extensions to `Regexp`
2932
----------------------
2933

2934
### `multiline?`
2935

2936
The method `multiline?` says whether a regexp has the `/m` flag set, that is, whether the dot matches newlines.
2937

2938
```ruby
2939 2940 2941 2942 2943
%r{.}.multiline?  # => false
%r{.}m.multiline? # => true

Regexp.new('.').multiline?                    # => false
Regexp.new('.', Regexp::MULTILINE).multiline? # => true
2944
```
2945 2946 2947

Rails uses this method in a single place, also in the routing code. Multiline regexps are disallowed for route requirements and this flag eases enforcing that constraint.

2948
```ruby
2949 2950 2951 2952 2953 2954 2955
def assign_route_options(segments, defaults, requirements)
  ...
  if requirement.multiline?
    raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
  end
  ...
end
2956
```
2957

2958
NOTE: Defined in `active_support/core_ext/regexp.rb`.
2959

2960
Extensions to `Range`
2961
---------------------
2962

2963
### `to_s`
2964

2965
Active Support extends the method `Range#to_s` so that it understands an optional format argument. As of this writing the only supported non-default format is `:db`:
2966

2967
```ruby
2968 2969 2970 2971 2972
(Date.today..Date.tomorrow).to_s
# => "2009-10-25..2009-10-26"

(Date.today..Date.tomorrow).to_s(:db)
# => "BETWEEN '2009-10-25' AND '2009-10-26'"
2973
```
2974

2975
As the example depicts, the `:db` format generates a `BETWEEN` SQL clause. That is used by Active Record in its support for range values in conditions.
2976

2977
NOTE: Defined in `active_support/core_ext/range/conversions.rb`.
2978

2979
### `include?`
2980

2981
The methods `Range#include?` and `Range#===` say whether some value falls between the ends of a given instance:
2982

2983
```ruby
2984
(2..3).include?(Math::E) # => true
2985
```
2986

A
Alexey Gaziev 已提交
2987
Active Support extends these methods so that the argument may be another range in turn. In that case we test whether the ends of the argument range belong to the receiver themselves:
2988

2989
```ruby
2990 2991 2992 2993 2994
(1..10).include?(3..7)  # => true
(1..10).include?(0..7)  # => false
(1..10).include?(3..11) # => false
(1...9).include?(3..9)  # => false

A
Alexey Gaziev 已提交
2995 2996 2997 2998
(1..10) === (3..7)  # => true
(1..10) === (0..7)  # => false
(1..10) === (3..11) # => false
(1...9) === (3..9)  # => false
2999
```
3000

3001
NOTE: Defined in `active_support/core_ext/range/include_range.rb`.
3002

3003
### `overlaps?`
3004

3005
The method `Range#overlaps?` says whether any two given ranges have non-void intersection:
3006

3007
```ruby
3008 3009 3010
(1..10).overlaps?(7..11)  # => true
(1..10).overlaps?(0..7)   # => true
(1..10).overlaps?(11..27) # => false
3011
```
3012

3013
NOTE: Defined in `active_support/core_ext/range/overlaps.rb`.
3014

3015
Extensions to `Proc`
3016
--------------------
3017

3018
### `bind`
X
Xavier Noria 已提交
3019

3020
As you surely know Ruby has an `UnboundMethod` class whose instances are methods that belong to the limbo of methods without a self. The method `Module#instance_method` returns an unbound method for example:
X
Xavier Noria 已提交
3021

3022
```ruby
X
Xavier Noria 已提交
3023
Hash.instance_method(:delete) # => #<UnboundMethod: Hash#delete>
3024
```
X
Xavier Noria 已提交
3025

3026
An unbound method is not callable as is, you need to bind it first to an object with `bind`:
X
Xavier Noria 已提交
3027

3028
```ruby
X
Xavier Noria 已提交
3029
clear = Hash.instance_method(:clear)
3030
clear.bind({a: 1}).call # => {}
3031
```
X
Xavier Noria 已提交
3032

3033
Active Support defines `Proc#bind` with an analogous purpose:
X
Xavier Noria 已提交
3034

3035
```ruby
X
Xavier Noria 已提交
3036
Proc.new { size }.bind([]).call # => 0
3037
```
X
Xavier Noria 已提交
3038

3039
As you see that's callable and bound to the argument, the return value is indeed a `Method`.
X
Xavier Noria 已提交
3040

3041
NOTE: To do so `Proc#bind` actually creates a method under the hood. If you ever see a method with a weird name like `__bind_1256598120_237302` in a stack trace you know now where it comes from.
X
Xavier Noria 已提交
3042

3043
Action Pack uses this trick in `rescue_from` for example, which accepts the name of a method and also a proc as callbacks for a given rescued exception. It has to call them in either case, so a bound method is returned by `handler_for_rescue`, thus simplifying the code in the caller:
X
Xavier Noria 已提交
3044

3045
```ruby
X
Xavier Noria 已提交
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
def handler_for_rescue(exception)
  _, rescuer = Array(rescue_handlers).reverse.detect do |klass_name, handler|
    ...
  end

  case rescuer
  when Symbol
    method(rescuer)
  when Proc
    rescuer.bind(self)
  end
end
3058
```
3059

3060
NOTE: Defined in `active_support/core_ext/proc.rb`.
3061

3062
Extensions to `Date`
3063
--------------------
3064

3065
### Calculations
3066

3067
NOTE: All the following methods are defined in `active_support/core_ext/date/calculations.rb`.
3068

3069
INFO: The following calculation methods have edge cases in October 1582, since days 5..14 just do not exist. This guide does not document their behavior around those days for brevity, but it is enough to say that they do what you would expect. That is, `Date.new(1582, 10, 4).tomorrow` returns `Date.new(1582, 10, 15)` and so on. Please check `test/core_ext/date_ext_test.rb` in the Active Support test suite for expected behavior.
3070

3071
#### `Date.current`
3072

3073
Active Support defines `Date.current` to be today in the current time zone. That's like `Date.today`, except that it honors the user time zone, if defined. It also defines `Date.yesterday` and `Date.tomorrow`, and the instance predicates `past?`, `today?`, and `future?`, all of them relative to `Date.current`.
3074

3075
When making Date comparisons using methods which honor the user time zone, make sure to use `Date.current` and not `Date.today`. There are cases where the user time zone might be in the future compared to the system time zone, which `Date.today` uses by default. This means `Date.today` may equal `Date.yesterday`.
3076

3077
#### Named dates
3078

3079
##### `prev_year`, `next_year`
3080

3081
In Ruby 1.9 `prev_year` and `next_year` return a date with the same day/month in the last or next year:
3082

3083
```ruby
3084
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
3085
d.prev_year              # => Fri, 08 May 2009
3086
d.next_year              # => Sun, 08 May 2011
3087
```
3088 3089 3090

If date is the 29th of February of a leap year, you obtain the 28th:

3091
```ruby
3092
d = Date.new(2000, 2, 29) # => Tue, 29 Feb 2000
3093
d.prev_year               # => Sun, 28 Feb 1999
3094
d.next_year               # => Wed, 28 Feb 2001
3095
```
3096

3097
`prev_year` is aliased to `last_year`.
3098

3099
##### `prev_month`, `next_month`
3100

3101
In Ruby 1.9 `prev_month` and `next_month` return the date with the same day in the last or next month:
3102

3103
```ruby
3104
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
3105
d.prev_month             # => Thu, 08 Apr 2010
3106
d.next_month             # => Tue, 08 Jun 2010
3107
```
3108 3109 3110

If such a day does not exist, the last day of the corresponding month is returned:

3111
```ruby
3112 3113
Date.new(2000, 5, 31).prev_month # => Sun, 30 Apr 2000
Date.new(2000, 3, 31).prev_month # => Tue, 29 Feb 2000
3114 3115
Date.new(2000, 5, 31).next_month # => Fri, 30 Jun 2000
Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
3116
```
3117

3118
`prev_month` is aliased to `last_month`.
3119

3120
##### `prev_quarter`, `next_quarter`
3121

3122
Same as `prev_month` and `next_month`. It returns the date with the same day in the previous or next quarter:
3123

3124
```ruby
3125 3126 3127
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
t.prev_quarter             # => Mon, 08 Feb 2010
t.next_quarter             # => Sun, 08 Aug 2010
3128
```
3129 3130 3131

If such a day does not exist, the last day of the corresponding month is returned:

3132
```ruby
3133 3134 3135 3136
Time.local(2000, 7, 31).prev_quarter  # => Sun, 30 Apr 2000
Time.local(2000, 5, 31).prev_quarter  # => Tue, 29 Feb 2000
Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000
Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001
3137
```
3138

3139
`prev_quarter` is aliased to `last_quarter`.
3140

3141
##### `beginning_of_week`, `end_of_week`
3142

3143
The methods `beginning_of_week` and `end_of_week` return the dates for the
3144
beginning and end of the week, respectively. Weeks are assumed to start on
3145 3146
Monday, but that can be changed passing an argument, setting thread local
`Date.beginning_of_week` or `config.beginning_of_week`.
3147

3148
```ruby
3149 3150 3151 3152 3153
d = Date.new(2010, 5, 8)     # => Sat, 08 May 2010
d.beginning_of_week          # => Mon, 03 May 2010
d.beginning_of_week(:sunday) # => Sun, 02 May 2010
d.end_of_week                # => Sun, 09 May 2010
d.end_of_week(:sunday)       # => Sat, 08 May 2010
3154
```
3155

3156
`beginning_of_week` is aliased to `at_beginning_of_week` and `end_of_week` is aliased to `at_end_of_week`.
3157

3158
##### `monday`, `sunday`
V
Vijay Dev 已提交
3159

3160 3161
The methods `monday` and `sunday` return the dates for the previous Monday and
next Sunday, respectively.
V
Vijay Dev 已提交
3162

3163
```ruby
V
Vijay Dev 已提交
3164 3165 3166
d = Date.new(2010, 5, 8)     # => Sat, 08 May 2010
d.monday                     # => Mon, 03 May 2010
d.sunday                     # => Sun, 09 May 2010
3167 3168 3169 3170 3171 3172

d = Date.new(2012, 9, 10)    # => Mon, 10 Sep 2012
d.monday                     # => Mon, 10 Sep 2012

d = Date.new(2012, 9, 16)    # => Sun, 16 Sep 2012
d.sunday                     # => Sun, 16 Sep 2012
3173
```
V
Vijay Dev 已提交
3174

3175
##### `prev_week`, `next_week`
3176

X
Xavier Noria 已提交
3177
The method `next_week` receives a symbol with a day name in English (default is the thread local `Date.beginning_of_week`, or `config.beginning_of_week`, or `:monday`) and it returns the date corresponding to that day.
3178

3179
```ruby
3180 3181 3182
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.next_week              # => Mon, 10 May 2010
d.next_week(:saturday)   # => Sat, 15 May 2010
3183
```
3184

3185
The method `prev_week` is analogous:
3186

3187
```ruby
3188 3189 3190
d.prev_week              # => Mon, 26 Apr 2010
d.prev_week(:saturday)   # => Sat, 01 May 2010
d.prev_week(:friday)     # => Fri, 30 Apr 2010
3191
```
3192

3193
`prev_week` is aliased to `last_week`.
X
Xavier Noria 已提交
3194 3195

Both `next_week` and `prev_week` work as expected when `Date.beginning_of_week` or `config.beginning_of_week` are set.
3196

3197
##### `beginning_of_month`, `end_of_month`
3198

3199
The methods `beginning_of_month` and `end_of_month` return the dates for the beginning and end of the month:
3200

3201
```ruby
3202 3203 3204
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_month     # => Sat, 01 May 2010
d.end_of_month           # => Mon, 31 May 2010
3205
```
3206

3207
`beginning_of_month` is aliased to `at_beginning_of_month`, and `end_of_month` is aliased to `at_end_of_month`.
3208

3209
##### `beginning_of_quarter`, `end_of_quarter`
3210

3211
The methods `beginning_of_quarter` and `end_of_quarter` return the dates for the beginning and end of the quarter of the receiver's calendar year:
3212

3213
```ruby
3214 3215 3216
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_quarter   # => Thu, 01 Apr 2010
d.end_of_quarter         # => Wed, 30 Jun 2010
3217
```
3218

3219
`beginning_of_quarter` is aliased to `at_beginning_of_quarter`, and `end_of_quarter` is aliased to `at_end_of_quarter`.
3220

3221
##### `beginning_of_year`, `end_of_year`
3222

3223
The methods `beginning_of_year` and `end_of_year` return the dates for the beginning and end of the year:
3224

3225
```ruby
3226 3227 3228
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_year      # => Fri, 01 Jan 2010
d.end_of_year            # => Fri, 31 Dec 2010
3229
```
3230

3231
`beginning_of_year` is aliased to `at_beginning_of_year`, and `end_of_year` is aliased to `at_end_of_year`.
3232

3233
#### Other Date Computations
3234

3235
##### `years_ago`, `years_since`
3236

3237
The method `years_ago` receives a number of years and returns the same date those many years ago:
3238

3239
```ruby
3240 3241
date = Date.new(2010, 6, 7)
date.years_ago(10) # => Wed, 07 Jun 2000
3242
```
3243

3244
`years_since` moves forward in time:
3245

3246
```ruby
3247 3248
date = Date.new(2010, 6, 7)
date.years_since(10) # => Sun, 07 Jun 2020
3249
```
3250 3251 3252

If such a day does not exist, the last day of the corresponding month is returned:

3253
```ruby
3254 3255
Date.new(2012, 2, 29).years_ago(3)     # => Sat, 28 Feb 2009
Date.new(2012, 2, 29).years_since(3)   # => Sat, 28 Feb 2015
3256
```
3257

3258
##### `months_ago`, `months_since`
3259

3260
The methods `months_ago` and `months_since` work analogously for months:
3261

3262
```ruby
3263 3264
Date.new(2010, 4, 30).months_ago(2)   # => Sun, 28 Feb 2010
Date.new(2010, 4, 30).months_since(2) # => Wed, 30 Jun 2010
3265
```
3266 3267 3268

If such a day does not exist, the last day of the corresponding month is returned:

3269
```ruby
3270 3271
Date.new(2010, 4, 30).months_ago(2)    # => Sun, 28 Feb 2010
Date.new(2009, 12, 31).months_since(2) # => Sun, 28 Feb 2010
3272
```
3273

3274
##### `weeks_ago`
3275

3276
The method `weeks_ago` works analogously for weeks:
3277

3278
```ruby
3279 3280
Date.new(2010, 5, 24).weeks_ago(1)    # => Mon, 17 May 2010
Date.new(2010, 5, 24).weeks_ago(2)    # => Mon, 10 May 2010
3281
```
3282

3283
##### `advance`
3284

3285
The most generic way to jump to other days is `advance`. This method receives a hash with keys `:years`, `:months`, `:weeks`, `:days`, and returns a date advanced as much as the present keys indicate:
3286

3287
```ruby
3288
date = Date.new(2010, 6, 6)
3289 3290
date.advance(years: 1, weeks: 2)  # => Mon, 20 Jun 2011
date.advance(months: 2, days: -2) # => Wed, 04 Aug 2010
3291
```
3292 3293 3294 3295 3296

Note in the previous example that increments may be negative.

To perform the computation the method first increments years, then months, then weeks, and finally days. This order is important towards the end of months. Say for example we are at the end of February of 2010, and we want to move one month and one day forward.

3297
The method `advance` advances first one month, and then one day, the result is:
3298

3299
```ruby
3300
Date.new(2010, 2, 28).advance(months: 1, days: 1)
3301
# => Sun, 29 Mar 2010
3302
```
3303 3304 3305

While if it did it the other way around the result would be different:

3306
```ruby
3307
Date.new(2010, 2, 28).advance(days: 1).advance(months: 1)
3308
# => Thu, 01 Apr 2010
3309
```
3310

3311
#### Changing Components
3312

3313
The method `change` allows you to get a new date which is the same as the receiver except for the given year, month, or day:
3314

3315
```ruby
3316
Date.new(2010, 12, 23).change(year: 2011, month: 11)
3317
# => Wed, 23 Nov 2011
3318
```
3319

3320
This method is not tolerant to non-existing dates, if the change is invalid `ArgumentError` is raised:
3321

3322
```ruby
3323
Date.new(2010, 1, 31).change(month: 2)
3324
# => ArgumentError: invalid date
3325
```
3326

3327
#### Durations
3328

E
Evan Farrar 已提交
3329
Durations can be added to and subtracted from dates:
3330

3331
```ruby
3332 3333 3334 3335 3336 3337
d = Date.current
# => Mon, 09 Aug 2010
d + 1.year
# => Tue, 09 Aug 2011
d - 3.hours
# => Sun, 08 Aug 2010 21:00:00 UTC +00:00
3338
```
3339

3340
They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
3341

3342
```ruby
3343 3344
Date.new(1582, 10, 4) + 1.day
# => Fri, 15 Oct 1582
3345
```
3346

3347
#### Timestamps
3348

3349
INFO: The following methods return a `Time` object if possible, otherwise a `DateTime`. If set, they honor the user time zone.
3350

3351
##### `beginning_of_day`, `end_of_day`
3352

3353
The method `beginning_of_day` returns a timestamp at the beginning of the day (00:00:00):
3354

3355
```ruby
3356
date = Date.new(2010, 6, 7)
3357
date.beginning_of_day # => Mon Jun 07 00:00:00 +0200 2010
3358
```
3359

3360
The method `end_of_day` returns a timestamp at the end of the day (23:59:59):
3361

3362
```ruby
3363
date = Date.new(2010, 6, 7)
3364
date.end_of_day # => Mon Jun 07 23:59:59 +0200 2010
3365
```
3366

3367
`beginning_of_day` is aliased to `at_beginning_of_day`, `midnight`, `at_midnight`.
3368

3369
##### `beginning_of_hour`, `end_of_hour`
3370

3371
The method `beginning_of_hour` returns a timestamp at the beginning of the hour (hh:00:00):
3372

3373
```ruby
3374 3375
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.beginning_of_hour # => Mon Jun 07 19:00:00 +0200 2010
3376
```
3377

3378
The method `end_of_hour` returns a timestamp at the end of the hour (hh:59:59):
3379

3380
```ruby
3381 3382
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.end_of_hour # => Mon Jun 07 19:59:59 +0200 2010
3383
```
3384

3385
`beginning_of_hour` is aliased to `at_beginning_of_hour`.
3386

3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
##### `beginning_of_minute`, `end_of_minute`

The method `beginning_of_minute` returns a timestamp at the beginning of the minute (hh:mm:00):

```ruby
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.beginning_of_minute # => Mon Jun 07 19:55:00 +0200 2010
```

The method `end_of_minute` returns a timestamp at the end of the minute (hh:mm:59):

```ruby
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.end_of_minute # => Mon Jun 07 19:55:59 +0200 2010
```

`beginning_of_minute` is aliased to `at_beginning_of_minute`.

INFO: `beginning_of_hour`, `end_of_hour`, `beginning_of_minute` and `end_of_minute` are implemented for `Time` and `DateTime` but **not** `Date` as it does not make sense to request the beginning or end of an hour or minute on a `Date` instance.
3406

3407
##### `ago`, `since`
3408

3409
The method `ago` receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight:
3410

3411
```ruby
3412
date = Date.current # => Fri, 11 Jun 2010
3413
date.ago(1)         # => Thu, 10 Jun 2010 23:59:59 EDT -04:00
3414
```
3415

3416
Similarly, `since` moves forward:
3417

3418
```ruby
3419
date = Date.current # => Fri, 11 Jun 2010
3420
date.since(1)       # => Fri, 11 Jun 2010 00:00:01 EDT -04:00
3421
```
3422

3423
#### Other Time Computations
3424

3425
### Conversions
3426

3427
Extensions to `DateTime`
3428
------------------------
3429

3430
WARNING: `DateTime` is not aware of DST rules and so some of these methods have edge cases when a DST change is going on. For example `seconds_since_midnight` might not return the real amount in such a day.
3431

3432
### Calculations
3433

3434
NOTE: All the following methods are defined in `active_support/core_ext/date_time/calculations.rb`.
3435

3436
The class `DateTime` is a subclass of `Date` so by loading `active_support/core_ext/date/calculations.rb` you inherit these methods and their aliases, except that they will always return datetimes:
3437

3438
```ruby
3439 3440
yesterday
tomorrow
3441
beginning_of_week (at_beginning_of_week)
V
Vijay Dev 已提交
3442
end_of_week (at_end_of_week)
3443 3444
monday
sunday
3445
weeks_ago
3446
prev_week (last_week)
3447 3448 3449
next_week
months_ago
months_since
3450 3451
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
3452
prev_month (last_month)
3453
next_month
3454 3455 3456 3457
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
3458 3459
years_ago
years_since
3460
prev_year (last_year)
3461
next_year
3462
```
3463

3464
The following methods are reimplemented so you do **not** need to load `active_support/core_ext/date/calculations.rb` for these ones:
3465

3466
```ruby
3467
beginning_of_day (midnight, at_midnight, at_beginning_of_day)
3468 3469
end_of_day
ago
3470
since (in)
3471
```
3472

3473
On the other hand, `advance` and `change` are also defined and support more options, they are documented below.
3474

3475
The following methods are only implemented in `active_support/core_ext/date_time/calculations.rb` as they only make sense when used with a `DateTime` instance:
3476

3477
```ruby
3478 3479
beginning_of_hour (at_beginning_of_hour)
end_of_hour
3480
```
3481

3482
#### Named Datetimes
3483

3484
##### `DateTime.current`
3485

3486
Active Support defines `DateTime.current` to be like `Time.now.to_datetime`, except that it honors the user time zone, if defined. It also defines `DateTime.yesterday` and `DateTime.tomorrow`, and the instance predicates `past?`, and `future?` relative to `DateTime.current`.
3487

3488
#### Other Extensions
3489

3490
##### `seconds_since_midnight`
3491

3492
The method `seconds_since_midnight` returns the number of seconds since midnight:
3493

3494
```ruby
3495 3496
now = DateTime.current     # => Mon, 07 Jun 2010 20:26:36 +0000
now.seconds_since_midnight # => 73596
3497
```
3498

3499
##### `utc`
3500

3501
The method `utc` gives you the same datetime in the receiver expressed in UTC.
3502

3503
```ruby
3504 3505
now = DateTime.current # => Mon, 07 Jun 2010 19:27:52 -0400
now.utc                # => Mon, 07 Jun 2010 23:27:52 +0000
3506
```
3507

3508
This method is also aliased as `getutc`.
3509

3510
##### `utc?`
3511

3512
The predicate `utc?` says whether the receiver has UTC as its time zone:
3513

3514
```ruby
3515 3516 3517
now = DateTime.now # => Mon, 07 Jun 2010 19:30:47 -0400
now.utc?           # => false
now.utc.utc?       # => true
3518
```
3519

3520
##### `advance`
3521

3522
The most generic way to jump to another datetime is `advance`. This method receives a hash with keys `:years`, `:months`, `:weeks`, `:days`, `:hours`, `:minutes`, and `:seconds`, and returns a datetime advanced as much as the present keys indicate.
3523

3524
```ruby
3525 3526
d = DateTime.current
# => Thu, 05 Aug 2010 11:33:31 +0000
3527
d.advance(years: 1, months: 1, days: 1, hours: 1, minutes: 1, seconds: 1)
3528
# => Tue, 06 Sep 2011 12:34:32 +0000
3529
```
3530

3531
This method first computes the destination date passing `:years`, `:months`, `:weeks`, and `:days` to `Date#advance` documented above. After that, it adjusts the time calling `since` with the number of seconds to advance. This order is relevant, a different ordering would give different datetimes in some edge-cases. The example in `Date#advance` applies, and we can extend it to show order relevance related to the time bits.
3532 3533 3534

If we first move the date bits (that have also a relative order of processing, as documented before), and then the time bits we get for example the following computation:

3535
```ruby
3536 3537
d = DateTime.new(2010, 2, 28, 23, 59, 59)
# => Sun, 28 Feb 2010 23:59:59 +0000
3538
d.advance(months: 1, seconds: 1)
3539
# => Mon, 29 Mar 2010 00:00:00 +0000
3540
```
3541 3542 3543

but if we computed them the other way around, the result would be different:

3544
```ruby
3545
d.advance(seconds: 1).advance(months: 1)
3546
# => Thu, 01 Apr 2010 00:00:00 +0000
3547
```
3548

3549
WARNING: Since `DateTime` is not DST-aware you can end up in a non-existing point in time with no warning or error telling you so.
3550

3551
#### Changing Components
3552

3553
The method `change` allows you to get a new datetime which is the same as the receiver except for the given options, which may include `:year`, `:month`, `:day`, `:hour`, `:min`, `:sec`, `:offset`, `:start`:
3554

3555
```ruby
3556 3557
now = DateTime.current
# => Tue, 08 Jun 2010 01:56:22 +0000
3558
now.change(year: 2011, offset: Rational(-6, 24))
3559
# => Wed, 08 Jun 2011 01:56:22 -0600
3560
```
3561 3562 3563

If hours are zeroed, then minutes and seconds are too (unless they have given values):

3564
```ruby
3565
now.change(hour: 0)
3566
# => Tue, 08 Jun 2010 00:00:00 +0000
3567
```
3568 3569 3570

Similarly, if minutes are zeroed, then seconds are too (unless it has given a value):

3571
```ruby
3572
now.change(min: 0)
3573
# => Tue, 08 Jun 2010 01:00:00 +0000
3574
```
3575

3576
This method is not tolerant to non-existing dates, if the change is invalid `ArgumentError` is raised:
3577

3578
```ruby
3579
DateTime.current.change(month: 2, day: 30)
3580
# => ArgumentError: invalid date
3581
```
3582

3583
#### Durations
3584

E
Evan Farrar 已提交
3585
Durations can be added to and subtracted from datetimes:
3586

3587
```ruby
3588 3589 3590 3591 3592 3593
now = DateTime.current
# => Mon, 09 Aug 2010 23:15:17 +0000
now + 1.year
# => Tue, 09 Aug 2011 23:15:17 +0000
now - 1.week
# => Mon, 02 Aug 2010 23:15:17 +0000
3594
```
3595

3596
They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
3597

3598
```ruby
3599 3600
DateTime.new(1582, 10, 4, 23) + 1.hour
# => Fri, 15 Oct 1582 00:00:00 +0000
3601
```
3602

3603
Extensions to `Time`
3604
--------------------
3605

3606
### Calculations
3607

3608
NOTE: All the following methods are defined in `active_support/core_ext/time/calculations.rb`.
3609

3610
Active Support adds to `Time` many of the methods available for `DateTime`:
3611

3612
```ruby
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
past?
today?
future?
yesterday
tomorrow
seconds_since_midnight
change
advance
ago
since (in)
beginning_of_day (midnight, at_midnight, at_beginning_of_day)
end_of_day
3625 3626
beginning_of_hour (at_beginning_of_hour)
end_of_hour
3627
beginning_of_week (at_beginning_of_week)
V
Vijay Dev 已提交
3628
end_of_week (at_end_of_week)
3629
monday
V
Vijay Dev 已提交
3630
sunday
3631
weeks_ago
3632
prev_week (last_week)
3633 3634 3635 3636 3637
next_week
months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
3638
prev_month (last_month)
3639 3640 3641 3642 3643 3644 3645
next_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
3646
prev_year (last_year)
3647
next_year
3648
```
3649 3650 3651

They are analogous. Please refer to their documentation above and take into account the following differences:

3652 3653
* `change` accepts an additional `:usec` option.
* `Time` understands DST, so you get correct DST calculations as in
3654

3655
```ruby
3656 3657 3658
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>

3659
# In Barcelona, 2010/03/28 02:00 +0100 becomes 2010/03/28 03:00 +0200 due to DST.
3660
t = Time.local(2010, 3, 28, 1, 59, 59)
V
Vijay Dev 已提交
3661
# => Sun Mar 28 01:59:59 +0100 2010
3662
t.advance(seconds: 1)
V
Vijay Dev 已提交
3663
# => Sun Mar 28 03:00:00 +0200 2010
3664
```
3665

3666
* If `since` or `ago` jump to a time that can't be expressed with `Time` a `DateTime` object is returned instead.
3667

3668
#### `Time.current`
3669

3670
Active Support defines `Time.current` to be today in the current time zone. That's like `Time.now`, except that it honors the user time zone, if defined. It also defines `Time.yesterday` and `Time.tomorrow`, and the instance predicates `past?`, `today?`, and `future?`, all of them relative to `Time.current`.
3671

3672
When making Time comparisons using methods which honor the user time zone, make sure to use `Time.current` and not `Time.now`. There are cases where the user time zone might be in the future compared to the system time zone, which `Time.today` uses by default. This means `Time.now` may equal `Time.yesterday`.
3673

3674
#### `all_day`, `all_week`, `all_month`, `all_quarter` and `all_year`
3675

3676
The method `all_day` returns a range representing the whole day of the current time.
3677

3678
```ruby
3679
now = Time.current
V
Vijay Dev 已提交
3680
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
3681
now.all_day
3682
# => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Mon, 09 Aug 2010 23:59:59 UTC +00:00
3683
```
3684

3685
Analogously, `all_week`, `all_month`, `all_quarter` and `all_year` all serve the purpose of generating time ranges.
3686

3687
```ruby
3688
now = Time.current
V
Vijay Dev 已提交
3689
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
3690
now.all_week
3691
# => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Sun, 15 Aug 2010 23:59:59 UTC +00:00
3692 3693
now.all_week(:sunday)
# => Sun, 16 Sep 2012 00:00:00 UTC +00:00..Sat, 22 Sep 2012 23:59:59 UTC +00:00
3694
now.all_month
3695
# => Sat, 01 Aug 2010 00:00:00 UTC +00:00..Tue, 31 Aug 2010 23:59:59 UTC +00:00
3696
now.all_quarter
3697
# => Thu, 01 Jul 2010 00:00:00 UTC +00:00..Thu, 30 Sep 2010 23:59:59 UTC +00:00
3698
now.all_year
3699
# => Fri, 01 Jan 2010 00:00:00 UTC +00:00..Fri, 31 Dec 2010 23:59:59 UTC +00:00
3700
```
3701

3702
### Time Constructors
3703

3704
Active Support defines `Time.current` to be `Time.zone.now` if there's a user time zone defined, with fallback to `Time.now`:
3705

3706
```ruby
3707 3708 3709
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
Time.current
V
Vijay Dev 已提交
3710
# => Fri, 06 Aug 2010 17:11:58 CEST +02:00
3711
```
3712

3713
Analogously to `DateTime`, the predicates `past?`, and `future?` are relative to `Time.current`.
3714

3715
If the time to be constructed lies beyond the range supported by `Time` in the runtime platform, usecs are discarded and a `DateTime` object is returned instead.
3716

3717
#### Durations
3718

E
Evan Farrar 已提交
3719
Durations can be added to and subtracted from time objects:
3720

3721
```ruby
3722 3723 3724 3725 3726 3727
now = Time.current
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
now + 1.year
#  => Tue, 09 Aug 2011 23:21:11 UTC +00:00
now - 1.week
# => Mon, 02 Aug 2010 23:21:11 UTC +00:00
3728
```
3729

3730
They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
3731

3732
```ruby
3733
Time.utc(1582, 10, 3) + 5.days
3734
# => Mon Oct 18 00:00:00 UTC 1582
3735
```
3736

3737
Extensions to `File`
3738
--------------------
3739

3740
### `atomic_write`
3741

3742
With the class method `File.atomic_write` you can write to a file in a way that will prevent any reader from seeing half-written content.
3743

3744
The name of the file is passed as an argument, and the method yields a file handle opened for writing. Once the block is done `atomic_write` closes the file handle and completes its job.
3745

3746
For example, Action Pack uses this method to write asset cache files like `all.css`:
3747

3748
```ruby
3749 3750 3751
File.atomic_write(joined_asset_path) do |cache|
  cache.write(join_asset_file_contents(asset_paths))
end
3752
```
3753

3754 3755 3756
To accomplish this `atomic_write` creates a temporary file. That's the file the code in the block actually writes to. On completion, the temporary file is renamed, which is an atomic operation on POSIX systems. If the target file exists `atomic_write` overwrites it and keeps owners and permissions. However there are a few cases where `atomic_write` cannot change the file ownership or permissions, this error is caught and skipped over trusting in the user/filesystem to ensure the file is accessible to the processes that need it.

NOTE. Due to the chmod operation `atomic_write` performs, if the target file has an ACL set on it this ACL will be recalculated/modified.
3757

3758
WARNING. Note you can't append with `atomic_write`.
3759 3760 3761

The auxiliary file is written in a standard directory for temporary files, but you can pass a directory of your choice as second argument.

3762
NOTE: Defined in `active_support/core_ext/file/atomic.rb`.
3763

3764
Extensions to `Marshal`
X
Xavier Noria 已提交
3765
-----------------------
3766 3767 3768

### `load`

X
Xavier Noria 已提交
3769
Active Support adds constant autoloading support to `load`.
3770

3771
For example, the file cache store deserializes this way:
3772 3773 3774 3775 3776

```ruby
File.open(file_name) { |f| Marshal.load(f) }
```

3777
If the cached data refers to a constant that is unknown at that point, the autoloading mechanism is triggered and if it succeeds the deserialization is retried transparently.
3778

X
Xavier Noria 已提交
3779
WARNING. If the argument is an `IO` it needs to respond to `rewind` to be able to retry. Regular files respond to `rewind`.
3780 3781 3782

NOTE: Defined in `active_support/core_ext/marshal.rb`.

3783
Extensions to `Logger`
3784
----------------------
3785

3786
### `around_[level]`
3787

3788
Takes two arguments, a `before_message` and `after_message` and calls the current level method on the `Logger` instance, passing in the `before_message`, then the specified message, then the `after_message`:
3789

3790
```ruby
V
Vijay Dev 已提交
3791 3792
logger = Logger.new("log/development.log")
logger.around_info("before", "after") { |logger| logger.info("during") }
3793
```
3794

3795
### `silence`
3796 3797 3798

Silences every log level lesser to the specified one for the duration of the given block. Log level orders are: debug, info, error and fatal.

3799
```ruby
V
Vijay Dev 已提交
3800 3801 3802 3803 3804
logger = Logger.new("log/development.log")
logger.silence(Logger::INFO) do
  logger.debug("In space, no one can hear you scream.")
  logger.info("Scream all you want, small mailman!")
end
3805
```
3806

3807
### `datetime_format=`
3808

3809
Modifies the datetime format output by the formatter class associated with this logger. If the formatter class does not have a `datetime_format` method then this is ignored.
3810

3811
```ruby
V
Vijay Dev 已提交
3812 3813
class Logger::FormatWithTime < Logger::Formatter
  cattr_accessor(:datetime_format) { "%Y%m%d%H%m%S" }
V
Vijay Dev 已提交
3814

V
Vijay Dev 已提交
3815 3816
  def self.call(severity, timestamp, progname, msg)
    "#{timestamp.strftime(datetime_format)} -- #{String === msg ? msg : msg.inspect}\n"
3817
  end
V
Vijay Dev 已提交
3818
end
V
Vijay Dev 已提交
3819

V
Vijay Dev 已提交
3820 3821 3822
logger = Logger.new("log/development.log")
logger.formatter = Logger::FormatWithTime
logger.info("<- is the current time")
3823
```
3824

3825
NOTE: Defined in `active_support/core_ext/logger.rb`.
3826

3827
Extensions to `NameError`
3828
-------------------------
3829

3830
Active Support adds `missing_name?` to `NameError`, which tests whether the exception was raised because of the name passed as argument.
3831 3832 3833

The name may be given as a symbol or string. A symbol is tested against the bare constant name, a string is against the fully-qualified constant name.

3834
TIP: A symbol can represent a fully-qualified constant name as in `:"ActiveRecord::Base"`, so the behavior for symbols is defined for convenience, not because it has to be that way technically.
3835

3836
For example, when an action of `PostsController` is called Rails tries optimistically to use `PostsHelper`. It is OK that the helper module does not exist, so if an exception for that constant name is raised it should be silenced. But it could be the case that `posts_helper.rb` raises a `NameError` due to an actual unknown constant. That should be reraised. The method `missing_name?` provides a way to distinguish both cases:
3837

3838
```ruby
3839 3840 3841 3842 3843
def default_helper_module!
  module_name = name.sub(/Controller$/, '')
  module_path = module_name.underscore
  helper module_path
rescue MissingSourceFile => e
3844
  raise e unless e.is_missing? "helpers/#{module_path}_helper"
3845 3846 3847
rescue NameError => e
  raise e unless e.missing_name? "#{module_name}Helper"
end
3848
```
3849

3850
NOTE: Defined in `actionpack/lib/abstract_controller/helpers.rb`.
3851

3852
Extensions to `LoadError`
3853
-------------------------
3854

3855
Active Support adds `is_missing?` to `LoadError`, and also assigns that class to the constant `MissingSourceFile` for backwards compatibility.
3856

3857
Given a path name `is_missing?` tests whether the exception was raised due to that particular file (except perhaps for the ".rb" extension).
3858

3859
For example, when an action of `PostsController` is called Rails tries to load `posts_helper.rb`, but that file may not exist. That's fine, the helper module is not mandatory so Rails silences a load error. But it could be the case that the helper module does exist and in turn requires another library that is missing. In that case Rails must reraise the exception. The method `is_missing?` provides a way to distinguish both cases:
3860

3861
```ruby
3862 3863 3864 3865 3866
def default_helper_module!
  module_name = name.sub(/Controller$/, '')
  module_path = module_name.underscore
  helper module_path
rescue MissingSourceFile => e
3867
  raise e unless e.is_missing? "helpers/#{module_path}_helper"
3868 3869 3870
rescue NameError => e
  raise e unless e.missing_name? "#{module_name}Helper"
end
3871
```
3872

R
Rashmi Yadav 已提交
3873
NOTE: Defined in `actionpack/lib/abstract_controller/helpers.rb`.