railties 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (334) hide show
  1. data/CHANGELOG +2201 -0
  2. data/README +286 -0
  3. data/bin/rails +11 -0
  4. data/builtin/rails_info/rails/info.rb +147 -0
  5. data/builtin/rails_info/rails/info_controller.rb +15 -0
  6. data/builtin/rails_info/rails/info_helper.rb +2 -0
  7. data/builtin/routes.rb +3 -0
  8. data/guides/files/javascripts/code_highlighter.js +188 -0
  9. data/guides/files/javascripts/guides.js +7 -0
  10. data/guides/files/javascripts/highlighters.js +90 -0
  11. data/guides/files/stylesheets/main.css +441 -0
  12. data/guides/files/stylesheets/print.css +52 -0
  13. data/guides/files/stylesheets/reset.css +43 -0
  14. data/guides/files/stylesheets/style.css +13 -0
  15. data/guides/files/stylesheets/syntax.css +31 -0
  16. data/guides/images/belongs_to.png +0 -0
  17. data/guides/images/book_icon.gif +0 -0
  18. data/guides/images/bullet.gif +0 -0
  19. data/guides/images/chapters_icon.gif +0 -0
  20. data/guides/images/check_bullet.gif +0 -0
  21. data/guides/images/credits_pic_blank.gif +0 -0
  22. data/guides/images/csrf.png +0 -0
  23. data/guides/images/customized_error_messages.png +0 -0
  24. data/guides/images/error_messages.png +0 -0
  25. data/guides/images/feature_tile.gif +0 -0
  26. data/guides/images/footer_tile.gif +0 -0
  27. data/guides/images/fxn.png +0 -0
  28. data/guides/images/grey_bullet.gif +0 -0
  29. data/guides/images/habtm.png +0 -0
  30. data/guides/images/has_many.png +0 -0
  31. data/guides/images/has_many_through.png +0 -0
  32. data/guides/images/has_one.png +0 -0
  33. data/guides/images/has_one_through.png +0 -0
  34. data/guides/images/header_backdrop.png +0 -0
  35. data/guides/images/header_tile.gif +0 -0
  36. data/guides/images/i18n/demo_localized_pirate.png +0 -0
  37. data/guides/images/i18n/demo_translated_en.png +0 -0
  38. data/guides/images/i18n/demo_translated_pirate.png +0 -0
  39. data/guides/images/i18n/demo_translation_missing.png +0 -0
  40. data/guides/images/i18n/demo_untranslated.png +0 -0
  41. data/guides/images/icons/README +5 -0
  42. data/guides/images/icons/callouts/1.png +0 -0
  43. data/guides/images/icons/callouts/10.png +0 -0
  44. data/guides/images/icons/callouts/11.png +0 -0
  45. data/guides/images/icons/callouts/12.png +0 -0
  46. data/guides/images/icons/callouts/13.png +0 -0
  47. data/guides/images/icons/callouts/14.png +0 -0
  48. data/guides/images/icons/callouts/15.png +0 -0
  49. data/guides/images/icons/callouts/2.png +0 -0
  50. data/guides/images/icons/callouts/3.png +0 -0
  51. data/guides/images/icons/callouts/4.png +0 -0
  52. data/guides/images/icons/callouts/5.png +0 -0
  53. data/guides/images/icons/callouts/6.png +0 -0
  54. data/guides/images/icons/callouts/7.png +0 -0
  55. data/guides/images/icons/callouts/8.png +0 -0
  56. data/guides/images/icons/callouts/9.png +0 -0
  57. data/guides/images/icons/caution.png +0 -0
  58. data/guides/images/icons/example.png +0 -0
  59. data/guides/images/icons/home.png +0 -0
  60. data/guides/images/icons/important.png +0 -0
  61. data/guides/images/icons/next.png +0 -0
  62. data/guides/images/icons/note.png +0 -0
  63. data/guides/images/icons/prev.png +0 -0
  64. data/guides/images/icons/tip.png +0 -0
  65. data/guides/images/icons/up.png +0 -0
  66. data/guides/images/icons/warning.png +0 -0
  67. data/guides/images/nav_arrow.gif +0 -0
  68. data/guides/images/polymorphic.png +0 -0
  69. data/guides/images/posts_index.png +0 -0
  70. data/guides/images/rails_guides_logo.gif +0 -0
  71. data/guides/images/rails_logo_remix.gif +0 -0
  72. data/guides/images/rails_welcome.png +0 -0
  73. data/guides/images/session_fixation.png +0 -0
  74. data/guides/images/tab_grey.gif +0 -0
  75. data/guides/images/tab_info.gif +0 -0
  76. data/guides/images/tab_note.gif +0 -0
  77. data/guides/images/tab_red.gif +0 -0
  78. data/guides/images/tab_yellow.gif +0 -0
  79. data/guides/images/tab_yellow.png +0 -0
  80. data/guides/images/validation_error_messages.png +0 -0
  81. data/guides/rails_guides.rb +44 -0
  82. data/guides/rails_guides/generator.rb +178 -0
  83. data/guides/rails_guides/helpers.rb +34 -0
  84. data/guides/rails_guides/indexer.rb +55 -0
  85. data/guides/rails_guides/levenshtein.rb +31 -0
  86. data/guides/rails_guides/textile_extensions.rb +41 -0
  87. data/guides/source/2_2_release_notes.textile +422 -0
  88. data/guides/source/2_3_release_notes.textile +610 -0
  89. data/guides/source/3_0_release_notes.textile +560 -0
  90. data/guides/source/action_controller_overview.textile +783 -0
  91. data/guides/source/action_mailer_basics.textile +437 -0
  92. data/guides/source/action_view_overview.textile +1481 -0
  93. data/guides/source/active_record_basics.textile +204 -0
  94. data/guides/source/active_record_querying.textile +961 -0
  95. data/guides/source/active_support_core_extensions.textile +1835 -0
  96. data/guides/source/activerecord_validations_callbacks.textile +1127 -0
  97. data/guides/source/ajax_on_rails.textile +342 -0
  98. data/guides/source/association_basics.textile +1816 -0
  99. data/guides/source/caching_with_rails.textile +390 -0
  100. data/guides/source/command_line.textile +589 -0
  101. data/guides/source/configuring.textile +290 -0
  102. data/guides/source/contribute.textile +71 -0
  103. data/guides/source/contributing_to_rails.textile +272 -0
  104. data/guides/source/credits.textile.erb +52 -0
  105. data/guides/source/debugging_rails_applications.textile +709 -0
  106. data/guides/source/form_helpers.textile +768 -0
  107. data/guides/source/generators.textile +378 -0
  108. data/guides/source/getting_started.textile +1310 -0
  109. data/guides/source/i18n.textile +879 -0
  110. data/guides/source/index.textile.erb +124 -0
  111. data/guides/source/layout.html.erb +103 -0
  112. data/guides/source/layouts_and_rendering.textile +979 -0
  113. data/guides/source/migrations.textile +591 -0
  114. data/guides/source/nested_model_forms.textile +222 -0
  115. data/guides/source/performance_testing.textile +531 -0
  116. data/guides/source/plugins.textile +1512 -0
  117. data/guides/source/rails_application_templates.textile +238 -0
  118. data/guides/source/rails_on_rack.textile +306 -0
  119. data/guides/source/routing.textile +903 -0
  120. data/guides/source/security.textile +984 -0
  121. data/guides/source/testing.textile +946 -0
  122. data/lib/generators/erb.rb +8 -0
  123. data/lib/generators/erb/controller/controller_generator.rb +21 -0
  124. data/lib/generators/erb/controller/templates/view.html.erb +2 -0
  125. data/lib/generators/erb/mailer/mailer_generator.rb +20 -0
  126. data/lib/generators/erb/mailer/templates/view.text.erb +3 -0
  127. data/lib/generators/erb/scaffold/scaffold_generator.rb +53 -0
  128. data/lib/generators/erb/scaffold/templates/_form.html.erb +13 -0
  129. data/lib/generators/erb/scaffold/templates/edit.html.erb +6 -0
  130. data/lib/generators/erb/scaffold/templates/index.html.erb +27 -0
  131. data/lib/generators/erb/scaffold/templates/layout.html.erb +16 -0
  132. data/lib/generators/erb/scaffold/templates/new.html.erb +5 -0
  133. data/lib/generators/erb/scaffold/templates/show.html.erb +10 -0
  134. data/lib/generators/rails/app/USAGE +9 -0
  135. data/lib/generators/rails/app/app_generator.rb +262 -0
  136. data/lib/generators/rails/app/templates/Gemfile +34 -0
  137. data/lib/generators/rails/app/templates/README +243 -0
  138. data/lib/generators/rails/app/templates/Rakefile +10 -0
  139. data/lib/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  140. data/lib/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  141. data/lib/generators/rails/app/templates/app/models/.empty_directory +0 -0
  142. data/lib/generators/rails/app/templates/app/views/layouts/.empty_directory +0 -0
  143. data/lib/generators/rails/app/templates/config.ru +4 -0
  144. data/lib/generators/rails/app/templates/config/application.rb +51 -0
  145. data/lib/generators/rails/app/templates/config/boot.rb +17 -0
  146. data/lib/generators/rails/app/templates/config/databases/frontbase.yml +28 -0
  147. data/lib/generators/rails/app/templates/config/databases/ibm_db.yml +71 -0
  148. data/lib/generators/rails/app/templates/config/databases/mysql.yml +60 -0
  149. data/lib/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  150. data/lib/generators/rails/app/templates/config/databases/postgresql.yml +51 -0
  151. data/lib/generators/rails/app/templates/config/databases/sqlite3.yml +22 -0
  152. data/lib/generators/rails/app/templates/config/environment.rb +5 -0
  153. data/lib/generators/rails/app/templates/config/environments/development.rb.tt +19 -0
  154. data/lib/generators/rails/app/templates/config/environments/production.rb.tt +33 -0
  155. data/lib/generators/rails/app/templates/config/environments/test.rb.tt +29 -0
  156. data/lib/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  157. data/lib/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +7 -0
  158. data/lib/generators/rails/app/templates/config/initializers/inflections.rb +10 -0
  159. data/lib/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  160. data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +15 -0
  161. data/lib/generators/rails/app/templates/config/locales/en.yml +5 -0
  162. data/lib/generators/rails/app/templates/config/routes.rb +58 -0
  163. data/lib/generators/rails/app/templates/db/seeds.rb +7 -0
  164. data/lib/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  165. data/lib/generators/rails/app/templates/gitignore +4 -0
  166. data/lib/generators/rails/app/templates/public/404.html +26 -0
  167. data/lib/generators/rails/app/templates/public/422.html +26 -0
  168. data/lib/generators/rails/app/templates/public/500.html +26 -0
  169. data/lib/generators/rails/app/templates/public/favicon.ico +0 -0
  170. data/lib/generators/rails/app/templates/public/images/rails.png +0 -0
  171. data/lib/generators/rails/app/templates/public/index.html +278 -0
  172. data/lib/generators/rails/app/templates/public/javascripts/application.js +2 -0
  173. data/lib/generators/rails/app/templates/public/javascripts/controls.js +963 -0
  174. data/lib/generators/rails/app/templates/public/javascripts/dragdrop.js +973 -0
  175. data/lib/generators/rails/app/templates/public/javascripts/effects.js +1128 -0
  176. data/lib/generators/rails/app/templates/public/javascripts/prototype.js +4320 -0
  177. data/lib/generators/rails/app/templates/public/javascripts/rails.js +110 -0
  178. data/lib/generators/rails/app/templates/public/robots.txt +5 -0
  179. data/lib/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  180. data/lib/generators/rails/app/templates/script/rails +9 -0
  181. data/lib/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  182. data/lib/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  183. data/lib/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  184. data/lib/generators/rails/app/templates/test/performance/browsing_test.rb +9 -0
  185. data/lib/generators/rails/app/templates/test/test_helper.rb +13 -0
  186. data/lib/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  187. data/lib/generators/rails/controller/USAGE +18 -0
  188. data/lib/generators/rails/controller/controller_generator.rb +14 -0
  189. data/lib/generators/rails/controller/templates/controller.rb +7 -0
  190. data/lib/generators/rails/generator/USAGE +11 -0
  191. data/lib/generators/rails/generator/generator_generator.rb +25 -0
  192. data/lib/generators/rails/generator/templates/%file_name%_generator.rb.tt +5 -0
  193. data/lib/generators/rails/generator/templates/USAGE.tt +8 -0
  194. data/lib/generators/rails/generator/templates/templates/.empty_directory +0 -0
  195. data/lib/generators/rails/helper/USAGE +17 -0
  196. data/lib/generators/rails/helper/helper_generator.rb +13 -0
  197. data/lib/generators/rails/helper/templates/helper.rb +2 -0
  198. data/lib/generators/rails/integration_test/USAGE +10 -0
  199. data/lib/generators/rails/integration_test/integration_test_generator.rb +7 -0
  200. data/lib/generators/rails/mailer/USAGE +15 -0
  201. data/lib/generators/rails/mailer/mailer_generator.rb +14 -0
  202. data/lib/generators/rails/mailer/templates/mailer.rb +16 -0
  203. data/lib/generators/rails/metal/USAGE +8 -0
  204. data/lib/generators/rails/metal/metal_generator.rb +11 -0
  205. data/lib/generators/rails/metal/templates/metal.rb +12 -0
  206. data/lib/generators/rails/migration/USAGE +29 -0
  207. data/lib/generators/rails/migration/migration_generator.rb +8 -0
  208. data/lib/generators/rails/model/USAGE +30 -0
  209. data/lib/generators/rails/model/model_generator.rb +8 -0
  210. data/lib/generators/rails/model_subclass/model_subclass_generator.rb +12 -0
  211. data/lib/generators/rails/observer/USAGE +12 -0
  212. data/lib/generators/rails/observer/observer_generator.rb +7 -0
  213. data/lib/generators/rails/performance_test/USAGE +10 -0
  214. data/lib/generators/rails/performance_test/performance_test_generator.rb +7 -0
  215. data/lib/generators/rails/plugin/USAGE +13 -0
  216. data/lib/generators/rails/plugin/plugin_generator.rb +47 -0
  217. data/lib/generators/rails/plugin/templates/MIT-LICENSE.tt +20 -0
  218. data/lib/generators/rails/plugin/templates/README.tt +13 -0
  219. data/lib/generators/rails/plugin/templates/Rakefile.tt +10 -0
  220. data/lib/generators/rails/plugin/templates/init.rb +1 -0
  221. data/lib/generators/rails/plugin/templates/install.rb +1 -0
  222. data/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt +1 -0
  223. data/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +4 -0
  224. data/lib/generators/rails/plugin/templates/uninstall.rb +1 -0
  225. data/lib/generators/rails/resource/USAGE +23 -0
  226. data/lib/generators/rails/resource/resource_generator.rb +34 -0
  227. data/lib/generators/rails/scaffold/USAGE +29 -0
  228. data/lib/generators/rails/scaffold/scaffold_generator.rb +13 -0
  229. data/lib/generators/rails/scaffold_controller/USAGE +20 -0
  230. data/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +27 -0
  231. data/lib/generators/rails/scaffold_controller/templates/controller.rb +85 -0
  232. data/lib/generators/rails/session_migration/USAGE +8 -0
  233. data/lib/generators/rails/session_migration/session_migration_generator.rb +8 -0
  234. data/lib/generators/rails/stylesheets/USAGE +5 -0
  235. data/lib/generators/rails/stylesheets/stylesheets_generator.rb +9 -0
  236. data/lib/generators/rails/stylesheets/templates/scaffold.css +61 -0
  237. data/lib/generators/test_unit.rb +8 -0
  238. data/lib/generators/test_unit/controller/controller_generator.rb +14 -0
  239. data/lib/generators/test_unit/controller/templates/functional_test.rb +8 -0
  240. data/lib/generators/test_unit/helper/helper_generator.rb +13 -0
  241. data/lib/generators/test_unit/helper/templates/helper_test.rb +4 -0
  242. data/lib/generators/test_unit/integration/integration_generator.rb +13 -0
  243. data/lib/generators/test_unit/integration/templates/integration_test.rb +10 -0
  244. data/lib/generators/test_unit/mailer/mailer_generator.rb +21 -0
  245. data/lib/generators/test_unit/mailer/templates/fixture +3 -0
  246. data/lib/generators/test_unit/mailer/templates/functional_test.rb +22 -0
  247. data/lib/generators/test_unit/model/model_generator.rb +24 -0
  248. data/lib/generators/test_unit/model/templates/fixtures.yml +23 -0
  249. data/lib/generators/test_unit/model/templates/unit_test.rb +8 -0
  250. data/lib/generators/test_unit/observer/observer_generator.rb +13 -0
  251. data/lib/generators/test_unit/observer/templates/unit_test.rb +8 -0
  252. data/lib/generators/test_unit/performance/performance_generator.rb +13 -0
  253. data/lib/generators/test_unit/performance/templates/performance_test.rb +9 -0
  254. data/lib/generators/test_unit/plugin/plugin_generator.rb +13 -0
  255. data/lib/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +8 -0
  256. data/lib/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  257. data/lib/generators/test_unit/scaffold/scaffold_generator.rb +18 -0
  258. data/lib/generators/test_unit/scaffold/templates/functional_test.rb +47 -0
  259. data/lib/rails.rb +95 -0
  260. data/lib/rails/all.rb +14 -0
  261. data/lib/rails/application.rb +125 -0
  262. data/lib/rails/application/bootstrap.rb +85 -0
  263. data/lib/rails/application/configurable.rb +19 -0
  264. data/lib/rails/application/configuration.rb +86 -0
  265. data/lib/rails/application/finisher.rb +49 -0
  266. data/lib/rails/application/metal_loader.rb +50 -0
  267. data/lib/rails/application/railties.rb +31 -0
  268. data/lib/rails/application/routes_reloader.rb +46 -0
  269. data/lib/rails/backtrace_cleaner.rb +54 -0
  270. data/lib/rails/code_statistics.rb +107 -0
  271. data/lib/rails/commands.rb +72 -0
  272. data/lib/rails/commands/application.rb +13 -0
  273. data/lib/rails/commands/console.rb +55 -0
  274. data/lib/rails/commands/dbconsole.rb +104 -0
  275. data/lib/rails/commands/destroy.rb +9 -0
  276. data/lib/rails/commands/generate.rb +9 -0
  277. data/lib/rails/commands/performance/benchmarker.rb +23 -0
  278. data/lib/rails/commands/performance/profiler.rb +46 -0
  279. data/lib/rails/commands/plugin.rb +542 -0
  280. data/lib/rails/commands/runner.rb +51 -0
  281. data/lib/rails/commands/server.rb +75 -0
  282. data/lib/rails/commands/update.rb +9 -0
  283. data/lib/rails/configuration.rb +191 -0
  284. data/lib/rails/console/app.rb +33 -0
  285. data/lib/rails/console/helpers.rb +5 -0
  286. data/lib/rails/console/sandbox.rb +6 -0
  287. data/lib/rails/deprecation.rb +62 -0
  288. data/lib/rails/dispatcher.rb +24 -0
  289. data/lib/rails/engine.rb +130 -0
  290. data/lib/rails/engine/configurable.rb +25 -0
  291. data/lib/rails/engine/configuration.rb +49 -0
  292. data/lib/rails/generators.rb +289 -0
  293. data/lib/rails/generators/actions.rb +309 -0
  294. data/lib/rails/generators/active_model.rb +78 -0
  295. data/lib/rails/generators/base.rb +362 -0
  296. data/lib/rails/generators/generated_attribute.rb +48 -0
  297. data/lib/rails/generators/migration.rb +64 -0
  298. data/lib/rails/generators/named_base.rb +95 -0
  299. data/lib/rails/generators/resource_helpers.rb +80 -0
  300. data/lib/rails/generators/test_case.rb +239 -0
  301. data/lib/rails/initializable.rb +97 -0
  302. data/lib/rails/paths.rb +144 -0
  303. data/lib/rails/performance_test_help.rb +3 -0
  304. data/lib/rails/plugin.rb +64 -0
  305. data/lib/rails/rack.rb +8 -0
  306. data/lib/rails/rack/debugger.rb +23 -0
  307. data/lib/rails/rack/log_tailer.rb +33 -0
  308. data/lib/rails/rack/logger.rb +34 -0
  309. data/lib/rails/rack/static.rb +5 -0
  310. data/lib/rails/railtie.rb +74 -0
  311. data/lib/rails/railtie/configurable.rb +23 -0
  312. data/lib/rails/railtie/configuration.rb +9 -0
  313. data/lib/rails/railties_path.rb +1 -0
  314. data/lib/rails/ruby_version_check.rb +10 -0
  315. data/lib/rails/rubyprof_ext.rb +35 -0
  316. data/lib/rails/source_annotation_extractor.rb +102 -0
  317. data/lib/rails/subscriber.rb +108 -0
  318. data/lib/rails/subscriber/test_helper.rb +98 -0
  319. data/lib/rails/tasks.rb +16 -0
  320. data/lib/rails/tasks/annotations.rake +20 -0
  321. data/lib/rails/tasks/documentation.rake +96 -0
  322. data/lib/rails/tasks/framework.rake +69 -0
  323. data/lib/rails/tasks/log.rake +9 -0
  324. data/lib/rails/tasks/middleware.rake +7 -0
  325. data/lib/rails/tasks/misc.rake +71 -0
  326. data/lib/rails/tasks/routes.rake +17 -0
  327. data/lib/rails/tasks/statistics.rake +16 -0
  328. data/lib/rails/tasks/tmp.rake +37 -0
  329. data/lib/rails/test_help.rb +30 -0
  330. data/lib/rails/test_unit/railtie.rb +17 -0
  331. data/lib/rails/test_unit/testing.rake +130 -0
  332. data/lib/rails/version.rb +9 -0
  333. data/lib/rails/webrick_server.rb +156 -0
  334. metadata +427 -0
@@ -0,0 +1,1835 @@
1
+ h2. Active Support Core Extensions
2
+
3
+ Active Support is the Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff. It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Rails itself.
4
+
5
+ By referring to this guide you will learn the extensions to the Ruby core classes and modules provided by Rails.
6
+
7
+ endprologue.
8
+
9
+ h3. Extensions to All Objects
10
+
11
+ h4. +blank?+ and +present?+
12
+
13
+ The following values are considered to be blank in a Rails application:
14
+
15
+ * +nil+ and +false+,
16
+
17
+ * strings composed only of whitespace, i.e. matching +/\A\s*\z/+,
18
+
19
+ * empty arrays and hashes, and
20
+
21
+ * any other object that responds to +empty?+ and it is empty.
22
+
23
+ WARNING: Note that numbers are not mentioned, in particular 0 and 0.0 are *not* blank.
24
+
25
+ For example, this method from +ActionDispatch::Response+ uses +blank?+ to easily be robust to +nil+ and whitespace strings in one shot:
26
+
27
+ <ruby>
28
+ def charset
29
+ charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
30
+ charset.blank? ? nil : charset.strip.split("=")[1]
31
+ end
32
+ </ruby>
33
+
34
+ That's a typical use case for +blank?+.
35
+
36
+ Here, the method Rails runs to instantiate observers upon initialization has nothing to do if there are none:
37
+
38
+ <ruby>
39
+ def instantiate_observers
40
+ return if @observers.blank?
41
+ # ...
42
+ end
43
+ </ruby>
44
+
45
+ The method +present?+ is equivalent to +!blank?+:
46
+
47
+ <ruby>
48
+ assert @response.body.present? # same as !@response.body.blank?
49
+ </ruby>
50
+
51
+ NOTE: Defined in +active_support/core_ext/object/blank.rb+.
52
+
53
+ h4. +presence+
54
+
55
+ The +presence+ method returns its receiver if +present?+, and +nil+ otherwise. It is useful for idioms like this:
56
+
57
+ <ruby>
58
+ host = config[:host].presence || 'localhost'
59
+ </ruby>
60
+
61
+ NOTE: Defined in +active_support/core_ext/object/blank.rb+.
62
+
63
+ h4. +duplicable?+
64
+
65
+ A few fundamental objects in Ruby are singletons. For example, in the whole live of a program the integer 1 refers always to the same instance:
66
+
67
+ <ruby>
68
+ 1.object_id # => 3
69
+ Math.cos(0).to_i.object_id # => 3
70
+ </ruby>
71
+
72
+ Hence, there's no way these objects can be duplicated through +dup+ or +clone+:
73
+
74
+ <ruby>
75
+ true.dup # => TypeError: can't dup TrueClass
76
+ </ruby>
77
+
78
+ Some numbers which are not singletons are not duplicable either:
79
+
80
+ <ruby>
81
+ 0.0.clone # => allocator undefined for Float
82
+ (2**1024).clone # => allocator undefined for Bignum
83
+ </ruby>
84
+
85
+ Active Support provides +duplicable?+ to programmatically query an object about this property:
86
+
87
+ <ruby>
88
+ "".duplicable? # => true
89
+ false.duplicable? # => false
90
+ </ruby>
91
+
92
+ By definition all objects are +duplicable?+ except +nil+, +false+, +true+, symbols, numbers, and class objects.
93
+
94
+ WARNING. Using +duplicable?+ is discouraged because it depends on a hard-coded list. Classes have means to disallow duplication like removing +dup+ and +clone+ or raising exceptions from them, only +rescue+ can tell.
95
+
96
+ NOTE: Defined in +active_support/core_ext/object/duplicable.rb+.
97
+
98
+ h4. +returning+
99
+
100
+ The method +returning+ yields its argument to a block and returns it. You tipically use it with a mutable object that gets modified in the block:
101
+
102
+ <ruby>
103
+ def html_options_for_form(url_for_options, options, *parameters_for_url)
104
+ returning options.stringify_keys do |html_options|
105
+ html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
106
+ html_options["action"] = url_for(url_for_options, *parameters_for_url)
107
+ end
108
+ end
109
+ </ruby>
110
+
111
+ NOTE: Defined in +active_support/core_ext/object/returning.rb+.
112
+
113
+ h4. +try+
114
+
115
+ Sometimes you want to call a method provided the receiver object is not +nil+, which is something you usually check first.
116
+
117
+ For instance, note how this method of +ActiveRecord::ConnectionAdapters::AbstractAdapter+ checks if there's a +@logger+:
118
+
119
+ <ruby>
120
+ def log_info(sql, name, ms)
121
+ if @logger && @logger.debug?
122
+ name = '%s (%.1fms)' % [name || 'SQL', ms]
123
+ @logger.debug(format_log_entry(name, sql.squeeze(' ')))
124
+ end
125
+ end
126
+ </ruby>
127
+
128
+ You can shorten that using +Object#try+. This method is a synonim for +Object#send+ except that it returns +nil+ if sent to +nil+. The previous example could then be rewritten as:
129
+
130
+ <ruby>
131
+ def log_info(sql, name, ms)
132
+ if @logger.try(:debug?)
133
+ name = '%s (%.1fms)' % [name || 'SQL', ms]
134
+ @logger.debug(format_log_entry(name, sql.squeeze(' ')))
135
+ end
136
+ end
137
+ </ruby>
138
+
139
+ NOTE: Defined in +active_support/core_ext/object/try.rb+.
140
+
141
+ h4. +metaclass+
142
+
143
+ The method +metaclass+ returns the singleton class on any object:
144
+
145
+ <ruby>
146
+ String.metaclass # => #<Class:String>
147
+ String.new.metaclass # => #<Class:#<String:0x17a1d1c>>
148
+ </ruby>
149
+
150
+ NOTE: Defined in +active_support/core_ext/object/metaclass.rb+.
151
+
152
+ h4. +class_eval(*args, &block)+
153
+
154
+ You can evaluate code in the context of any object's singleton class using +class_eval+:
155
+
156
+ <ruby>
157
+ class Proc
158
+ def bind(object)
159
+ block, time = self, Time.now
160
+ object.class_eval do
161
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
162
+ define_method(method_name, &block)
163
+ method = instance_method(method_name)
164
+ remove_method(method_name)
165
+ method
166
+ end.bind(object)
167
+ end
168
+ end
169
+ </ruby>
170
+
171
+ NOTE: Defined in +active_support/core_ext/object/metaclass.rb+.
172
+
173
+ h4. +acts_like?(duck)+
174
+
175
+ The method +acts_like+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines
176
+
177
+ <ruby>
178
+ def acts_like_string?
179
+ end
180
+ </ruby>
181
+
182
+ which is only a marker, its body or return value are irrelevant. Then, client code can query for duck-type-safeness this way:
183
+
184
+ <ruby>
185
+ some_klass.acts_like?(:string)
186
+ </ruby>
187
+
188
+ Rails has classes that act like +Date+ or +Time+ and follow this contract.
189
+
190
+ NOTE: Defined in +active_support/core_ext/object/acts_like.rb+.
191
+
192
+ h4. +to_param+
193
+
194
+ All objects in Rails respond to the method +to_param+, which is meant to return something that represents them as values in a query string, or as a URL fragments.
195
+
196
+ By default +to_param+ just calls +to_s+:
197
+
198
+ <ruby>
199
+ 7.to_param # => "7"
200
+ </ruby>
201
+
202
+ The return value of +to_param+ should *not* be escaped:
203
+
204
+ <ruby>
205
+ "Tom & Jerry".to_param # => "Tom & Jerry"
206
+ </ruby>
207
+
208
+ Several classes in Rails overwrite this method.
209
+
210
+ For example +nil+, +true+, and +false+ return themselves. +Array#to_param+ calls +to_param+ on the elements and joins the result with "/":
211
+
212
+ <ruby>
213
+ [0, true, String].to_param # => "0/true/String"
214
+ </ruby>
215
+
216
+ Notably, the Rails routing system calls +to_param+ on models to get a value for the +:id+ placeholder. +ActiveRecord::Base#to_param+ returns the +id+ of a model, but you can redefine that method in your models. For example, given
217
+
218
+ <ruby>
219
+ class User
220
+ def to_param
221
+ "#{id}-#{name.parameterize}"
222
+ end
223
+ end
224
+ </ruby>
225
+
226
+ we get:
227
+
228
+ <ruby>
229
+ user_path(@user) # => "/users/357-john-smith"
230
+ </ruby>
231
+
232
+ WARNING. Controllers need to be aware of any redifinition of +to_param+ because when a request like that comes in "357-john-smith" is the value of +params[:id]+.
233
+
234
+ NOTE: Defined in +active_support/core_ext/object/to_param.rb+.
235
+
236
+ h4. +to_query+
237
+
238
+ Except for hashes, given an unescaped +key+ this method constructs the part of a query string that would map such key to what +to_param+ returns. For example, given
239
+
240
+ <ruby>
241
+ class User
242
+ def to_param
243
+ "#{id}-#{name.parameterize}"
244
+ end
245
+ end
246
+ </ruby>
247
+
248
+ we get:
249
+
250
+ <ruby>
251
+ current_user.to_query('user') # => user=357-john-smith
252
+ </ruby>
253
+
254
+ This method escapes whatever is needed, both for the key and the value:
255
+
256
+ <ruby>
257
+ account.to_query('company[name]')
258
+ # => "company%5Bname%5D=Johnson+%26+Johnson"
259
+ </ruby>
260
+
261
+ so its output is ready to be used in a query string.
262
+
263
+ Arrays return the result of applying +to_query+ to each element with <tt>_key_[]</tt> as key, and join the result with "&":
264
+
265
+ <ruby>
266
+ [3.4, -45.6].to_query('sample')
267
+ # => "sample%5B%5D=3.4&sample%5B%5D=-45.6"
268
+ </ruby>
269
+
270
+ Hashes also respond to +to_query+ but with a different signature. If no argument is passed a call generates a sorted series of key/value assigments calling +to_query(key)+ on its values. Then it joins the result with "&":
271
+
272
+ <ruby>
273
+ {:c => 3, :b => 2, :a => 1}.to_query # => "a=1&b=2&c=3"
274
+ </ruby>
275
+
276
+ The method +Hash#to_query+ accepts an optional namespace for the keys:
277
+
278
+ <ruby>
279
+ {:id => 89, :name => "John Smith"}.to_query('user')
280
+ # => "user%5Bid%5D=89&user%5Bname%5D=John+Smith"
281
+ </ruby>
282
+
283
+ NOTE: Defined in +active_support/core_ext/object/to_query.rb+.
284
+
285
+ h4. +with_options+
286
+
287
+ The method +with_options+ provides a way to factor out common options in a series of method calls.
288
+
289
+ Given a default options hash, +with_options+ yields a proxy object to a block. Within the block, methods called on the proxy are forwarded to the receiver with their options merged. For example, you get rid of the duplication in:
290
+
291
+ <ruby>
292
+ class Account < ActiveRecord::Base
293
+ has_many :customers, :dependent => :destroy
294
+ has_many :products, :dependent => :destroy
295
+ has_many :invoices, :dependent => :destroy
296
+ has_many :expenses, :dependent => :destroy
297
+ end
298
+ </ruby>
299
+
300
+ this way:
301
+
302
+ <ruby>
303
+ class Account < ActiveRecord::Base
304
+ with_options :dependent => :destroy do |assoc|
305
+ assoc.has_many :customers
306
+ assoc.has_many :products
307
+ assoc.has_many :invoices
308
+ assoc.has_many :expenses
309
+ end
310
+ end
311
+ </ruby>
312
+
313
+ That idiom may convey _grouping_ to the reader as well. For example, say you want to send a newsletter whose language depends on the user. Somewhere in the mailer you could group locale-dependent bits like this:
314
+
315
+ <ruby>
316
+ I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|
317
+ subject i18n.t :subject
318
+ body i18n.t :body, :user_name => user.name
319
+ end
320
+ </ruby>
321
+
322
+ TIP: Since +with_options+ forwards calls to its receiver they can be nested. Each nesting level will merge inherited defaults in addition to their own.
323
+
324
+ NOTE: Defined in +active_support/core_ext/object/with_options.rb+.
325
+
326
+ h4. Instance Variables
327
+
328
+ Active Support provides several methods to ease access to instance variables.
329
+
330
+ h5. +instance_variable_names+
331
+
332
+ Ruby 1.8 and 1.9 have a method called +instance_variables+ that returns the names of the defined instance variables. But they behave differently, in 1.8 it returns strings whereas in 1.9 it returns symbols. Active Support defines +instance_variable_names+ as a portable way to obtain them as strings:
333
+
334
+ <ruby>
335
+ class C
336
+ def initialize(x, y)
337
+ @x, @y = x, y
338
+ end
339
+ end
340
+
341
+ C.new(0, 1).instance_variable_names # => ["@y", "@x"]
342
+ </ruby>
343
+
344
+ WARNING: The order in which the names are returned is unespecified, and it indeed depends on the version of the interpreter.
345
+
346
+ NOTE: Defined in +active_support/core_ext/object/instance_variables.rb+.
347
+
348
+ h5. +instance_values+
349
+
350
+ The method +instance_values+ returns a hash that maps instance variable names without "@" to their
351
+ corresponding values. Keys are strings both in Ruby 1.8 and 1.9:
352
+
353
+ <ruby>
354
+ class C
355
+ def initialize(x, y)
356
+ @x, @y = x, y
357
+ end
358
+ end
359
+
360
+ C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
361
+ </ruby>
362
+
363
+ NOTE: Defined in +active_support/core_ext/object/instance_variables.rb+.
364
+
365
+ h5. +copy_instance_variables_from(object, exclude = [])+
366
+
367
+ Copies the instance variables of +object+ into +self+.
368
+
369
+ Instance variable names in the +exclude+ array are ignored. If +object+
370
+ responds to +protected_instance_variables+ the ones returned are
371
+ also ignored. For example, Rails controllers implement that method.
372
+
373
+ In both arrays strings and symbols are understood, and they have to include
374
+ the at sign.
375
+
376
+ <ruby>
377
+ class C
378
+ def initialize(x, y, z)
379
+ @x, @y, @z = x, y, z
380
+ end
381
+
382
+ def protected_instance_variables
383
+ %w(@z)
384
+ end
385
+ end
386
+
387
+ a = C.new(0, 1, 2)
388
+ b = C.new(3, 4, 5)
389
+
390
+ a.copy_instance_variables_from(b, [:@y])
391
+ # a is now: @x = 3, @y = 1, @z = 2
392
+ </ruby>
393
+
394
+ In the example +object+ and +self+ are of the same type, but they don't need to.
395
+
396
+ NOTE: Defined in +active_support/core_ext/object/instance_variables.rb+.
397
+
398
+ h4. Silencing Warnings, Streams, and Exceptions
399
+
400
+ The methods +silence_warnings+ and +enable_warnings+ change the value of +$VERBOSE+ accordingly for the duration of their block, and reset it afterwards:
401
+
402
+ <ruby>
403
+ silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
404
+ </ruby>
405
+
406
+ You can silence any stream while a block runs with +silence_stream+:
407
+
408
+ <ruby>
409
+ silence_stream(STDOUT) do
410
+ # STDOUT is silent here
411
+ end
412
+ </ruby>
413
+
414
+ Silencing exceptions is also possible with +suppress+. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is +kind_of?+ any of the arguments, +suppress+ captures it and returns silently. Otherwise the exception is reraised:
415
+
416
+ <ruby>
417
+ # If the user is locked the increment is lost, no big deal.
418
+ suppress(ActiveRecord::StaleObjectError) do
419
+ current_user.increment! :visits
420
+ end
421
+ </ruby>
422
+
423
+ NOTE: Defined in +active_support/core_ext/kernel/reporting.rb+.
424
+
425
+ h3. Extensions to +Module+
426
+
427
+ h4. Aliasing
428
+
429
+ h5. +alias_method_chain+
430
+
431
+ Using plain Ruby you can wrap methods with other methods, that's called _alias chaining_.
432
+
433
+ For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap +ActionController::TestCase#process+ this way in +test/test_helper.rb+:
434
+
435
+ <ruby>
436
+ ActionController::TestCase.class_eval do
437
+ # save a reference to the original process method
438
+ alias_method :original_process, :process
439
+
440
+ # now redefine process and delegate to original_process
441
+ def process(action, params=nil, session=nil, flash=nil, http_method='GET')
442
+ params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
443
+ original_process(action, params, session, flash, http_method)
444
+ end
445
+ end
446
+ </ruby>
447
+
448
+ That's the method +get+, +post+, etc., delegate the work to.
449
+
450
+ That technique has a risk, it could be the case that +:original_process+ was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about:
451
+
452
+ <ruby>
453
+ ActionController::TestCase.class_eval do
454
+ def process_with_stringified_params(...)
455
+ params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
456
+ process_without_stringified_params(action, params, session, flash, http_method)
457
+ end
458
+ alias_method :process_without_stringified_params, :process
459
+ alias_method :process, :process_with_stringified_params
460
+ end
461
+ </ruby>
462
+
463
+ The method +alias_method_chain+ provides a shortcut for that pattern:
464
+
465
+ <ruby>
466
+ ActionController::TestCase.class_eval do
467
+ def process_with_stringified_params(...)
468
+ params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
469
+ process_without_stringified_params(action, params, session, flash, http_method)
470
+ end
471
+ alias_method_chain :process, :stringified_params
472
+ end
473
+ </ruby>
474
+
475
+ Rails uses +alias_method_chain+ all over the code base. For example validations are added to +ActiveRecord::Base#save+ by wrapping the method that way in a separate module specialised in validations.
476
+
477
+ NOTE: Defined in +active_support/core_ext/module/aliasing.rb+.
478
+
479
+ h5. +alias_attribute+
480
+
481
+ Model attributes have a reader, a writer, and a predicate. You can aliase a model attribute having the corresponding three methods defined for you in one shot. As in other aliasing methods, the new name is the first argument, and the old name is the second (my mnemonic is they go in the same order as if you did an assignment):
482
+
483
+ <ruby>
484
+ class User < ActiveRecord::Base
485
+ # let me refer to the email column as "login",
486
+ # much meaningful for authentication code
487
+ alias_attribute :login, :email
488
+ end
489
+ </ruby>
490
+
491
+ NOTE: Defined in +active_support/core_ext/module/aliasing.rb+.
492
+
493
+ h4. Delegation
494
+
495
+ The class method +delegate+
496
+
497
+ h3. Extensions to +Class+
498
+
499
+ h4. Class Attribute Accessors
500
+
501
+ The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it:
502
+
503
+ <ruby>
504
+ class MysqlAdapter < AbstractAdapter
505
+ # Generates class methods to access @@emulate_booleans.
506
+ cattr_accessor :emulate_booleans
507
+ self.emulate_booleans = true
508
+ end
509
+ </ruby>
510
+
511
+ Instance methods are created as well for convenience. For example given
512
+
513
+ <ruby>
514
+ module ActionController
515
+ class Base
516
+ cattr_accessor :logger
517
+ end
518
+ end
519
+ </ruby>
520
+
521
+ we can access +logger+ in actions. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
522
+
523
+ <ruby>
524
+ module ActiveRecord
525
+ class Base
526
+ # No pluralize_table_names= instance writer is generated.
527
+ cattr_accessor :pluralize_table_names, :instance_writer => false
528
+ end
529
+ end
530
+ </ruby>
531
+
532
+ NOTE: Defined in +active_support/core_ext/class/attribute_accessors.rb+.
533
+
534
+ h4. Class Inheritable Attributes
535
+
536
+ Class variables are shared down the inheritance tree. Class instance variables are not shared, but they are not inherited either. The macros +class_inheritable_reader+, +class_inheritable_writer+, and +class_inheritable_accessor+ provide accesors for class-level data which is inherited but not shared with children:
537
+
538
+ <ruby>
539
+ module ActionController
540
+ class Base
541
+ # FIXME: REVISE/SIMPLIFY THIS COMMENT.
542
+ # The value of allow_forgery_protection is inherited,
543
+ # but its value in a particular class does not affect
544
+ # the value in the rest of the controllers hierarchy.
545
+ class_inheritable_accessor :allow_forgery_protection
546
+ end
547
+ end
548
+ </ruby>
549
+
550
+ They accomplish this with class instance variables and cloning on subclassing, there are no class variables involved. Cloning is performed with +dup+ as long as the value is duplicable.
551
+
552
+ There are some variants specialised in arrays and hashes:
553
+
554
+ <ruby>
555
+ class_inheritable_array
556
+ class_inheritable_hash
557
+ </ruby>
558
+
559
+ Those writers take any inherited array or hash into account and extend them rather than overwrite them.
560
+
561
+ As with vanilla class attribute accessors these macros create convenience instance methods for reading and writing. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
562
+
563
+ <ruby>
564
+ module ActiveRecord
565
+ class Base
566
+ class_inheritable_accessor :default_scoping, :instance_writer => false
567
+ end
568
+ end
569
+ </ruby>
570
+
571
+ Since values are copied when a subclass is defined, if the base class changes the attribute after that, the subclass does not see the new value. That's the point.
572
+
573
+ NOTE: Defined in +active_support/core_ext/class/inheritable_attributes.rb+.
574
+
575
+ There's a related macro called +superclass_delegating_accessor+, however, that does not copy the value when the base class is subclassed. Instead, it delegates reading to the superclass as long as the attribute is not set via its own writer. For example, +ActionMailer::Base+ defines +delivery_method+ this way:
576
+
577
+ <ruby>
578
+ module ActionMailer
579
+ class Base
580
+ superclass_delegating_accessor :delivery_method
581
+ self.delivery_method = :smtp
582
+ end
583
+ end
584
+ </ruby>
585
+
586
+ If for whatever reason an application loads the definition of a mailer class and after that sets +ActionMailer::Base.delivery_method+, the mailer class will still see the new value. In addition, the mailer class is able to change the +delivery_method+ without affecting the value in the parent using its own inherited class attribute writer.
587
+
588
+ NOTE: Defined in +active_support/core_ext/class/delegating_attributes.rb+.
589
+
590
+ h4. Subclasses
591
+
592
+ The +subclasses+ method returns the names of all subclasses of a given class as an array of strings. That comprises not only direct subclasses, but all descendants down the hierarchy:
593
+
594
+ <ruby>
595
+ class C; end
596
+ C.subclasses # => []
597
+
598
+ Integer.subclasses # => ["Bignum", "Fixnum"]
599
+
600
+ module M
601
+ class A; end
602
+ class B1 < A; end
603
+ class B2 < A; end
604
+ end
605
+
606
+ module N
607
+ class C < M::B1; end
608
+ end
609
+
610
+ M::A.subclasses # => ["N::C", "M::B2", "M::B1"]
611
+ </ruby>
612
+
613
+ The order in which these class names are returned is unspecified.
614
+
615
+ See also +Object#subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME.
616
+
617
+ NOTE: Defined in +active_support/core_ext/class/removal.rb+.
618
+
619
+ h4. Class Removal
620
+
621
+ Roughly speaking, the +remove_class+ method removes the class objects passed as arguments:
622
+
623
+ <ruby>
624
+ Class.remove_class(Hash, Dir) # => [Hash, Dir]
625
+ Hash # => NameError: uninitialized constant Hash
626
+ Dir # => NameError: uninitialized constant Dir
627
+ </ruby>
628
+
629
+ More specifically, +remove_class+ attempts to remove constants with the same name as the passed class objects from their parent modules. So technically this method does not guarantee the class objects themselves are not still valid and alive somewhere after the method call:
630
+
631
+ <ruby>
632
+ module M
633
+ class A; end
634
+ class B < A; end
635
+ end
636
+
637
+ A2 = M::A
638
+
639
+ M::A.object_id # => 13053950
640
+ Class.remove_class(M::A)
641
+
642
+ M::B.superclass.object_id # => 13053950 (same object as before)
643
+ A2.name # => "M::A" (name is hard-coded in object)
644
+ </ruby>
645
+
646
+ WARNING: Removing fundamental classes like +String+ can result in really funky behaviour.
647
+
648
+ The method +remove_subclasses+ provides a shortcut for removing all descendants of a given class, where "removing" has the meaning explained above:
649
+
650
+ <ruby>
651
+ class A; end
652
+ class B1 < A; end
653
+ class B2 < A; end
654
+ class C < A; end
655
+
656
+ A.subclasses # => ["C", "B2", "B1"]
657
+ A.remove_subclasses
658
+ A.subclasses # => []
659
+ C # => NameError: uninitialized constant C
660
+ </ruby>
661
+
662
+ See also +Object#remove_subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME.
663
+
664
+ NOTE: Defined in +active_support/core_ext/class/removal.rb+.
665
+
666
+ h3. Extensions to +String+
667
+
668
+ h4. +squish+
669
+
670
+ The method +String#squish+ strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:
671
+
672
+ <ruby>
673
+ " \n foo\n\r \t bar \n".squish # => "foo bar"
674
+ </ruby>
675
+
676
+ There's also the destructive version +String#squish!+.
677
+
678
+ NOTE: Defined in +active_support/core_ext/string/filters.rb+.
679
+
680
+ h4. Key-based Interpolation
681
+
682
+ In Ruby 1.9 the <tt>%</tt> string operator supports key-based interpolation, both formatted and unformatted:
683
+
684
+ <ruby>
685
+ "Total is %<total>.02f" % {:total => 43.1} # => Total is 43.10
686
+ "I say %{foo}" % {:foo => "wadus"} # => "I say wadus"
687
+ "I say %{woo}" % {:foo => "wadus"} # => KeyError
688
+ </ruby>
689
+
690
+ Active Support adds that functionality to <tt>%</tt> in previous versions of Ruby.
691
+
692
+ NOTE: Defined in +active_support/core_ext/string/interpolation.rb+.
693
+
694
+ h4. +starts_with?+ and +ends_width?+
695
+
696
+ Active Support defines 3rd person aliases of +String#start_with?+ and +String#end_with?+:
697
+
698
+ <ruby>
699
+ "foo".starts_with?("f") # => true
700
+ "foo".ends_with?("o") # => true
701
+ </ruby>
702
+
703
+ NOTE: Defined in +active_support/core_ext/string/starts_ends_with.rb+.
704
+
705
+ h4. Access
706
+
707
+ h5. +at(position)+
708
+
709
+ Returns the character of the string at position +position+:
710
+
711
+ <ruby>
712
+ "hello".at(0) # => "h"
713
+ "hello".at(4) # => "o"
714
+ "hello".at(-1) # => "o"
715
+ "hello".at(10) # => ERROR if < 1.9, nil in 1.9
716
+ </ruby>
717
+
718
+ NOTE: Defined in +active_support/core_ext/string/access.rb+.
719
+
720
+ h5. +from(position)+
721
+
722
+ Returns the substring of the string starting at position +position+:
723
+
724
+ <ruby>
725
+ "hello".from(0) # => "hello"
726
+ "hello".from(2) # => "llo"
727
+ "hello".from(-2) # => "lo"
728
+ "hello".from(10) # => "" if < 1.9, nil in 1.9
729
+ </ruby>
730
+
731
+ NOTE: Defined in +active_support/core_ext/string/access.rb+.
732
+
733
+ h5. +to(position)+
734
+
735
+ Returns the substring of the string up to position +position+:
736
+
737
+ <ruby>
738
+ "hello".to(0) # => "h"
739
+ "hello".to(2) # => "hel"
740
+ "hello".to(-2) # => "hell"
741
+ "hello".to(10) # => "hello"
742
+ </ruby>
743
+
744
+ NOTE: Defined in +active_support/core_ext/string/access.rb+.
745
+
746
+ h5. +first(limit = 1)+
747
+
748
+ The call +str.first(n)+ is equivalent to +str.to(n-1)+ if +n+ > 0, and returns an empty string for +n+ == 0.
749
+
750
+ NOTE: Defined in +active_support/core_ext/string/access.rb+.
751
+
752
+ h5. +last(limit = 1)+
753
+
754
+ The call +str.last(n)+ is equivalent to +str.from(-n)+ if +n+ > 0, and returns an empty string for +n+ == 0.
755
+
756
+ NOTE: Defined in +active_support/core_ext/string/access.rb+.
757
+
758
+ h3. Extensions to +Numeric+
759
+
760
+ h4. Bytes
761
+
762
+ All numbers respond to these methods:
763
+
764
+ <ruby>
765
+ bytes
766
+ kilobytes
767
+ megabytes
768
+ gigabytes
769
+ terabytes
770
+ petabytes
771
+ exabytes
772
+ </ruby>
773
+
774
+ They return the corresponding amount of bytes, using a conversion factor of 1024:
775
+
776
+ <ruby>
777
+ 2.kilobytes # => 2048
778
+ 3.megabytes # => 3145728
779
+ 3.5.gigabytes # => 3758096384
780
+ -4.exabytes # => -4611686018427387904
781
+ </ruby>
782
+
783
+ Singular forms are aliased so you are able to say:
784
+
785
+ <ruby>
786
+ 1.megabyte # => 1048576
787
+ </ruby>
788
+
789
+ NOTE: Defined in +active_support/core_ext/numeric/bytes.rb+.
790
+
791
+ h3. Extensions to +Integer+
792
+
793
+ h4. +multiple_of?+
794
+
795
+ The method +multiple_of?+ tests whether an integer is multiple of the argument:
796
+
797
+ <ruby>
798
+ 2.multiple_of?(1) # => true
799
+ 1.multiple_of?(2) # => false
800
+ </ruby>
801
+
802
+ NOTE: Defined in +active_support/core_ext/integer/multiple.rb+.
803
+
804
+ h4. +ordinalize+
805
+
806
+ The method +ordinalize+ returns the ordinal string corresponding to the receiver integer:
807
+
808
+ <ruby>
809
+ 1.ordinalize # => "1st"
810
+ 2.ordinalize # => "2nd"
811
+ 53.ordinalize # => "53rd"
812
+ 2009.ordinalize # => "2009th"
813
+ </ruby>
814
+
815
+ NOTE: Defined in +active_support/core_ext/integer/inflections.rb+.
816
+
817
+ h3. Extensions to +Float+
818
+
819
+ ...
820
+
821
+ h3. Extensions to +BigDecimal+
822
+
823
+ ...
824
+
825
+ h3. Extensions to +Enumerable+
826
+
827
+ h4. +group_by+
828
+
829
+ Ruby 1.8.7 and up define +group_by+, and Active Support does it for previous versions.
830
+
831
+ This iterator takes a block and builds an ordered hash with its return values as keys. Each key is mapped to the array of elements for which the block returned that value:
832
+
833
+ <ruby>
834
+ entries_by_surname_initial = address_book.group_by do |entry|
835
+ entry.surname.at(0).upcase
836
+ end
837
+ </ruby>
838
+
839
+ WARNING. Active Support redefines +group_by+ in Ruby 1.8.7 so that it still returns an ordered hash.
840
+
841
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
842
+
843
+ h4. +sum+
844
+
845
+ The method +sum+ adds the elements of an enumerable:
846
+
847
+ <ruby>
848
+ [1, 2, 3].sum # => 6
849
+ (1..100).sum # => 5050
850
+ </ruby>
851
+
852
+ Addition only assumes the elements respond to <tt>+</tt>:
853
+
854
+ <ruby>
855
+ [[1, 2], [2, 3], [3, 4]].sum # => [1, 2, 2, 3, 3, 4]
856
+ %w(foo bar baz).sum # => "foobarbaz"
857
+ {:a => 1, :b => 2, :c => 3}.sum # => [:b, 2, :c, 3, :a, 1]
858
+ </ruby>
859
+
860
+ The sum of an empty collection is zero by default, but this is customizable:
861
+
862
+ <ruby>
863
+ [].sum # => 0
864
+ [].sum(1) # => 1
865
+ </ruby>
866
+
867
+ If a block is given +sum+ becomes an iterator that yields the elements of the collection and sums the returned values:
868
+
869
+ <ruby>
870
+ (1..5).sum {|n| n * 2 } # => 30
871
+ [2, 4, 6, 8, 10].sum # => 30
872
+ </ruby>
873
+
874
+ The sum of an empty receiver can be customized in this form as well:
875
+
876
+ <ruby>
877
+ [].sum(1) {|n| n**3} # => 1
878
+ </ruby>
879
+
880
+ The method +ActiveRecord::Observer#observed_subclasses+ for example is implemented this way:
881
+
882
+ <ruby>
883
+ def observed_subclasses
884
+ observed_classes.sum([]) { |klass| klass.send(:subclasses) }
885
+ end
886
+ </ruby>
887
+
888
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
889
+
890
+ h4. +each_with_object+
891
+
892
+ The +inject+ method offers iteration with an accumulator:
893
+
894
+ <ruby>
895
+ [2, 3, 4].inject(1) {|acc, i| product*i } # => 24
896
+ </ruby>
897
+
898
+ The block is expected to return the value for the accumulator in the next iteration, and this makes building mutable objects a bit cumbersome:
899
+
900
+ <ruby>
901
+ [1, 2].inject({}) {|h, i| h[i] = i**2; h} # => {1 => 1, 2 => 4}
902
+ </ruby>
903
+
904
+ See that spurious "+; h+"?
905
+
906
+ Active Support backports +each_with_object+ from Ruby 1.9, which addresses that use case. It iterates over the collection, passes the accumulator, and returns the accumulator when done. You normally modify the accumulator in place. The example above would be written this way:
907
+
908
+ <ruby>
909
+ [1, 2].each_with_object({}) {|i, h| h[i] = i**2} # => {1 => 1, 2 => 4}
910
+ </ruby>
911
+
912
+ WARNING. Note that the item of the collection and the accumulator come in different order in +inject+ and +each_with_object+.
913
+
914
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
915
+
916
+ h4. +index_by+
917
+
918
+ The method +index_by+ generates a hash with the elements of an enumerable indexed by some key.
919
+
920
+ It iterates through the collection and passes each element to a block. The element will be keyed by the value returned by the block:
921
+
922
+ <ruby>
923
+ invoices.index_by(&:number)
924
+ # => {'2009-032' => <Invoice ...>, '2009-008' => <Invoice ...>, ...}
925
+ </ruby>
926
+
927
+ WARNING. Keys should normally be unique. If the block returns the same value for different elements no collection is built for that key. The last item will win.
928
+
929
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
930
+
931
+ h4. +many?+
932
+
933
+ The method +many?+ is shorthand for +collection.size > 1+:
934
+
935
+ <erb>
936
+ <% if pages.many? %>
937
+ <%= pagination_links %>
938
+ <% end %>
939
+ </erb>
940
+
941
+ If an optional block is given +many?+ only takes into account those elements that return true:
942
+
943
+ <ruby>
944
+ @see_more = videos.many? {|video| video.category == params[:category]}
945
+ </ruby>
946
+
947
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
948
+
949
+ h4. +exclude?+
950
+
951
+ The predicate +exclude?+ tests whether a given object does *not* belong to the collection. It is the negation of the builtin +include?+:
952
+
953
+ <ruby>
954
+ to_visit << node if visited.exclude?(node)
955
+ </ruby>
956
+
957
+ NOTE: Defined in +active_support/core_ext/enumerable.rb+.
958
+
959
+ h3. Extensions to +Array+
960
+
961
+ h4. Accessing
962
+
963
+ Active Support augments the API of arrays to ease certain ways of accessing them. For example, +to+ returns the subarray of elements up to the one at the passed index:
964
+
965
+ <ruby>
966
+ %w(a b c d).to(2) # => %w(a b c)
967
+ [].to(7) # => []
968
+ </ruby>
969
+
970
+ Similarly, +from+ returns the tail from the element at the passed index on:
971
+
972
+ <ruby>
973
+ %w(a b c d).from(2) # => %w(c d)
974
+ %w(a b c d).from(10) # => nil
975
+ [].from(0) # => []
976
+ </ruby>
977
+
978
+ The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is builtin). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available.
979
+
980
+ You can pick a random element with +rand+:
981
+
982
+ <ruby>
983
+ shape_type = [Circle, Square, Triangle].rand
984
+ </ruby>
985
+
986
+ NOTE: Defined in +active_support/core_ext/array/access.rb+.
987
+
988
+ h4. Options Extraction
989
+
990
+ When the last argument in a method call is a hash, except perhaps for a +&block+ argument, Ruby allows you to omit the brackets:
991
+
992
+ <ruby>
993
+ User.exists?(:email => params[:email])
994
+ </ruby>
995
+
996
+ That syntactic sugar is used a lot in Rails to avoid positional arguments where there would be too many, offering instead interfaces that emulate named parameters. In particular it is very idiomatic to use a trailing hash for options.
997
+
998
+ If a method expects a variable number of arguments and uses <tt>*</tt> in its declaration, however, such an options hash ends up being an item of the array of arguments, where kind of loses its role.
999
+
1000
+ In those cases, you may give an options hash a distinguished treatment with +extract_options!+. That method checks the type of the last item of an array. If it is a hash it pops it and returns it, otherwise returns an empty hash.
1001
+
1002
+ Let's see for example the definition of the +caches_action+ controller macro:
1003
+
1004
+ <ruby>
1005
+ def caches_action(*actions)
1006
+ return unless cache_configured?
1007
+ options = actions.extract_options!
1008
+ ...
1009
+ end
1010
+ </ruby>
1011
+
1012
+ This method receives an arbitrary number of action names, and an optional hash of options as last argument. With the call to +extract_options!+ you obtain the options hash and remove it from +actions+ in a simple and explicit way.
1013
+
1014
+ NOTE: Defined in +active_support/core_ext/array/extract_options.rb+.
1015
+
1016
+ h4. Conversions
1017
+
1018
+ h5. +to_sentence+
1019
+
1020
+ The method +to_sentence+ turns an array into a string containing a sentence that enumerates its items:
1021
+
1022
+ <ruby>
1023
+ %w().to_sentence # => ""
1024
+ %w(Earth).to_sentence # => "Earth"
1025
+ %w(Earth Wind).to_sentence # => "Earth and Wind"
1026
+ %w(Earth Wind Fire).to_sentence # => "Earth, Wind, and Fire"
1027
+ </ruby>
1028
+
1029
+ This method accepts three options:
1030
+
1031
+ * <tt>:two_words_connector</tt>: What is used for arrays of length 2. Default is " and ".
1032
+ * <tt>:words_connector</tt>: What is used to join the elements of arrays with 3 or more elements, except for the last two. Default is ", ".
1033
+ * <tt>:last_word_connector</tt>: What is used to join the last items of an array with 3 or more elements. Default is ", and ".
1034
+
1035
+ The defaults for these options can be localised, their keys are:
1036
+
1037
+ |_. Option |_. I18n key |
1038
+ | <tt>:two_words_connector</tt> | <tt>support.array.two_words_connector</tt> |
1039
+ | <tt>:words_connector</tt> | <tt>support.array.words_connector</tt> |
1040
+ | <tt>:last_word_connector</tt> | <tt>support.array.last_word_connector</tt> |
1041
+
1042
+ Options <tt>:connector</tt> and <tt>:skip_last_comma</tt> are deprecated.
1043
+
1044
+ NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
1045
+
1046
+ h5. +to_formatted_s+
1047
+
1048
+ The method +to_formatted_s+ acts like +to_s+ by default.
1049
+
1050
+ If the array contains items that respond to +id+, however, it may be passed the symbol <tt>:db</tt> as argument. That's typically used with collections of ARs, though technically any object in Ruby 1.8 responds to +id+ indeed. Returned strings are:
1051
+
1052
+ <ruby>
1053
+ [].to_formatted_s(:db) # => "null"
1054
+ [user].to_formatted_s(:db) # => "8456"
1055
+ invoice.lines.to_formatted_s(:db) # => "23,567,556,12"
1056
+ </ruby>
1057
+
1058
+ Integers in the example above are supposed to come from the respective calls to +id+.
1059
+
1060
+ NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
1061
+
1062
+ h5. +to_xml+
1063
+
1064
+ The method +to_xml+ returns a string containing an XML representation of its receiver:
1065
+
1066
+ <ruby>
1067
+ Contributor.all(:limit => 2, :order => 'rank ASC').to_xml
1068
+ # =>
1069
+ # <?xml version="1.0" encoding="UTF-8"?>
1070
+ # <contributors type="array">
1071
+ # <contributor>
1072
+ # <id type="integer">4356</id>
1073
+ # <name>Jeremy Kemper</name>
1074
+ # <rank type="integer">1</rank>
1075
+ # <url-id>jeremy-kemper</url-id>
1076
+ # </contributor>
1077
+ # <contributor>
1078
+ # <id type="integer">4404</id>
1079
+ # <name>David Heinemeier Hansson</name>
1080
+ # <rank type="integer">2</rank>
1081
+ # <url-id>david-heinemeier-hansson</url-id>
1082
+ # </contributor>
1083
+ # </contributors>
1084
+ </ruby>
1085
+
1086
+ To do so it sends +to_xml+ to every item in turn, and collects the results under a root node. All items must respond to +to_xml+, an exception is raised otherwise.
1087
+
1088
+ By default, the name of the root element is the underscorized and dasherized plural of the name of the class of the first item, provided the rest of elements belong to that type (checked with <tt>is_a?</tt>) and they are not hashes. In the example above that's "contributors".
1089
+
1090
+ If there's any element that does not belong to the type of the first one the root node becomes "records":
1091
+
1092
+ <ruby>
1093
+ [Contributor.first, Commit.first].to_xml
1094
+ # =>
1095
+ # <?xml version="1.0" encoding="UTF-8"?>
1096
+ # <records type="array">
1097
+ # <record>
1098
+ # <id type="integer">4583</id>
1099
+ # <name>Aaron Batalion</name>
1100
+ # <rank type="integer">53</rank>
1101
+ # <url-id>aaron-batalion</url-id>
1102
+ # </record>
1103
+ # <record>
1104
+ # <author>Joshua Peek</author>
1105
+ # <authored-timestamp type="datetime">2009-09-02T16:44:36Z</authored-timestamp>
1106
+ # <branch>origin/master</branch>
1107
+ # <committed-timestamp type="datetime">2009-09-02T16:44:36Z</committed-timestamp>
1108
+ # <committer>Joshua Peek</committer>
1109
+ # <git-show nil="true"></git-show>
1110
+ # <id type="integer">190316</id>
1111
+ # <imported-from-svn type="boolean">false</imported-from-svn>
1112
+ # <message>Kill AMo observing wrap_with_notifications since ARes was only using it</message>
1113
+ # <sha1>723a47bfb3708f968821bc969a9a3fc873a3ed58</sha1>
1114
+ # </record>
1115
+ # </records>
1116
+ </ruby>
1117
+
1118
+ If the receiver is an array of hashes the root element is by default also "records":
1119
+
1120
+ <ruby>
1121
+ [{:a => 1, :b => 2}, {:c => 3}].to_xml
1122
+ # =>
1123
+ # <?xml version="1.0" encoding="UTF-8"?>
1124
+ # <records type="array">
1125
+ # <record>
1126
+ # <b type="integer">2</b>
1127
+ # <a type="integer">1</a>
1128
+ # </record>
1129
+ # <record>
1130
+ # <c type="integer">3</c>
1131
+ # </record>
1132
+ # </records>
1133
+ </ruby>
1134
+
1135
+ WARNING. If the collection is empty the root element is by default "nil-classes". That's a gotcha, for example the root element of the list of contributors above would not be "contributors" if the collection was empty, but "nil-classes". You may use the <tt>:root</tt> option to ensure a consistent root element.
1136
+
1137
+ The name of children nodes is by default the name of the root node singularized. In the examples above we've seen "contributor" and "record". The option <tt>:children</tt> allows you to set these node names.
1138
+
1139
+ The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can configure your own builder via the <tt>:builder</tt> option. The method also accepts options like <tt>:dasherize</tt> and friends, they are forwarded to the builder:
1140
+
1141
+ <ruby>
1142
+ Contributor.all(:limit => 2, :order => 'rank ASC').to_xml(:skip_types => true)
1143
+ # =>
1144
+ # <?xml version="1.0" encoding="UTF-8"?>
1145
+ # <contributors>
1146
+ # <contributor>
1147
+ # <id>4356</id>
1148
+ # <name>Jeremy Kemper</name>
1149
+ # <rank>1</rank>
1150
+ # <url-id>jeremy-kemper</url-id>
1151
+ # </contributor>
1152
+ # <contributor>
1153
+ # <id>4404</id>
1154
+ # <name>David Heinemeier Hansson</name>
1155
+ # <rank>2</rank>
1156
+ # <url-id>david-heinemeier-hansson</url-id>
1157
+ # </contributor>
1158
+ # </contributors>
1159
+ </ruby>
1160
+
1161
+ NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
1162
+
1163
+ h4. Wrapping
1164
+
1165
+ The class method +Array.wrap+ behaves like the function +Array()+ except that it does not try to call +to_a+ on its argument. That changes the behaviour for enumerables:
1166
+
1167
+ <ruby>
1168
+ Array.wrap(:foo => :bar) # => [{:foo => :bar}]
1169
+ Array(:foo => :bar) # => [[:foo, :bar]]
1170
+
1171
+ Array.wrap("foo\nbar") # => ["foo\nbar"]
1172
+ Array("foo\nbar") # => ["foo\n", "bar"], in Ruby 1.8
1173
+ </ruby>
1174
+
1175
+ NOTE: Defined in +active_support/core_ext/array/wrap.rb+.
1176
+
1177
+ h4. Grouping
1178
+
1179
+ h5. +in_groups_of(number, fill_with = nil)+
1180
+
1181
+ The method +in_groups_of+ splits an array into consecutive groups of a certain size. It returns an array with the groups:
1182
+
1183
+ <ruby>
1184
+ [1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
1185
+ </ruby>
1186
+
1187
+ or yields them in turn if a block is passed:
1188
+
1189
+ <ruby>
1190
+ <% sample.in_groups_of(3) do |a, b, c| %>
1191
+ <tr>
1192
+ <td><%=h a %></td>
1193
+ <td><%=h b %></td>
1194
+ <td><%=h c %></td>
1195
+ </tr>
1196
+ <% end %>
1197
+ </ruby>
1198
+
1199
+ The first example shows +in_groups_of+ fills the last group with as many +nil+ elements as needed to have the requested size. You can change this padding value using the second optional argument:
1200
+
1201
+ <ruby>
1202
+ [1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
1203
+ </ruby>
1204
+
1205
+ And you can tell the method not to fill the last group passing +false+:
1206
+
1207
+ <ruby>
1208
+ [1, 2, 3].in_groups_of(2, false) # => [[1, 2], [3]]
1209
+ </ruby>
1210
+
1211
+ As a consequence +false+ can't be a used as a padding value.
1212
+
1213
+ NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
1214
+
1215
+ h5. +in_groups(number, fill_with = nil)+
1216
+
1217
+ The method +in_groups+ splits an array into a certain number of groups. The method returns and array with the groups:
1218
+
1219
+ <ruby>
1220
+ %w(1 2 3 4 5 6 7).in_groups(3)
1221
+ # => [["1", "2", "3"], ["4", "5", nil], ["6", "7", nil]]
1222
+ </ruby>
1223
+
1224
+ or yields them in turn if a block is passed:
1225
+
1226
+ <ruby>
1227
+ %w(1 2 3 4 5 6 7).in_groups(3) {|group| p group}
1228
+ ["1", "2", "3"]
1229
+ ["4", "5", nil]
1230
+ ["6", "7", nil]
1231
+ </ruby>
1232
+
1233
+ The examples above show that +in_groups+ fills some groups with a trailing +nil+ element as needed. A group can get at most one of these extra elements, the rightmost one if any. And the groups that have them are always the last ones.
1234
+
1235
+ You can change this padding value using the second optional argument:
1236
+
1237
+ <ruby>
1238
+ %w(1 2 3 4 5 6 7).in_groups(3, "0")
1239
+ # => [["1", "2", "3"], ["4", "5", "0"], ["6", "7", "0"]]
1240
+ </ruby>
1241
+
1242
+ And you can tell the method not to fill the smaller groups passing +false+:
1243
+
1244
+ <ruby>
1245
+ %w(1 2 3 4 5 6 7).in_groups(3, false)
1246
+ # => [["1", "2", "3"], ["4", "5"], ["6", "7"]]
1247
+ </ruby>
1248
+
1249
+ As a consequence +false+ can't be a used as a padding value.
1250
+
1251
+ NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
1252
+
1253
+ h5. +split(value = nil)+
1254
+
1255
+ The method +split+ divides an array by a separator and returns the resulting chunks.
1256
+
1257
+ If a block is passed the separators are those elements of the array for which the block returns true:
1258
+
1259
+ <ruby>
1260
+ (-5..5).to_a.split { |i| i.multiple_of?(4) }
1261
+ # => [[-5], [-3, -2, -1], [1, 2, 3], [5]]
1262
+ </ruby>
1263
+
1264
+ Otherwise, the value received as argument, which defaults to +nil+, is the separator:
1265
+
1266
+ <ruby>
1267
+ [0, 1, -5, 1, 1, "foo", "bar"].split(1)
1268
+ # => [[0], [-5], [], ["foo", "bar"]]
1269
+ </ruby>
1270
+
1271
+ TIP: Observe in the previous example that consecutive separators result in empty arrays.
1272
+
1273
+ NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
1274
+
1275
+ h3. Extensions to +Hash+
1276
+
1277
+ h4. Conversions
1278
+
1279
+ h5. +to_xml+
1280
+
1281
+ The method +to_xml+ returns a string containing an XML representation of its receiver:
1282
+
1283
+ <ruby>
1284
+ {"foo" => 1, "bar" => 2}.to_xml
1285
+ # =>
1286
+ # <?xml version="1.0" encoding="UTF-8"?>
1287
+ # <hash>
1288
+ # <foo type="integer">1</foo>
1289
+ # <bar type="integer">2</bar>
1290
+ # </hash>
1291
+ </ruby>
1292
+
1293
+ To do so, the method loops over the pairs and builds nodes that depend on the _values_. Given a pair +key+, +value+:
1294
+
1295
+ * If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
1296
+
1297
+ * If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>, and +key+ singularized as <tt>:children</tt>.
1298
+
1299
+ * If +value+ is a callable object it must expect one or two arguments. Depending on the arity, the callable is invoked with the +options+ hash as first argument with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. Its return value becomes a new node.
1300
+
1301
+ * If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
1302
+
1303
+ * Otherwise, a node with +key+ as tag is created with a string representation of +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added. Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is added as well according to the following mapping:
1304
+ <ruby>
1305
+ XML_TYPE_NAMES = {
1306
+ "Symbol" => "symbol",
1307
+ "Fixnum" => "integer",
1308
+ "Bignum" => "integer",
1309
+ "BigDecimal" => "decimal",
1310
+ "Float" => "float",
1311
+ "TrueClass" => "boolean",
1312
+ "FalseClass" => "boolean",
1313
+ "Date" => "date",
1314
+ "DateTime" => "datetime",
1315
+ "Time" => "datetime"
1316
+ }
1317
+ </ruby>
1318
+
1319
+ By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
1320
+
1321
+ The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can configure your own builder with the <tt>:builder</tt> option. The method also accepts options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
1322
+
1323
+ NOTE: Defined in +active_support/core_ext/hash/conversions.rb+.
1324
+
1325
+ h4. Merging
1326
+
1327
+ Ruby has a builtin method +Hash#merge+ that merges two hashes:
1328
+
1329
+ <ruby>
1330
+ {:a => 1, :b => 1}.merge(:a => 0, :c => 2)
1331
+ # => {:a => 0, :b => 1, :c => 2}
1332
+ </ruby>
1333
+
1334
+ Active Support defines a few more ways of merging hashes that may be convenient.
1335
+
1336
+ h5. +reverse_merge+ and +reverse_merge!+
1337
+
1338
+ In case of collision the key in the hash of the argument wins in +merge+. You can support option hashes with default values in a compact way with this idiom:
1339
+
1340
+ <ruby>
1341
+ options = {:length => 30, :omission => "..."}.merge(options)
1342
+ </ruby>
1343
+
1344
+ Active Support defines +reverse_merge+ in case you prefer this alternative notation:
1345
+
1346
+ <ruby>
1347
+ options = options.reverse_merge(:length => 30, :omission => "...")
1348
+ </ruby>
1349
+
1350
+ And a bang version +reverse_merge!+ that performs the merge in place:
1351
+
1352
+ <ruby>
1353
+ options.reverse_merge!(:length => 30, :omission => "...")
1354
+ </ruby>
1355
+
1356
+ WARNING. Take into account that +reverse_merge!+ may change the hash in the caller, which may or may not be a good idea.
1357
+
1358
+ NOTE: Defined in +active_support/core_ext/hash/reverse_merge.rb+.
1359
+
1360
+ h5. +reverse_update+
1361
+
1362
+ The method +reverse_update+ is an alias for +reverse_merge!+, explained above.
1363
+
1364
+ WARNING. Note that +reverse_update+ has no bang.
1365
+
1366
+ NOTE: Defined in +active_support/core_ext/hash/reverse_merge.rb+.
1367
+
1368
+ h5. +deep_merge+ and +deep_merge!+
1369
+
1370
+ As you can see in the previous example if a key is found in both hashes the value in the one in the argument wins.
1371
+
1372
+ Active Support defines +Hash#deep_merge+. In a deep merge, if a key is found in both hashes and their values are hashes in turn, then their _merge_ becomes the value in the resulting hash:
1373
+
1374
+ <ruby>
1375
+ {:a => {:b => 1}}.deep_merge(:a => {:c => 2})
1376
+ # => {:a => {:b => 1, :c => 2}}
1377
+ </ruby>
1378
+
1379
+ The method +deep_merge!+ performs a deep merge in place.
1380
+
1381
+ NOTE: Defined in +active_support/core_ext/hash/deep_merge.rb+.
1382
+
1383
+ h4. Diffing
1384
+
1385
+ The method +diff+ returns a hash that represents a diff of the receiver and the argument with the following logic:
1386
+
1387
+ * Pairs +key+, +value+ that exist in both hashes do not belong to the diff hash.
1388
+
1389
+ * If both hashes have +key+, but with different values, the pair in the receiver wins.
1390
+
1391
+ * The rest is just merged.
1392
+
1393
+ <ruby>
1394
+ {:a => 1}.diff(:a => 1)
1395
+ # => {}, first rule
1396
+
1397
+ {:a => 1}.diff(:a => 2)
1398
+ # => {:a => 1}, second rule
1399
+
1400
+ {:a => 1}.diff(:b => 2)
1401
+ # => {:a => 1, :b => 2}, third rule
1402
+
1403
+ {:a => 1, :b => 2, :c => 3}.diff(:b => 1, :c => 3, :d => 4)
1404
+ # => {:a => 1, :b => 2, :d => 4}, all rules
1405
+
1406
+ {}.diff({}) # => {}
1407
+ {:a => 1}.diff({}) # => {:a => 1}
1408
+ {}.diff(:a => 1) # => {:a => 1}
1409
+ </ruby>
1410
+
1411
+ An important property of this diff hash is that you can retrieve the original hash by applying +diff+ twice:
1412
+
1413
+ <ruby>
1414
+ hash.diff(hash2).diff(hash2) == hash
1415
+ </ruby>
1416
+
1417
+ Diffing hashes may be useful for error messages related to expected option hashes for example.
1418
+
1419
+ NOTE: Defined in +active_support/core_ext/hash/diff.rb+.
1420
+
1421
+ h4. Working with Keys
1422
+
1423
+ h5. +except+ and +except!+
1424
+
1425
+ The method +except+ returns a hash with the keys in the argument list removed, if present:
1426
+
1427
+ <ruby>
1428
+ {:a => 1, :b => 2}.except(:a) # => {:b => 2}
1429
+ </ruby>
1430
+
1431
+ If the receiver responds to +convert_key+, the method is called on each of the arguments. This allows +except+ to play nice with hashes with indifferent access for instance:
1432
+
1433
+ <ruby>
1434
+ {:a => 1}.with_indifferent_access.except(:a) # => {}
1435
+ {:a => 1}.with_indifferent_access.except("a") # => {}
1436
+ </ruby>
1437
+
1438
+ The method +except+ may come in handy for example when you want to protect some parameter that can't be globally protected with +attr_protected+:
1439
+
1440
+ <ruby>
1441
+ params[:account] = params[:account].except(:plan_id) unless admin?
1442
+ @account.update_attributes(params[:account])
1443
+ </ruby>
1444
+
1445
+ There's also the bang variant +except!+ that removes keys in the very receiver.
1446
+
1447
+ NOTE: Defined in +active_support/core_ext/hash/except.rb+.
1448
+
1449
+ h5. +stringify_keys+ and +stringify_keys!+
1450
+
1451
+ The method +stringify_keys+ returns a hash that has a stringified version of the keys in the receiver. It does so by sending +to_s+ to them:
1452
+
1453
+ <ruby>
1454
+ {nil => nil, 1 => 1, :a => :a}.stringify_keys
1455
+ # => {"" => nil, "a" => :a, "1" => 1}
1456
+ </ruby>
1457
+
1458
+ The result in case of collision is undefined:
1459
+
1460
+ <ruby>
1461
+ {"a" => 1, :a => 2}.stringify_keys
1462
+ # => {"a" => 2}, in my test, can't rely on this result though
1463
+ </ruby>
1464
+
1465
+ This method may be useful for example to easily accept both symbols and strings as options. For instance +ActionView::Helpers::FormHelper+ defines:
1466
+
1467
+ <ruby>
1468
+ def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
1469
+ options = options.stringify_keys
1470
+ options["type"] = "checkbox"
1471
+ ...
1472
+ end
1473
+ </ruby>
1474
+
1475
+ The second line can safely access the "type" key, and let the user to pass either +:type+ or "type".
1476
+
1477
+ There's also the bang variant +stringify_keys!+ that stringifies keys in the very receiver.
1478
+
1479
+ NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
1480
+
1481
+ h5. +symbolize_keys+ and +symbolize_keys!+
1482
+
1483
+ The method +symbolize_keys+ returns a hash that has a symbolized version of the keys in the receiver, where possible. It does so by sending +to_sym+ to them:
1484
+
1485
+ <ruby>
1486
+ {nil => nil, 1 => 1, "a" => "a"}.symbolize_keys
1487
+ # => {1 => 1, nil => nil, :a => "a"}
1488
+ </ruby>
1489
+
1490
+ WARNING. Note in the previous example only one key was symbolized.
1491
+
1492
+ The result in case of collision is undefined:
1493
+
1494
+ <ruby>
1495
+ {"a" => 1, :a => 2}.symbolize_keys
1496
+ # => {:a => 2}, in my test, can't rely on this result though
1497
+ </ruby>
1498
+
1499
+ This method may be useful for example to easily accept both symbols and strings as options. For instance +ActionController::UrlRewriter+ defines
1500
+
1501
+ <ruby>
1502
+ def rewrite_path(options)
1503
+ options = options.symbolize_keys
1504
+ options.update(options[:params].symbolize_keys) if options[:params]
1505
+ ...
1506
+ end
1507
+ </ruby>
1508
+
1509
+ The second line can safely access the +:params+ key, and let the user to pass either +:params+ or "params".
1510
+
1511
+ There's also the bang variant +symbolize_keys!+ that symbolizes keys in the very receiver.
1512
+
1513
+ NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
1514
+
1515
+ h5. +to_options+ and +to_options!+
1516
+
1517
+ The methods +to_options+ and +to_options!+ are respectively aliases of +symbolize_keys+ and +symbolize_keys!+.
1518
+
1519
+ NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
1520
+
1521
+ h5. +assert_valid_keys+
1522
+
1523
+ The method +assert_valid_keys+ receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does +ArgumentError+ is raised.
1524
+
1525
+ <ruby>
1526
+ {:a => 1}.assert_valid_keys(:a) # passes
1527
+ {:a => 1}.assert_valid_keys("a") # ArgumentError
1528
+ </ruby>
1529
+
1530
+ Active Record does not accept unknown options when building associations for example. It implements that control via +assert_valid_keys+:
1531
+
1532
+ <ruby>
1533
+ mattr_accessor :valid_keys_for_has_many_association
1534
+ @@valid_keys_for_has_many_association = [
1535
+ :class_name, :table_name, :foreign_key, :primary_key,
1536
+ :dependent,
1537
+ :select, :conditions, :include, :order, :group, :having, :limit, :offset,
1538
+ :as, :through, :source, :source_type,
1539
+ :uniq,
1540
+ :finder_sql, :counter_sql,
1541
+ :before_add, :after_add, :before_remove, :after_remove,
1542
+ :extend, :readonly,
1543
+ :validate, :inverse_of
1544
+ ]
1545
+
1546
+ def create_has_many_reflection(association_id, options, &extension)
1547
+ options.assert_valid_keys(valid_keys_for_has_many_association)
1548
+ ...
1549
+ end
1550
+ </ruby>
1551
+
1552
+ NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
1553
+
1554
+ h4. Slicing
1555
+
1556
+ Ruby has builtin support for taking slices out of strings and arrays. Active Support extends slicing to hashes:
1557
+
1558
+ <ruby>
1559
+ {:a => 1, :b => 2, :c => 3}.slice(:a, :c)
1560
+ # => {:c => 3, :a => 1}
1561
+
1562
+ {:a => 1, :b => 2, :c => 3}.slice(:b, :X)
1563
+ # => {:b => 2} # non-existing keys are ignored
1564
+ </ruby>
1565
+
1566
+ If the receiver responds to +convert_key+ keys are normalized:
1567
+
1568
+ <ruby>
1569
+ {:a => 1, :b => 2}.with_indifferent_access.slice("a")
1570
+ # => {:a => 1}
1571
+ </ruby>
1572
+
1573
+ NOTE. Slicing may come in handy for sanitizing option hashes with a white list of keys.
1574
+
1575
+ There's also +slice!+ which in addition to perform a slice in place returns what's removed:
1576
+
1577
+ <ruby>
1578
+ hash = {:a => 1, :b => 2}
1579
+ rest = hash.slice!(:a) # => {:b => 2}
1580
+ hash # => {:a => 1}
1581
+ </ruby>
1582
+
1583
+ NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
1584
+
1585
+ h4. Indifferent Access
1586
+
1587
+ The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver:
1588
+
1589
+ <ruby>
1590
+ {:a => 1}.with_indifferent_access["a"] # => 1
1591
+ </ruby>
1592
+
1593
+ NOTE: Defined in +active_support/core_ext/hash/indifferent_access.rb+.
1594
+
1595
+ h3. Extensions to +Regexp+
1596
+
1597
+ h4. +multiline?+
1598
+
1599
+ The method +multiline?+ says whether a regexp has the +/m+ flag set, that is, whether the dot matches newlines.
1600
+
1601
+ <ruby>
1602
+ %r{.}.multiline? # => false
1603
+ %r{.}m.multiline? # => true
1604
+
1605
+ Regexp.new('.').multiline? # => false
1606
+ Regexp.new('.', Regexp::MULTILINE).multiline? # => true
1607
+ </ruby>
1608
+
1609
+ Rails uses this method in a single place, also in the routing code. Multiline regexps are disallowed for route requirements and this flag eases enforcing that constraint.
1610
+
1611
+ <ruby>
1612
+ def assign_route_options(segments, defaults, requirements)
1613
+ ...
1614
+ if requirement.multiline?
1615
+ raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
1616
+ end
1617
+ ...
1618
+ end
1619
+ </ruby>
1620
+
1621
+ NOTE: Defined in +active_support/core_ext/regexp.rb+.
1622
+
1623
+ h3. Extensions to +Range+
1624
+
1625
+ h4. +to_s+
1626
+
1627
+ Active Support extends the method +Range#to_s+ so that it understands an optional format argument. As of this writing the only supported non-default format is +:db+:
1628
+
1629
+ <ruby>
1630
+ (Date.today..Date.tomorrow).to_s
1631
+ # => "2009-10-25..2009-10-26"
1632
+
1633
+ (Date.today..Date.tomorrow).to_s(:db)
1634
+ # => "BETWEEN '2009-10-25' AND '2009-10-26'"
1635
+ </ruby>
1636
+
1637
+ As the example depicts, the +:db+ format generates a +BETWEEN+ SQL clause. That is used by Active Record in its support for range values in conditions.
1638
+
1639
+ NOTE: Defined in +active_support/core_ext/range/conversions.rb+.
1640
+
1641
+ h4. +step+
1642
+
1643
+ Active Support extends the method +Range#step+ so that it can be invoked without a block:
1644
+
1645
+ <ruby>
1646
+ (1..10).step(2) # => [1, 3, 5, 7, 9]
1647
+ </ruby>
1648
+
1649
+ As the example shows, in that case the method returns and array with the corresponding elements.
1650
+
1651
+ NOTE: Defined in +active_support/core_ext/range/blockless_step.rb+.
1652
+
1653
+ h4. +include?+
1654
+
1655
+ The method +Range#include?+ says whether some value falls between the ends of a given instance:
1656
+
1657
+ <ruby>
1658
+ (2..3).include?(Math::E) # => true
1659
+ </ruby>
1660
+
1661
+ Active Support extends this method so that the argument may be another range in turn. In that case we test whether the ends of the argument range belong to the receiver themselves:
1662
+
1663
+ <ruby>
1664
+ (1..10).include?(3..7) # => true
1665
+ (1..10).include?(0..7) # => false
1666
+ (1..10).include?(3..11) # => false
1667
+ (1...9).include?(3..9) # => false
1668
+ </ruby>
1669
+
1670
+ WARNING: The orginal +Range#include?+ is still the one aliased to +Range#===+.
1671
+
1672
+ NOTE: Defined in +active_support/core_ext/range/include_range.rb+.
1673
+
1674
+ h4. +overlaps?+
1675
+
1676
+ The method +Range#overlaps?+ says whether any two given ranges have non-void intersection:
1677
+
1678
+ <ruby>
1679
+ (1..10).overlaps?(7..11) # => true
1680
+ (1..10).overlaps?(0..7) # => true
1681
+ (1..10).overlaps?(11..27) # => false
1682
+ </ruby>
1683
+
1684
+ NOTE: Defined in +active_support/core_ext/range/overlaps.rb+.
1685
+
1686
+ h3. Extensions to +Proc+
1687
+
1688
+ h4. +bind+
1689
+
1690
+ As you surely know Ruby has an +UnboundMethod+ class whose instances are methods that belong to the limbo of methods without a self. The method +Module#instance_method+ returns an unbound method for example:
1691
+
1692
+ <ruby>
1693
+ Hash.instance_method(:delete) # => #<UnboundMethod: Hash#delete>
1694
+ </ruby>
1695
+
1696
+ An unbound method is not callable as is, you need to bind it first to an object with +bind+:
1697
+
1698
+ <ruby>
1699
+ clear = Hash.instance_method(:clear)
1700
+ clear.bind({:a => 1}).call # => {}
1701
+ </ruby>
1702
+
1703
+ Active Support defines +Proc#bind+ with an analogous purpose:
1704
+
1705
+ <ruby>
1706
+ Proc.new { size }.bind([]).call # => 0
1707
+ </ruby>
1708
+
1709
+ As you see that's callable and bound to the argument, the return value is indeed a +Method+.
1710
+
1711
+ NOTE: To do so +Proc#bind+ actually creates a method under the hood. If you ever see a method with a weird name like +__bind_1256598120_237302+ in a stack trace you know now where it comes from.
1712
+
1713
+ Action Pack uses this trick in +rescue_from+ for example, which accepts the name of a method and also a proc as callbacks for a given rescued exception. It has to call them in either case, so a bound method is returned by +handler_for_rescue+, thus simplifying the code in the caller:
1714
+
1715
+ <ruby>
1716
+ def handler_for_rescue(exception)
1717
+ _, rescuer = Array(rescue_handlers).reverse.detect do |klass_name, handler|
1718
+ ...
1719
+ end
1720
+
1721
+ case rescuer
1722
+ when Symbol
1723
+ method(rescuer)
1724
+ when Proc
1725
+ rescuer.bind(self)
1726
+ end
1727
+ end
1728
+ </ruby>
1729
+
1730
+ NOTE: Defined in +active_support/core_ext/proc.rb+.
1731
+
1732
+ h3. Extensions to +Date+
1733
+
1734
+ ...
1735
+
1736
+ h3. Extensions to +DateTime+
1737
+
1738
+ ...
1739
+
1740
+ h3. Extensions to +Time+
1741
+
1742
+ ...
1743
+
1744
+ h3. Extensions to +Process+
1745
+
1746
+ ...
1747
+
1748
+ h3. Extensions to +Pathname+
1749
+
1750
+ ...
1751
+
1752
+ h3. Extensions to +File+
1753
+
1754
+ h4. +atomic_write+
1755
+
1756
+ 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.
1757
+
1758
+ 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.
1759
+
1760
+ For example, Action Pack uses this method to write asset cache files like +all.css+:
1761
+
1762
+ <ruby>
1763
+ File.atomic_write(joined_asset_path) do |cache|
1764
+ cache.write(join_asset_file_contents(asset_paths))
1765
+ end
1766
+ </ruby>
1767
+
1768
+ 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.
1769
+
1770
+ WARNING. Note you can't append with +atomic_write+.
1771
+
1772
+ The auxiliary file is written in a standard directory for temporary files, but you can pass a directory of your choice as second argument.
1773
+
1774
+ NOTE: Defined in +active_support/core_ext/file/atomic.rb+.
1775
+
1776
+ h3. Extensions to +NameError+
1777
+
1778
+ Active Support adds +missing_name?+ to +NameError+, which tests whether the exception was raised because of the name passed as argument.
1779
+
1780
+ The name may be given as a symbol or string. A symbol is tested against the bare constant name, a string is against the fully-qualified constant name.
1781
+
1782
+ TIP: A symbol can represent a fully-qualified constant name as in +:"ActiveRecord::Base"+, so the behaviour for symbols is defined for convenience, not because it has to be that way technically.
1783
+
1784
+ For example, when an action of +PostsController+ is called Rails tries optimistically to use +PostsHelper+. It is OK that the helper module does not exist, so if an exception for that constant name is raised it should be silenced. But it could be the case that +posts_helper.rb+ raises a +NameError+ due to an actual unknown constant. That should be reraised. The method +missing_name?+ provides a way to distinguish both cases:
1785
+
1786
+ <ruby>
1787
+ def default_helper_module!
1788
+ module_name = name.sub(/Controller$/, '')
1789
+ module_path = module_name.underscore
1790
+ helper module_path
1791
+ rescue MissingSourceFile => e
1792
+ raise e unless e.is_missing? "#{module_path}_helper"
1793
+ rescue NameError => e
1794
+ raise e unless e.missing_name? "#{module_name}Helper"
1795
+ end
1796
+ </ruby>
1797
+
1798
+ NOTE: Defined in +active_support/core_ext/name_error.rb+.
1799
+
1800
+ h3. Extensions to +LoadError+
1801
+
1802
+ Rails hijacks +LoadError.new+ to return a +MissingSourceFile+ exception:
1803
+
1804
+ <shell>
1805
+ $ ruby -e 'require "nonexistent"'
1806
+ ...: no such file to load -- nonexistent (LoadError)
1807
+ ...
1808
+ $ script/runner 'require "nonexistent"'
1809
+ ...: no such file to load -- nonexistent (MissingSourceFile)
1810
+ ...
1811
+ </shell>
1812
+
1813
+ The class +MissingSourceFile+ is a subclass of +LoadError+, so any code that rescues +LoadError+ as usual still works as expected. Point is these exception objects respond to +is_missing?+, which given a path name tests whether the exception was raised due to that particular file (except perhaps for the ".rb" extension).
1814
+
1815
+ For example, when an action of +PostsController+ is called Rails tries to load +posts_helper.rb+, but that file may not exist. That's fine, the helper module is not mandatory so Rails silences a load error. But it could be the case that the helper module does exist, but it in turn requires another library that is missing. In that case Rails must reraise the exception. The method +is_missing?+ provides a way to distinguish both cases:
1816
+
1817
+ <ruby>
1818
+ def default_helper_module!
1819
+ module_name = name.sub(/Controller$/, '')
1820
+ module_path = module_name.underscore
1821
+ helper module_path
1822
+ rescue MissingSourceFile => e
1823
+ raise e unless e.is_missing? "#{module_path}_helper"
1824
+ rescue NameError => e
1825
+ raise e unless e.missing_name? "#{module_name}Helper"
1826
+ end
1827
+ </ruby>
1828
+
1829
+ NOTE: Defined in +active_support/core_ext/load_error.rb+.
1830
+
1831
+ h3. Changelog
1832
+
1833
+ "Lighthouse ticket":https://rails.lighthouseapp.com/projects/16213/tickets/67
1834
+
1835
+ * April 18, 2009: Initial version by "Xavier Noria":credits.html#fxn