aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Cohen <cohen.jeff@gmail.com>2013-06-08 11:10:30 -0500
committerJeff Cohen <cohen.jeff@gmail.com>2013-06-08 11:10:30 -0500
commitad2dc5f90c688745e663bc8d2b7ca401515c27c9 (patch)
treeb0a92ade7e06c3a3c81a3c100d4a4989ca136cc8
parent1f4c4c69ee5d0cc37ed35ffd3d27fb29c72751cf (diff)
downloadrails-ad2dc5f90c688745e663bc8d2b7ca401515c27c9.tar.gz
rails-ad2dc5f90c688745e663bc8d2b7ca401515c27c9.tar.bz2
rails-ad2dc5f90c688745e663bc8d2b7ca401515c27c9.zip
Describe ActiveSupport core extensions for BigDecimal, JSON support, and instance variable names.
-rw-r--r--guides/source/active_support_core_extensions.md66
1 files changed, 65 insertions, 1 deletions
diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md
index 274cffbfab..0a29d85303 100644
--- a/guides/source/active_support_core_extensions.md
+++ b/guides/source/active_support_core_extensions.md
@@ -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`
--------------------------