Skip to content

Commit

Permalink
Describe ActiveSupport core extensions for BigDecimal, JSON support, …
Browse files Browse the repository at this point in the history
…and instance variable names.
  • Loading branch information
JeffCohen committed Jun 8, 2013
1 parent 1f4c4c6 commit ad2dc5f
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion guides/source/active_support_core_extensions.md
Expand Up @@ -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`. 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 ### Instance Variables


Active Support provides several methods to ease access to instance variables. Active Support provides several methods to ease access to instance variables.
Expand All @@ -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`. 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 ### 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: The methods `silence_warnings` and `enable_warnings` change the value of `$VERBOSE` accordingly for the duration of their block, and reset it afterwards:
Expand Down Expand Up @@ -2011,8 +2052,31 @@ NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.


Extensions to `BigDecimal` 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` Extensions to `Enumerable`
-------------------------- --------------------------
Expand Down

0 comments on commit ad2dc5f

Please sign in to comment.