mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Describe ActiveSupport core extensions for BigDecimal, JSON support, and instance variable names.
This commit is contained in:
parent
1f4c4c69ee
commit
ad2dc5f90c
1 changed files with 65 additions and 1 deletions
|
@ -418,6 +418,14 @@ TIP: Since `with_options` forwards calls to its receiver they can be nested. Eac
|
|||
|
||||
NOTE: Defined in `active_support/core_ext/object/with_options.rb`.
|
||||
|
||||
### JSON support
|
||||
|
||||
Active Support provides a better implemention of `to_json` than the json gem ordinarily provides for Ruby objects. This is because some classes, like Hash and OrderedHash, needs special handling in order to provide a proper JSON representation.
|
||||
|
||||
Active Support also provides an implementation of `as_json` for the Process::Status class.
|
||||
|
||||
NOTE: Defined in `active_support/core_ext/object/to_json.rb`.
|
||||
|
||||
### Instance Variables
|
||||
|
||||
Active Support provides several methods to ease access to instance variables.
|
||||
|
@ -439,6 +447,39 @@ C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
|
|||
|
||||
NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.
|
||||
|
||||
#### `instance_values`
|
||||
|
||||
The method `instance_values` returns a hash that maps instance variable names without "@" to their
|
||||
corresponding values. Keys are strings:
|
||||
|
||||
```ruby
|
||||
class C
|
||||
def initialize(x, y)
|
||||
@x, @y = x, y
|
||||
end
|
||||
end
|
||||
|
||||
C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
|
||||
```
|
||||
|
||||
NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.
|
||||
|
||||
#### `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`.
|
||||
|
||||
### Silencing Warnings, Streams, and Exceptions
|
||||
|
||||
The methods `silence_warnings` and `enable_warnings` change the value of `$VERBOSE` accordingly for the duration of their block, and reset it afterwards:
|
||||
|
@ -2011,8 +2052,31 @@ NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.
|
|||
|
||||
Extensions to `BigDecimal`
|
||||
--------------------------
|
||||
### `to_s`
|
||||
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`
|
||||
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:
|
||||
|
||||
```ruby
|
||||
BigDecimal.new(5.00, 6).to_formatted_s # => "5.0"
|
||||
```
|
||||
|
||||
and that symbol specifiers are also supported:
|
||||
|
||||
```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"
|
||||
```
|
||||
|
||||
Extensions to `Enumerable`
|
||||
--------------------------
|
||||
|
|
Loading…
Reference in a new issue