diff options
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index aea77c8d4e..4157694844 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -610,7 +610,23 @@ h3. Extensions to +FalseClass+ h3. Extensions to +Symbol+ -... +h4. +to_proc+ + +The method +to_proc+ turns a symbol into a Proc object so that for example + +<ruby> +emails = users.map {|u| u.email} +</ruby> + +can be written as + +<ruby> +emails = users.map(&:email) +</ruby> + +TIP: If the method that receives the Proc yields more than one value to it the rest are considered to be arguments of the method call. + +Symbols from Ruby 1.8.7 on respond to +to_proc+, and Active Support defines it for previous versions. h3. Extensions to +String+ @@ -789,7 +805,25 @@ h3. Extensions to +Pathname+ h3. Extensions to +File+ -... +h4. +atomic_write+ + +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. + +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. + +For example, Action Pack uses this method to write asset cache files like +all.css+: + +<ruby> +File.atomic_write(joined_asset_path) do |cache| + cache.write(join_asset_file_contents(asset_paths)) +end +</ruby> + +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. If the target file exists +atomic_write+ overwrites it and keeps owners and permissions. + +WARNING. Note you can't append with +atomic_write+. + +The auxiliary file is written in a standard directory for temporary files, but you can pass a directory of your choice as second argument. h3. Extensions to +Exception+ |