Prev | Current Page 99 | Next

Brad Ediger

"Advanced Rails"


File.atomic_write('important_file') do |important_file|
important_file.write data
end
Float core_ext/float.rb
Float#round rounds a floating-point number to the specified decimal place:
Math::PI # => 3.14159265358979
Math::PI.round # => 3
Math::PI.round(1) # => 3.1
Math::PI.round(2) # => 3.14
Math::PI.round(3) # => 3.142
Hash
Hash | 67
Hash
Conversions core_ext/hash/conversions.rb
Many methods are provided to convert back and forth between hashes and XML representation.
These are useful for round-tripping a hash to and from XML for web services.
??? Hash#to_xml converts a single-level hash to XML:
{:a => "One", :b => "Two", :int => 1, :opt => false}.to_xml
yields:


One
Two
1
false

??? Hash.from_xml creates a hash from the provided XML document. Note that the root
element is included in the hash.
xml = <

One
Two

EOXML
Hash.from_xml(xml)["hash"] # => {"a"=>"One", "b"=>"Two"}
Option processing core_ext/hash/diff.rb, core_ext/hash/keys.rb, core_ext/hash/reverse_merge.rb,
core_ext/hash/slice.rb, core_ext/hash/except.rb
Rails uses hashes to provide keyword arguments to many methods. This is mostly due to a
bit of syntactic sugar on Ruby??™s part; if a hash is passed as a function??™s last argument, the
brackets can be omitted, resembling keyword arguments.


Pages:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111