rails 4.0.0 → 4.2.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +30 -23
  3. data/guides/CHANGELOG.md +108 -6
  4. data/guides/Rakefile +21 -6
  5. data/guides/assets/images/akshaysurve.jpg +0 -0
  6. data/guides/assets/images/edge_badge.png +0 -0
  7. data/guides/assets/images/feature_tile.gif +0 -0
  8. data/guides/assets/images/footer_tile.gif +0 -0
  9. data/guides/assets/images/fxn.png +0 -0
  10. data/guides/assets/images/getting_started/article_with_comments.png +0 -0
  11. data/guides/assets/images/getting_started/challenge.png +0 -0
  12. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  13. data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  14. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  15. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  16. data/guides/assets/images/getting_started/new_article.png +0 -0
  17. data/guides/assets/images/getting_started/rails_welcome.png +0 -0
  18. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  19. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  20. data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
  21. data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
  22. data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
  23. data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
  24. data/guides/assets/images/header_tile.gif +0 -0
  25. data/guides/assets/images/icons/README +1 -1
  26. data/guides/assets/images/icons/callouts/11.png +0 -0
  27. data/guides/assets/images/icons/callouts/12.png +0 -0
  28. data/guides/assets/images/icons/callouts/13.png +0 -0
  29. data/guides/assets/images/icons/callouts/15.png +0 -0
  30. data/guides/assets/images/icons/caution.png +0 -0
  31. data/guides/assets/images/icons/example.png +0 -0
  32. data/guides/assets/images/radar.png +0 -0
  33. data/guides/assets/images/rails4_features.png +0 -0
  34. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  35. data/guides/assets/images/vijaydev.jpg +0 -0
  36. data/guides/assets/javascripts/guides.js +36 -34
  37. data/guides/assets/stylesheets/main.css +6 -2
  38. data/guides/assets/stylesheets/print.css +1 -1
  39. data/guides/bug_report_templates/action_controller_gem.rb +47 -0
  40. data/guides/bug_report_templates/action_controller_master.rb +54 -0
  41. data/guides/bug_report_templates/active_record_gem.rb +5 -2
  42. data/guides/bug_report_templates/active_record_master.rb +3 -2
  43. data/guides/bug_report_templates/generic_gem.rb +15 -0
  44. data/guides/bug_report_templates/generic_master.rb +26 -0
  45. data/guides/rails_guides.rb +23 -4
  46. data/guides/rails_guides/generator.rb +1 -1
  47. data/guides/rails_guides/helpers.rb +4 -2
  48. data/guides/rails_guides/levenshtein.rb +27 -21
  49. data/guides/rails_guides/markdown.rb +11 -7
  50. data/guides/rails_guides/markdown/renderer.rb +1 -1
  51. data/guides/source/2_2_release_notes.md +3 -3
  52. data/guides/source/2_3_release_notes.md +12 -12
  53. data/guides/source/3_0_release_notes.md +10 -13
  54. data/guides/source/3_1_release_notes.md +7 -4
  55. data/guides/source/3_2_release_notes.md +17 -14
  56. data/guides/source/4_0_release_notes.md +110 -54
  57. data/guides/source/4_1_release_notes.md +730 -0
  58. data/guides/source/4_2_release_notes.md +877 -0
  59. data/guides/source/_license.html.erb +1 -1
  60. data/guides/source/_welcome.html.erb +6 -2
  61. data/guides/source/action_controller_overview.md +223 -57
  62. data/guides/source/action_mailer_basics.md +129 -76
  63. data/guides/source/action_view_overview.md +247 -246
  64. data/guides/source/active_job_basics.md +339 -0
  65. data/guides/source/active_model_basics.md +374 -20
  66. data/guides/source/active_record_basics.md +46 -45
  67. data/guides/source/active_record_callbacks.md +83 -28
  68. data/guides/source/{migrations.md → active_record_migrations.md} +191 -275
  69. data/guides/source/active_record_postgresql.md +433 -0
  70. data/guides/source/active_record_querying.md +382 -300
  71. data/guides/source/active_record_validations.md +64 -55
  72. data/guides/source/active_support_core_extensions.md +229 -187
  73. data/guides/source/active_support_instrumentation.md +23 -22
  74. data/guides/source/api_documentation_guidelines.md +167 -15
  75. data/guides/source/asset_pipeline.md +768 -294
  76. data/guides/source/association_basics.md +188 -96
  77. data/guides/source/autoloading_and_reloading_constants.md +1311 -0
  78. data/guides/source/caching_with_rails.md +45 -11
  79. data/guides/source/command_line.md +96 -65
  80. data/guides/source/configuring.md +404 -70
  81. data/guides/source/contributing_to_ruby_on_rails.md +270 -130
  82. data/guides/source/credits.html.erb +7 -3
  83. data/guides/source/debugging_rails_applications.md +471 -284
  84. data/guides/source/development_dependencies_install.md +115 -21
  85. data/guides/source/documents.yaml +31 -9
  86. data/guides/source/engines.md +737 -291
  87. data/guides/source/form_helpers.md +137 -89
  88. data/guides/source/generators.md +60 -28
  89. data/guides/source/getting_started.md +1007 -596
  90. data/guides/source/i18n.md +178 -96
  91. data/guides/source/index.html.erb +2 -1
  92. data/guides/source/initialization.md +248 -104
  93. data/guides/source/kindle/toc.html.erb +1 -1
  94. data/guides/source/layout.html.erb +14 -22
  95. data/guides/source/layouts_and_rendering.md +78 -46
  96. data/guides/source/maintenance_policy.md +78 -0
  97. data/guides/source/nested_model_forms.md +10 -7
  98. data/guides/source/plugins.md +66 -57
  99. data/guides/source/rails_application_templates.md +49 -12
  100. data/guides/source/rails_on_rack.md +50 -60
  101. data/guides/source/routing.md +190 -139
  102. data/guides/source/ruby_on_rails_guides_guidelines.md +12 -13
  103. data/guides/source/security.md +134 -83
  104. data/guides/source/testing.md +322 -200
  105. data/guides/source/upgrading_ruby_on_rails.md +834 -37
  106. data/guides/source/working_with_javascript_in_rails.md +36 -26
  107. data/guides/w3c_validator.rb +2 -0
  108. metadata +93 -116
  109. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  110. data/guides/assets/images/getting_started/new_post.png +0 -0
  111. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  112. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  113. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  114. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  115. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  116. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  117. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  118. data/guides/code/getting_started/Gemfile +0 -43
  119. data/guides/code/getting_started/Gemfile.lock +0 -150
  120. data/guides/code/getting_started/README.rdoc +0 -28
  121. data/guides/code/getting_started/Rakefile +0 -6
  122. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -16
  123. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  124. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  125. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
  126. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  127. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  128. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  129. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
  130. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
  131. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -17
  132. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -47
  133. data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
  134. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  135. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  136. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
  137. data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
  138. data/guides/code/getting_started/app/models/comment.rb +0 -3
  139. data/guides/code/getting_started/app/models/post.rb +0 -7
  140. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  141. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  142. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  143. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
  144. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
  145. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
  146. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  147. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
  148. data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -3
  149. data/guides/code/getting_started/bin/bundle +0 -4
  150. data/guides/code/getting_started/bin/rails +0 -4
  151. data/guides/code/getting_started/bin/rake +0 -4
  152. data/guides/code/getting_started/config.ru +0 -4
  153. data/guides/code/getting_started/config/application.rb +0 -18
  154. data/guides/code/getting_started/config/boot.rb +0 -4
  155. data/guides/code/getting_started/config/database.yml +0 -25
  156. data/guides/code/getting_started/config/environment.rb +0 -5
  157. data/guides/code/getting_started/config/environments/development.rb +0 -30
  158. data/guides/code/getting_started/config/environments/production.rb +0 -80
  159. data/guides/code/getting_started/config/environments/test.rb +0 -36
  160. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  161. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
  162. data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
  163. data/guides/code/getting_started/config/initializers/locale.rb +0 -9
  164. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  165. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
  166. data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
  167. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  168. data/guides/code/getting_started/config/locales/en.yml +0 -23
  169. data/guides/code/getting_started/config/routes.rb +0 -7
  170. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
  171. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
  172. data/guides/code/getting_started/db/schema.rb +0 -33
  173. data/guides/code/getting_started/db/seeds.rb +0 -7
  174. data/guides/code/getting_started/public/404.html +0 -58
  175. data/guides/code/getting_started/public/422.html +0 -58
  176. data/guides/code/getting_started/public/500.html +0 -57
  177. data/guides/code/getting_started/public/favicon.ico +0 -0
  178. data/guides/code/getting_started/public/robots.txt +0 -5
  179. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
  180. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
  181. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
  182. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  183. data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
  184. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
  185. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
  186. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
  187. data/guides/code/getting_started/test/models/comment_test.rb +0 -7
  188. data/guides/code/getting_started/test/models/post_test.rb +0 -7
  189. data/guides/code/getting_started/test/test_helper.rb +0 -15
  190. data/guides/source/kindle/KINDLE.md +0 -26
@@ -32,7 +32,7 @@ Ruby on Rails Guides: Credits
32
32
  <% end %>
33
33
 
34
34
  <%= author('Oscar Del Ben', 'oscardelben', 'oscardelben.jpg') do %>
35
- Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wildfire</a>. He's a regular open source contributor (<a href="https://github.com/oscardelben">Github account</a>) and tweets regularly at <a href="https://twitter.com/oscardelben">@oscardelben</a>.
35
+ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wildfire</a>. He's a regular open source contributor (<a href="https://github.com/oscardelben">GitHub account</a>) and tweets regularly at <a href="https://twitter.com/oscardelben">@oscardelben</a>.
36
36
  <% end %>
37
37
 
38
38
  <%= author('Frederick Cheung', 'fcheung') do %>
@@ -40,7 +40,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
40
40
  <% end %>
41
41
 
42
42
  <%= author('Tore Darell', 'toretore') do %>
43
- Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. His home on the internet is his blog <a href="http://tore.darell.no">Sneaky Abstractions</a>.
43
+ Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. You can follow him on <a href="http://twitter.com/toretore">Twitter</a>.
44
44
  <% end %>
45
45
 
46
46
  <%= author('Jeff Dean', 'zilkey') do %>
@@ -64,7 +64,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
64
64
  <% end %>
65
65
 
66
66
  <%= author('Pratik Naik', 'lifo') do %>
67
- Pratik Naik is a Ruby on Rails developer at <a href="http://www.37signals.com">37signals</a> and also a member of the <a href="http://rubyonrails.org/core">Rails core team</a>. He maintains a blog at <a href="http://m.onkey.org">has_many :bugs, :through =&gt; :rails</a> and has a semi-active <a href="http://twitter.com/lifo">twitter account</a>.
67
+ Pratik Naik is a Ruby on Rails developer at <a href="https://basecamp.com/">Basecamp</a> and also a member of the <a href="http://rubyonrails.org/core">Rails core team</a>. He maintains a blog at <a href="http://m.onkey.org">has_many :bugs, :through =&gt; :rails</a> and has a semi-active <a href="http://twitter.com/lifo">twitter account</a>.
68
68
  <% end %>
69
69
 
70
70
  <%= author('Emilio Tagua', 'miloops') do %>
@@ -74,3 +74,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
74
74
  <%= author('Heiko Webers', 'hawe') do %>
75
75
  Heiko Webers is the founder of <a href="http://www.bauland42.de">bauland42</a>, a German web application security consulting and development company focused on Ruby on Rails. He blogs at the <a href="http://www.rorsecurity.info">Ruby on Rails Security Project</a>. After 10 years of desktop application development, Heiko has rarely looked back.
76
76
  <% end %>
77
+
78
+ <%= author('Akshay Surve', 'startupjockey', 'akshaysurve.jpg') do %>
79
+ Akshay Surve is the Founder at <a href="http://www.deltax.com">DeltaX</a>, hackathon specialist, a midnight code junkie and occasionally writes prose. You can connect with him on <a href="https://twitter.com/akshaysurve">Twitter</a>, <a href="http://www.linkedin.com/in/akshaysurve">Linkedin</a>, <a href="http://www.akshaysurve.com/">Personal Blog</a> or <a href="http://www.quora.com/Akshay-Surve">Quora</a>.
80
+ <% end %>
@@ -26,17 +26,17 @@ One common task is to inspect the contents of a variable. In Rails, you can do t
26
26
  The `debug` helper will return a \<pre> tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
27
27
 
28
28
  ```html+erb
29
- <%= debug @post %>
29
+ <%= debug @article %>
30
30
  <p>
31
31
  <b>Title:</b>
32
- <%= @post.title %>
32
+ <%= @article.title %>
33
33
  </p>
34
34
  ```
35
35
 
36
36
  You'll see something like this:
37
37
 
38
38
  ```yaml
39
- --- !ruby/object:Post
39
+ --- !ruby/object Article
40
40
  attributes:
41
41
  updated_at: 2008-09-05 22:55:47
42
42
  body: It's a very helpful guide for debugging your Rails app.
@@ -55,10 +55,10 @@ Title: Rails debugging guide
55
55
  Displaying an instance variable, or any other object or method, in YAML format can be achieved this way:
56
56
 
57
57
  ```html+erb
58
- <%= simple_format @post.to_yaml %>
58
+ <%= simple_format @article.to_yaml %>
59
59
  <p>
60
60
  <b>Title:</b>
61
- <%= @post.title %>
61
+ <%= @article.title %>
62
62
  </p>
63
63
  ```
64
64
 
@@ -67,7 +67,7 @@ The `to_yaml` method converts the method to YAML format leaving it more readable
67
67
  As a result of this, you will have something like this in your view:
68
68
 
69
69
  ```yaml
70
- --- !ruby/object:Post
70
+ --- !ruby/object Article
71
71
  attributes:
72
72
  updated_at: 2008-09-05 22:55:47
73
73
  body: It's a very helpful guide for debugging your Rails app.
@@ -88,7 +88,7 @@ Another useful method for displaying object values is `inspect`, especially when
88
88
  <%= [1, 2, 3, 4, 5].inspect %>
89
89
  <p>
90
90
  <b>Title:</b>
91
- <%= @post.title %>
91
+ <%= @article.title %>
92
92
  </p>
93
93
  ```
94
94
 
@@ -123,7 +123,7 @@ config.logger = Logger.new(STDOUT)
123
123
  config.logger = Log4r::Logger.new("Application Log")
124
124
  ```
125
125
 
126
- TIP: By default, each log is created under `Rails.root/log/` and the log file name is `environment_name.log`.
126
+ TIP: By default, each log is created under `Rails.root/log/` and the log file is named after the environment in which the application is running.
127
127
 
128
128
  ### Log Levels
129
129
 
@@ -138,7 +138,7 @@ Rails.logger.level = 0 # at any time
138
138
 
139
139
  This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
140
140
 
141
- TIP: The default Rails log level is `info` in production mode and `debug` in development and test mode.
141
+ TIP: The default Rails log level is `debug` in all environments.
142
142
 
143
143
  ### Sending Messages
144
144
 
@@ -153,18 +153,18 @@ logger.fatal "Terminating application, raised unrecoverable error!!!"
153
153
  Here's an example of a method instrumented with extra logging:
154
154
 
155
155
  ```ruby
156
- class PostsController < ApplicationController
156
+ class ArticlesController < ApplicationController
157
157
  # ...
158
158
 
159
159
  def create
160
- @post = Post.new(params[:post])
161
- logger.debug "New post: #{@post.attributes.inspect}"
162
- logger.debug "Post should be valid: #{@post.valid?}"
163
-
164
- if @post.save
165
- flash[:notice] = 'Post was successfully created.'
166
- logger.debug "The post was saved and now the user is going to be redirected..."
167
- redirect_to(@post)
160
+ @article = Article.new(params[:article])
161
+ logger.debug "New article: #{@article.attributes.inspect}"
162
+ logger.debug "Article should be valid: #{@article.valid?}"
163
+
164
+ if @article.save
165
+ flash[:notice] = 'Article was successfully created.'
166
+ logger.debug "The article was saved and now the user is going to be redirected..."
167
+ redirect_to(@article)
168
168
  else
169
169
  render action: "new"
170
170
  end
@@ -177,28 +177,28 @@ end
177
177
  Here's an example of the log generated when this controller action is executed:
178
178
 
179
179
  ```
180
- Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
180
+ Processing ArticlesController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
181
181
  Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
182
182
  vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
183
- Parameters: {"commit"=>"Create", "post"=>{"title"=>"Debugging Rails",
183
+ Parameters: {"commit"=>"Create", "article"=>{"title"=>"Debugging Rails",
184
184
  "body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
185
- "authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"posts"}
186
- New post: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
185
+ "authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"articles"}
186
+ New article: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
187
187
  "published"=>false, "created_at"=>nil}
188
- Post should be valid: true
189
- Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
188
+ Article should be valid: true
189
+ Article Create (0.000443) INSERT INTO "articles" ("updated_at", "title", "body", "published",
190
190
  "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
191
191
  'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
192
- The post was saved and now the user is going to be redirected...
193
- Redirected to #<Post:0x20af760>
194
- Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]
192
+ The article was saved and now the user is going to be redirected...
193
+ Redirected to # Article:0x20af760>
194
+ Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/articles]
195
195
  ```
196
196
 
197
197
  Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels to avoid filling your production logs with useless trivia.
198
198
 
199
199
  ### Tagged Logging
200
200
 
201
- When running multi-user, multi-account applications, its often useful
201
+ When running multi-user, multi-account applications, it's often useful
202
202
  to be able to filter the logs using some custom rules. `TaggedLogging`
203
203
  in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
204
204
 
@@ -209,430 +209,591 @@ logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "
209
209
  logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
210
210
  ```
211
211
 
212
- Debugging with the `debugger` gem
212
+ ### Impact of Logs on Performance
213
+ Logging will always have a small impact on performance of your rails app,
214
+ particularly when logging to disk. However, there are a few subtleties:
215
+
216
+ Using the `:debug` level will have a greater performance penalty than `:fatal`,
217
+ as a far greater number of strings are being evaluated and written to the
218
+ log output (e.g. disk).
219
+
220
+ Another potential pitfall is that if you have many calls to `Logger` like this
221
+ in your code:
222
+
223
+ ```ruby
224
+ logger.debug "Person attributes hash: #{@person.attributes.inspect}"
225
+ ```
226
+
227
+ In the above example, There will be a performance impact even if the allowed
228
+ output level doesn't include debug. The reason is that Ruby has to evaluate
229
+ these strings, which includes instantiating the somewhat heavy `String` object
230
+ and interpolating the variables, and which takes time.
231
+ Therefore, it's recommended to pass blocks to the logger methods, as these are
232
+ only evaluated if the output level is the same or included in the allowed level
233
+ (i.e. lazy loading). The same code rewritten would be:
234
+
235
+ ```ruby
236
+ logger.debug {"Person attributes hash: #{@person.attributes.inspect}"}
237
+ ```
238
+
239
+ The contents of the block, and therefore the string interpolation, is only
240
+ evaluated if debug is enabled. This performance savings is only really
241
+ noticeable with large amounts of logging, but it's a good practice to employ.
242
+
243
+ Debugging with the `byebug` gem
213
244
  ---------------------------------
214
245
 
215
- When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.
246
+ When your code is behaving in unexpected ways, you can try printing to logs or
247
+ the console to diagnose the problem. Unfortunately, there are times when this
248
+ sort of error tracking is not effective in finding the root cause of a problem.
249
+ When you actually need to journey into your running source code, the debugger
250
+ is your best companion.
216
251
 
217
- The debugger can also help you if you want to learn about the Rails source code but don't know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.
252
+ The debugger can also help you if you want to learn about the Rails source code
253
+ but don't know where to start. Just debug any request to your application and
254
+ use this guide to learn how to move from the code you have written deeper into
255
+ Rails code.
218
256
 
219
257
  ### Setup
220
258
 
221
- You can use the `debugger` gem to set breakpoints and step through live code in Rails. To install it, just run:
259
+ You can use the `byebug` gem to set breakpoints and step through live code in
260
+ Rails. To install it, just run:
222
261
 
223
262
  ```bash
224
- $ gem install debugger
263
+ $ gem install byebug
225
264
  ```
226
265
 
227
- Rails has had built-in support for debugging since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the `debugger` method.
266
+ Inside any Rails application you can then invoke the debugger by calling the
267
+ `byebug` method.
228
268
 
229
269
  Here's an example:
230
270
 
231
271
  ```ruby
232
272
  class PeopleController < ApplicationController
233
273
  def new
234
- debugger
274
+ byebug
235
275
  @person = Person.new
236
276
  end
237
277
  end
238
278
  ```
239
279
 
240
- If you see this message in the console or logs:
280
+ ### The Shell
281
+
282
+ As soon as your application calls the `byebug` method, the debugger will be
283
+ started in a debugger shell inside the terminal window where you launched your
284
+ application server, and you will be placed at the debugger's prompt `(byebug)`.
285
+ Before the prompt, the code around the line that is about to be run will be
286
+ displayed and the current line will be marked by '=>'. Like this:
241
287
 
242
288
  ```
243
- ***** Debugger requested, but was not available: Start server with --debugger to enable *****
289
+ [1, 10] in /PathTo/project/app/controllers/articles_controller.rb
290
+ 3:
291
+ 4: # GET /articles
292
+ 5: # GET /articles.json
293
+ 6: def index
294
+ 7: byebug
295
+ => 8: @articles = Article.find_recent
296
+ 9:
297
+ 10: respond_to do |format|
298
+ 11: format.html # index.html.erb
299
+ 12: format.json { render json: @articles }
300
+
301
+ (byebug)
244
302
  ```
245
303
 
246
- Make sure you have started your web server with the option `--debugger`:
304
+ If you got there by a browser request, the browser tab containing the request
305
+ will be hung until the debugger has finished and the trace has finished
306
+ processing the entire request.
307
+
308
+ For example:
247
309
 
248
310
  ```bash
249
- $ rails server --debugger
250
311
  => Booting WEBrick
251
- => Rails 3.2.13 application starting on http://0.0.0.0:3000
252
- => Debugger enabled
253
- ...
254
- ```
312
+ => Rails 4.2.0 application starting in development on http://0.0.0.0:3000
313
+ => Run `rails server -h` for more startup options
314
+ => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
315
+ => Ctrl-C to shutdown server
316
+ [2014-04-11 13:11:47] INFO WEBrick 1.3.1
317
+ [2014-04-11 13:11:47] INFO ruby 2.1.1 (2014-02-24) [i686-linux]
318
+ [2014-04-11 13:11:47] INFO WEBrick::HTTPServer#start: pid=6370 port=3000
255
319
 
256
- TIP: In development mode, you can dynamically `require \'debugger\'` instead of restarting the server, even if it was started without `--debugger`.
257
320
 
258
- ### The Shell
321
+ Started GET "/" for 127.0.0.1 at 2014-04-11 13:11:48 +0200
322
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
323
+ Processing by ArticlesController#index as HTML
259
324
 
260
- As soon as your application calls the `debugger` method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at the debugger's prompt `(rdb:n)`. The _n_ is the thread number. The prompt will also show you the next line of code that is waiting to run.
325
+ [3, 12] in /PathTo/project/app/controllers/articles_controller.rb
326
+ 3:
327
+ 4: # GET /articles
328
+ 5: # GET /articles.json
329
+ 6: def index
330
+ 7: byebug
331
+ => 8: @articles = Article.find_recent
332
+ 9:
333
+ 10: respond_to do |format|
334
+ 11: format.html # index.html.erb
335
+ 12: format.json { render json: @articles }
261
336
 
262
- If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.
337
+ (byebug)
338
+ ```
263
339
 
264
- For example:
340
+ Now it's time to explore and dig into your application. A good place to start is
341
+ by asking the debugger for help. Type: `help`
265
342
 
266
- ```bash
267
- @posts = Post.all
268
- (rdb:7)
269
343
  ```
344
+ (byebug) help
270
345
 
271
- Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help. Type: `help`
346
+ byebug 2.7.0
272
347
 
273
- ```
274
- (rdb:7) help
275
- ruby-debug help v0.10.2
276
348
  Type 'help <command-name>' for help on a specific command
277
349
 
278
350
  Available commands:
279
- backtrace delete enable help next quit show trace
280
- break disable eval info p reload source undisplay
281
- catch display exit irb pp restart step up
282
- condition down finish list ps save thread var
283
- continue edit frame method putl set tmate where
351
+ backtrace delete enable help list pry next restart source up
352
+ break disable eval info method ps save step var
353
+ catch display exit interrupt next putl set thread
354
+ condition down finish irb p quit show trace
355
+ continue edit frame kill pp reload skip undisplay
284
356
  ```
285
357
 
286
- TIP: To view the help menu for any command use `help <command-name>` at the debugger prompt. For example: _`help var`_
358
+ TIP: To view the help menu for any command use `help <command-name>` at the
359
+ debugger prompt. For example: _`help list`_. You can abbreviate any debugging
360
+ command by supplying just enough letters to distinguish them from other
361
+ commands, so you can also use `l` for the `list` command, for example.
287
362
 
288
- The next command to learn is one of the most useful: `list`. You can abbreviate any debugging command by supplying just enough letters to distinguish them from other commands, so you can also use `l` for the `list` command.
363
+ To see the previous ten lines you should type `list-` (or `l-`)
289
364
 
290
- This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by `=>`.
291
-
292
- ```
293
- (rdb:7) list
294
- [1, 10] in /PathTo/project/app/controllers/posts_controller.rb
295
- 1 class PostsController < ApplicationController
296
- 2 # GET /posts
297
- 3 # GET /posts.json
298
- 4 def index
299
- 5 debugger
300
- => 6 @posts = Post.all
301
- 7
302
- 8 respond_to do |format|
303
- 9 format.html # index.html.erb
304
- 10 format.json { render :json => @posts }
305
365
  ```
366
+ (byebug) l-
306
367
 
307
- If you repeat the `list` command, this time using just `l`, the next ten lines of the file will be printed out.
368
+ [1, 10] in /PathTo/project/app/controllers/articles_controller.rb
369
+ 1 class ArticlesController < ApplicationController
370
+ 2 before_action :set_article, only: [:show, :edit, :update, :destroy]
371
+ 3
372
+ 4 # GET /articles
373
+ 5 # GET /articles.json
374
+ 6 def index
375
+ 7 byebug
376
+ 8 @articles = Article.find_recent
377
+ 9
378
+ 10 respond_to do |format|
308
379
 
309
380
  ```
310
- (rdb:7) l
311
- [11, 20] in /PathTo/project/app/controllers/posts_controller.rb
312
- 11 end
313
- 12 end
314
- 13
315
- 14 # GET /posts/1
316
- 15 # GET /posts/1.json
317
- 16 def show
318
- 17 @post = Post.find(params[:id])
319
- 18
320
- 19 respond_to do |format|
321
- 20 format.html # show.html.erb
322
- ```
323
-
324
- And so on until the end of the current file. When the end of file is reached, the `list` command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.
325
381
 
326
- On the other hand, to see the previous ten lines you should type `list-` (or `l-`)
382
+ This way you can move inside the file, being able to see the code above and over
383
+ the line where you added the `byebug` call. Finally, to see where you are in
384
+ the code again you can type `list=`
327
385
 
328
386
  ```
329
- (rdb:7) l-
330
- [1, 10] in /PathTo/project/app/controllers/posts_controller.rb
331
- 1 class PostsController < ApplicationController
332
- 2 # GET /posts
333
- 3 # GET /posts.json
334
- 4 def index
335
- 5 debugger
336
- 6 @posts = Post.all
337
- 7
338
- 8 respond_to do |format|
339
- 9 format.html # index.html.erb
340
- 10 format.json { render :json => @posts }
341
- ```
387
+ (byebug) list=
342
388
 
343
- This way you can move inside the file, being able to see the code above and over the line you added the `debugger`.
344
- Finally, to see where you are in the code again you can type `list=`
389
+ [3, 12] in /PathTo/project/app/controllers/articles_controller.rb
390
+ 3:
391
+ 4: # GET /articles
392
+ 5: # GET /articles.json
393
+ 6: def index
394
+ 7: byebug
395
+ => 8: @articles = Article.find_recent
396
+ 9:
397
+ 10: respond_to do |format|
398
+ 11: format.html # index.html.erb
399
+ 12: format.json { render json: @articles }
345
400
 
346
- ```
347
- (rdb:7) list=
348
- [1, 10] in /PathTo/project/app/controllers/posts_controller.rb
349
- 1 class PostsController < ApplicationController
350
- 2 # GET /posts
351
- 3 # GET /posts.json
352
- 4 def index
353
- 5 debugger
354
- => 6 @posts = Post.all
355
- 7
356
- 8 respond_to do |format|
357
- 9 format.html # index.html.erb
358
- 10 format.json { render :json => @posts }
401
+ (byebug)
359
402
  ```
360
403
 
361
404
  ### The Context
362
405
 
363
- When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.
364
-
365
- The debugger creates a context when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.
366
-
367
- At any time you can call the `backtrace` command (or its alias `where`) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then `backtrace` will supply the answer.
368
-
369
- ```
370
- (rdb:5) where
371
- #0 PostsController.index
372
- at line /PathTo/project/app/controllers/posts_controller.rb:6
373
- #1 Kernel.send
374
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
375
- #2 ActionController::Base.perform_action_without_filters
376
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
377
- #3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)
378
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
406
+ When you start debugging your application, you will be placed in different
407
+ contexts as you go through the different parts of the stack.
408
+
409
+ The debugger creates a context when a stopping point or an event is reached. The
410
+ context has information about the suspended program which enables the debugger
411
+ to inspect the frame stack, evaluate variables from the perspective of the
412
+ debugged program, and contains information about the place where the debugged
413
+ program is stopped.
414
+
415
+ At any time you can call the `backtrace` command (or its alias `where`) to print
416
+ the backtrace of the application. This can be very helpful to know how you got
417
+ where you are. If you ever wondered about how you got somewhere in your code,
418
+ then `backtrace` will supply the answer.
419
+
420
+ ```
421
+ (byebug) where
422
+ --> #0 ArticlesController.index
423
+ at /PathTo/project/test_app/app/controllers/articles_controller.rb:8
424
+ #1 ActionController::ImplicitRender.send_action(method#String, *args#Array)
425
+ at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/implicit_render.rb:4
426
+ #2 AbstractController::Base.process_action(action#NilClass, *args#Array)
427
+ at /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb:189
428
+ #3 ActionController::Rendering.process_action(action#NilClass, *args#NilClass)
429
+ at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/rendering.rb:10
379
430
  ...
380
431
  ```
381
432
 
382
- You move anywhere you want in this trace (thus changing the context) by using the `frame _n_` command, where _n_ is the specified frame number.
433
+ The current frame is marked with `-->`. You can move anywhere you want in this
434
+ trace (thus changing the context) by using the `frame _n_` command, where _n_ is
435
+ the specified frame number. If you do that, `byebug` will display your new
436
+ context.
383
437
 
384
438
  ```
385
- (rdb:5) frame 2
386
- #2 ActionController::Base.perform_action_without_filters
387
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
439
+ (byebug) frame 2
440
+
441
+ [184, 193] in /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb
442
+ 184: # is the intended way to override action dispatching.
443
+ 185: #
444
+ 186: # Notice that the first argument is the method to be dispatched
445
+ 187: # which is *not* necessarily the same as the action name.
446
+ 188: def process_action(method_name, *args)
447
+ => 189: send_action(method_name, *args)
448
+ 190: end
449
+ 191:
450
+ 192: # Actually call the method associated with the action. Override
451
+ 193: # this method if you wish to change how action methods are called,
452
+
453
+ (byebug)
388
454
  ```
389
455
 
390
- The available variables are the same as if you were running the code line by line. After all, that's what debugging is.
456
+ The available variables are the same as if you were running the code line by
457
+ line. After all, that's what debugging is.
391
458
 
392
- Moving up and down the stack frame: You can use `up [n]` (`u` for abbreviated) and `down [n]` commands in order to change the context _n_ frames up or down the stack respectively. _n_ defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.
459
+ You can also use `up [n]` (`u` for abbreviated) and `down [n]` commands in order
460
+ to change the context _n_ frames up or down the stack respectively. _n_ defaults
461
+ to one. Up in this case is towards higher-numbered stack frames, and down is
462
+ towards lower-numbered stack frames.
393
463
 
394
464
  ### Threads
395
465
 
396
- The debugger can list, stop, resume and switch between running threads by using the command `thread` (or the abbreviated `th`). This command has a handful of options:
466
+ The debugger can list, stop, resume and switch between running threads by using
467
+ the `thread` command (or the abbreviated `th`). This command has a handful of
468
+ options:
397
469
 
398
470
  * `thread` shows the current thread.
399
- * `thread list` is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
471
+ * `thread list` is used to list all threads and their statuses. The plus +
472
+ character and the number indicates the current thread of execution.
400
473
  * `thread stop _n_` stop thread _n_.
401
474
  * `thread resume _n_` resumes thread _n_.
402
475
  * `thread switch _n_` switches the current thread context to _n_.
403
476
 
404
- This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.
477
+ This command is very helpful, among other occasions, when you are debugging
478
+ concurrent threads and need to verify that there are no race conditions in your
479
+ code.
405
480
 
406
481
  ### Inspecting Variables
407
482
 
408
- Any expression can be evaluated in the current context. To evaluate an expression, just type it!
409
-
410
- This example shows how you can print the instance_variables defined within the current context:
411
-
412
- ```
413
- @posts = Post.all
414
- (rdb:11) instance_variables
415
- ["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]
416
- ```
417
-
418
- As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using `next` (you'll learn more about this command later in this guide).
419
-
420
- ```
421
- (rdb:11) next
422
- Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
423
- Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e
424
- Parameters: {"action"=>"index", "controller"=>"posts"}
425
- /PathToProject/posts_controller.rb:8
426
- respond_to do |format|
483
+ Any expression can be evaluated in the current context. To evaluate an
484
+ expression, just type it!
485
+
486
+ This example shows how you can print the instance variables defined within the
487
+ current context:
488
+
489
+ ```
490
+ [3, 12] in /PathTo/project/app/controllers/articles_controller.rb
491
+ 3:
492
+ 4: # GET /articles
493
+ 5: # GET /articles.json
494
+ 6: def index
495
+ 7: byebug
496
+ => 8: @articles = Article.find_recent
497
+ 9:
498
+ 10: respond_to do |format|
499
+ 11: format.html # index.html.erb
500
+ 12: format.json { render json: @articles }
501
+
502
+ (byebug) instance_variables
503
+ [:@_action_has_layout, :@_routes, :@_headers, :@_status, :@_request,
504
+ :@_response, :@_env, :@_prefixes, :@_lookup_context, :@_action_name,
505
+ :@_response_body, :@marked_for_same_origin_verification, :@_config]
506
+ ```
507
+
508
+ As you may have figured out, all of the variables that you can access from a
509
+ controller are displayed. This list is dynamically updated as you execute code.
510
+ For example, run the next line using `next` (you'll learn more about this
511
+ command later in this guide).
512
+
513
+ ```
514
+ (byebug) next
515
+ [5, 14] in /PathTo/project/app/controllers/articles_controller.rb
516
+ 5 # GET /articles.json
517
+ 6 def index
518
+ 7 byebug
519
+ 8 @articles = Article.find_recent
520
+ 9
521
+ => 10 respond_to do |format|
522
+ 11 format.html # index.html.erb
523
+ 12 format.json { render json: @articles }
524
+ 13 end
525
+ 14 end
526
+ 15
527
+ (byebug)
427
528
  ```
428
529
 
429
530
  And then ask again for the instance_variables:
430
531
 
431
532
  ```
432
- (rdb:11) instance_variables.include? "@posts"
533
+ (byebug) instance_variables.include? "@articles"
433
534
  true
434
535
  ```
435
536
 
436
- Now `@posts` is included in the instance variables, because the line defining it was executed.
537
+ Now `@articles` is included in the instance variables, because the line defining it
538
+ was executed.
437
539
 
438
- TIP: You can also step into **irb** mode with the command `irb` (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.
540
+ TIP: You can also step into **irb** mode with the command `irb` (of course!).
541
+ This way an irb session will be started within the context you invoked it. But
542
+ be warned: this is an experimental feature.
439
543
 
440
- The `var` method is the most convenient way to show variables and their values:
544
+ The `var` method is the most convenient way to show variables and their values.
545
+ Let's let `byebug` to help us with it.
441
546
 
442
547
  ```
443
- var
444
- (rdb:1) v[ar] const <object> show constants of object
445
- (rdb:1) v[ar] g[lobal] show global variables
446
- (rdb:1) v[ar] i[nstance] <object> show instance variables of object
447
- (rdb:1) v[ar] l[ocal] show local variables
548
+ (byebug) help var
549
+ v[ar] cl[ass] show class variables of self
550
+ v[ar] const <object> show constants of object
551
+ v[ar] g[lobal] show global variables
552
+ v[ar] i[nstance] <object> show instance variables of object
553
+ v[ar] l[ocal] show local variables
448
554
  ```
449
555
 
450
- This is a great way to inspect the values of the current context variables. For example:
556
+ This is a great way to inspect the values of the current context variables. For
557
+ example, to check that we have no local variables currently defined.
451
558
 
452
559
  ```
453
- (rdb:9) var local
454
- __dbg_verbose_save => false
560
+ (byebug) var local
561
+ (byebug)
455
562
  ```
456
563
 
457
564
  You can also inspect for an object method this way:
458
565
 
459
566
  ```
460
- (rdb:9) var instance Post.new
461
- @attributes = {"updated_at"=>nil, "body"=>nil, "title"=>nil, "published"=>nil, "created_at"...
567
+ (byebug) var instance Article.new
568
+ @_start_transaction_state = {}
569
+ @aggregation_cache = {}
570
+ @association_cache = {}
571
+ @attributes = {"id"=>nil, "created_at"=>nil, "updated_at"=>nil}
462
572
  @attributes_cache = {}
463
- @new_record = true
573
+ @changed_attributes = nil
574
+ ...
464
575
  ```
465
576
 
466
- TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.
577
+ TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate
578
+ Ruby expressions and display the value of variables to the console.
467
579
 
468
- You can use also `display` to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.
580
+ You can use also `display` to start watching variables. This is a good way of
581
+ tracking the values of a variable while the execution goes on.
469
582
 
470
583
  ```
471
- (rdb:1) display @recent_comments
472
- 1: @recent_comments =
584
+ (byebug) display @articles
585
+ 1: @articles = nil
473
586
  ```
474
587
 
475
- The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use `undisplay _n_` where _n_ is the variable number (1 in the last example).
588
+ The variables inside the displaying list will be printed with their values after
589
+ you move in the stack. To stop displaying a variable use `undisplay _n_` where
590
+ _n_ is the variable number (1 in the last example).
476
591
 
477
592
  ### Step by Step
478
593
 
479
- Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.
594
+ Now you should know where you are in the running trace and be able to print the
595
+ available variables. But lets continue and move on with the application
596
+ execution.
480
597
 
481
- Use `step` (abbreviated `s`) to continue running your program until the next logical stopping point and return control to the debugger.
598
+ Use `step` (abbreviated `s`) to continue running your program until the next
599
+ logical stopping point and return control to the debugger.
482
600
 
483
- TIP: You can also use `step+ n` and `step- n` to move forward or backward `n` steps respectively.
601
+ You may also use `next` which is similar to step, but function or method calls
602
+ that appear within the line of code are executed without stopping.
484
603
 
485
- You may also use `next` which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move _n_ steps.
604
+ TIP: You can also use `step n` or `next n` to move forwards `n` steps at once.
486
605
 
487
- The difference between `next` and `step` is that `step` stops at the next line of code executed, doing just a single step, while `next` moves to the next line without descending inside methods.
606
+ The difference between `next` and `step` is that `step` stops at the next line
607
+ of code executed, doing just a single step, while `next` moves to the next line
608
+ without descending inside methods.
488
609
 
489
- For example, consider this block of code with an included `debugger` statement:
610
+ For example, consider the following situation:
490
611
 
491
612
  ```ruby
492
- class Author < ActiveRecord::Base
493
- has_one :editorial
494
- has_many :comments
613
+ Started GET "/" for 127.0.0.1 at 2014-04-11 13:39:23 +0200
614
+ Processing by ArticlesController#index as HTML
495
615
 
496
- def find_recent_comments(limit = 10)
497
- debugger
498
- @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
499
- end
500
- end
616
+ [1, 8] in /home/davidr/Proyectos/test_app/app/models/article.rb
617
+ 1: class Article < ActiveRecord::Base
618
+ 2:
619
+ 3: def self.find_recent(limit = 10)
620
+ 4: byebug
621
+ => 5: where('created_at > ?', 1.week.ago).limit(limit)
622
+ 6: end
623
+ 7:
624
+ 8: end
625
+
626
+ (byebug)
501
627
  ```
502
628
 
503
- TIP: You can use the debugger while using `rails console`. Just remember to `require "debugger"` before calling the `debugger` method.
629
+ If we use `next`, we want go deep inside method calls. Instead, byebug will go
630
+ to the next line within the same context. In this case, this is the last line of
631
+ the method, so `byebug` will jump to next next line of the previous frame.
504
632
 
505
633
  ```
506
- $ rails console
507
- Loading development environment (Rails 3.2.13)
508
- >> require "debugger"
509
- => []
510
- >> author = Author.first
511
- => #<Author id: 1, first_name: "Bob", last_name: "Smith", created_at: "2008-07-31 12:46:10", updated_at: "2008-07-31 12:46:10">
512
- >> author.find_recent_comments
513
- /PathTo/project/app/models/author.rb:11
514
- )
515
- ```
634
+ (byebug) next
635
+ Next went up a frame because previous frame finished
516
636
 
517
- With the code stopped, take a look around:
637
+ [4, 13] in /PathTo/project/test_app/app/controllers/articles_controller.rb
638
+ 4: # GET /articles
639
+ 5: # GET /articles.json
640
+ 6: def index
641
+ 7: @articles = Article.find_recent
642
+ 8:
643
+ => 9: respond_to do |format|
644
+ 10: format.html # index.html.erb
645
+ 11: format.json { render json: @articles }
646
+ 12: end
647
+ 13: end
518
648
 
519
- ```
520
- (rdb:1) list
521
- [2, 9] in /PathTo/project/app/models/author.rb
522
- 2 has_one :editorial
523
- 3 has_many :comments
524
- 4
525
- 5 def find_recent_comments(limit = 10)
526
- 6 debugger
527
- => 7 @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
528
- 8 end
529
- 9 end
649
+ (byebug)
530
650
  ```
531
651
 
532
- You are at the end of the line, but... was this line executed? You can inspect the instance variables.
652
+ If we use `step` in the same situation, we will literally go the next ruby
653
+ instruction to be executed. In this case, the activesupport's `week` method.
533
654
 
534
655
  ```
535
- (rdb:1) var instance
536
- @attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
537
- @attributes_cache = {}
538
- ```
656
+ (byebug) step
539
657
 
540
- `@recent_comments` hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the `next` command to move on in the code:
658
+ [50, 59] in /PathToGems/activesupport-4.2.0/lib/active_support/core_ext/numeric/time.rb
659
+ 50: ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
660
+ 51: end
661
+ 52: alias :day :days
662
+ 53:
663
+ 54: def weeks
664
+ => 55: ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
665
+ 56: end
666
+ 57: alias :week :weeks
667
+ 58:
668
+ 59: def fortnights
541
669
 
670
+ (byebug)
542
671
  ```
543
- (rdb:1) next
544
- /PathTo/project/app/models/author.rb:12
545
- @recent_comments
546
- (rdb:1) var instance
547
- @attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
548
- @attributes_cache = {}
549
- @comments = []
550
- @recent_comments = []
551
- ```
552
-
553
- Now you can see that the `@comments` relationship was loaded and @recent_comments defined because the line was executed.
554
672
 
555
- If you want to go deeper into the stack trace you can move single `steps`, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.
673
+ This is one of the best ways to find bugs in your code, or perhaps in Ruby on
674
+ Rails.
556
675
 
557
676
  ### Breakpoints
558
677
 
559
- A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.
678
+ A breakpoint makes your application stop whenever a certain point in the program
679
+ is reached. The debugger shell is invoked in that line.
560
680
 
561
- You can add breakpoints dynamically with the command `break` (or just `b`). There are 3 possible ways of adding breakpoints manually:
681
+ You can add breakpoints dynamically with the command `break` (or just `b`).
682
+ There are 3 possible ways of adding breakpoints manually:
562
683
 
563
684
  * `break line`: set breakpoint in the _line_ in the current source file.
564
- * `break file:line [if expression]`: set breakpoint in the _line_ number inside the _file_. If an _expression_ is given it must evaluated to _true_ to fire up the debugger.
565
- * `break class(.|\#)method [if expression]`: set breakpoint in _method_ (. and \# for class and instance method respectively) defined in _class_. The _expression_ works the same way as with file:line.
685
+ * `break file:line [if expression]`: set breakpoint in the _line_ number inside
686
+ the _file_. If an _expression_ is given it must evaluated to _true_ to fire up
687
+ the debugger.
688
+ * `break class(.|\#)method [if expression]`: set breakpoint in _method_ (. and
689
+ \# for class and instance method respectively) defined in _class_. The
690
+ _expression_ works the same way as with file:line.
691
+
692
+
693
+ For example, in the previous situation
566
694
 
567
695
  ```
568
- (rdb:5) break 10
569
- Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10
696
+ [4, 13] in /PathTo/project/app/controllers/articles_controller.rb
697
+ 4: # GET /articles
698
+ 5: # GET /articles.json
699
+ 6: def index
700
+ 7: @articles = Article.find_recent
701
+ 8:
702
+ => 9: respond_to do |format|
703
+ 10: format.html # index.html.erb
704
+ 11: format.json { render json: @articles }
705
+ 12: end
706
+ 13: end
707
+
708
+ (byebug) break 11
709
+ Created breakpoint 1 at /PathTo/project/app/controllers/articles_controller.rb:11
710
+
570
711
  ```
571
712
 
572
- Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
713
+ Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you
714
+ supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
573
715
 
574
716
  ```
575
- (rdb:5) info breakpoints
717
+ (byebug) info breakpoints
576
718
  Num Enb What
577
- 1 y at filters.rb:10
719
+ 1 y at /PathTo/project/app/controllers/articles_controller.rb:11
578
720
  ```
579
721
 
580
- To delete breakpoints: use the command `delete _n_` to remove the breakpoint number _n_. If no number is specified, it deletes all breakpoints that are currently active..
722
+ To delete breakpoints: use the command `delete _n_` to remove the breakpoint
723
+ number _n_. If no number is specified, it deletes all breakpoints that are
724
+ currently active.
581
725
 
582
726
  ```
583
- (rdb:5) delete 1
584
- (rdb:5) info breakpoints
727
+ (byebug) delete 1
728
+ (byebug) info breakpoints
585
729
  No breakpoints.
586
730
  ```
587
731
 
588
732
  You can also enable or disable breakpoints:
589
733
 
590
- * `enable breakpoints`: allow a list _breakpoints_ or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
734
+ * `enable breakpoints`: allow a _breakpoints_ list or all of them if no list is
735
+ specified, to stop your program. This is the default state when you create a
736
+ breakpoint.
591
737
  * `disable breakpoints`: the _breakpoints_ will have no effect on your program.
592
738
 
593
739
  ### Catching Exceptions
594
740
 
595
- The command `catch exception-name` (or just `cat exception-name`) can be used to intercept an exception of type _exception-name_ when there would otherwise be is no handler for it.
741
+ The command `catch exception-name` (or just `cat exception-name`) can be used to
742
+ intercept an exception of type _exception-name_ when there would otherwise be no
743
+ handler for it.
596
744
 
597
745
  To list all active catchpoints use `catch`.
598
746
 
599
747
  ### Resuming Execution
600
748
 
601
- There are two ways to resume execution of an application that is stopped in the debugger:
602
-
603
- * `continue` [line-specification] \(or `c`): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
604
- * `finish` [frame-number] \(or `fin`): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
749
+ There are two ways to resume execution of an application that is stopped in the
750
+ debugger:
751
+
752
+ * `continue` [line-specification] \(or `c`): resume program execution, at the
753
+ address where your script last stopped; any breakpoints set at that address are
754
+ bypassed. The optional argument line-specification allows you to specify a line
755
+ number to set a one-time breakpoint which is deleted when that breakpoint is
756
+ reached.
757
+ * `finish` [frame-number] \(or `fin`): execute until the selected stack frame
758
+ returns. If no frame number is given, the application will run until the
759
+ currently selected frame returns. The currently selected frame starts out the
760
+ most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been
761
+ performed. If a frame number is given it will run until the specified frame
762
+ returns.
605
763
 
606
764
  ### Editing
607
765
 
608
766
  Two commands allow you to open code from the debugger into an editor:
609
767
 
610
- * `edit [file:line]`: edit _file_ using the editor specified by the EDITOR environment variable. A specific _line_ can also be given.
611
- * `tmate _n_` (abbreviated `tm`): open the current file in TextMate. It uses n-th frame if _n_ is specified.
768
+ * `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
769
+ environment variable. A specific _line_ can also be given.
612
770
 
613
771
  ### Quitting
614
772
 
615
- To exit the debugger, use the `quit` command (abbreviated `q`), or its alias `exit`.
773
+ To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
774
+ `exit`.
616
775
 
617
- A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.
776
+ A simple quit tries to terminate all threads in effect. Therefore your server
777
+ will be stopped and you will have to start it again.
618
778
 
619
779
  ### Settings
620
780
 
621
- The `debugger` gem can automatically show the code you're stepping through and reload it when you change it in an editor. Here are a few of the available options:
622
-
623
- * `set reload`: Reload source code when changed.
624
- * `set autolist`: Execute `list` command on every breakpoint.
625
- * `set listsize _n_`: Set number of source lines to list by default to _n_.
626
- * `set forcestep`: Make sure the `next` and `step` commands always move to a new line
781
+ `byebug` has a few available options to tweak its behaviour:
627
782
 
628
- You can see the full list by using `help set`. Use `help set _subcommand_` to learn about a particular `set` command.
783
+ * `set autoreload`: Reload source code when changed (default: true).
784
+ * `set autolist`: Execute `list` command on every breakpoint (default: true).
785
+ * `set listsize _n_`: Set number of source lines to list by default to _n_
786
+ (default: 10)
787
+ * `set forcestep`: Make sure the `next` and `step` commands always move to a new
788
+ line.
629
789
 
630
- TIP: You can save these settings in an `.rdebugrc` file in your home directory. The debugger reads these global settings when it starts.
790
+ You can see the full list by using `help set`. Use `help set _subcommand_` to
791
+ learn about a particular `set` command.
631
792
 
632
- Here's a good start for an `.rdebugrc`:
793
+ TIP: You can save these settings in an `.byebugrc` file in your home directory.
794
+ The debugger reads these global settings when it starts. For example:
633
795
 
634
796
  ```bash
635
- set autolist
636
797
  set forcestep
637
798
  set listsize 25
638
799
  ```
@@ -640,33 +801,59 @@ set listsize 25
640
801
  Debugging Memory Leaks
641
802
  ----------------------
642
803
 
643
- A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.
804
+ A Ruby application (on Rails or not), can leak memory - either in the Ruby code
805
+ or at the C code level.
644
806
 
645
- In this section, you will learn how to find and fix such leaks by using tool such as Valgrind.
807
+ In this section, you will learn how to find and fix such leaks by using tool
808
+ such as Valgrind.
646
809
 
647
810
  ### Valgrind
648
811
 
649
- [Valgrind](http://valgrind.org/) is a Linux-only application for detecting C-based memory leaks and race conditions.
812
+ [Valgrind](http://valgrind.org/) is a Linux-only application for detecting
813
+ C-based memory leaks and race conditions.
650
814
 
651
- There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls `malloc()` but is doesn't properly call `free()`, this memory won't be available until the app terminates.
815
+ There are Valgrind tools that can automatically detect many memory management
816
+ and threading bugs, and profile your programs in detail. For example, if a C
817
+ extension in the interpreter calls `malloc()` but doesn't properly call
818
+ `free()`, this memory won't be available until the app terminates.
652
819
 
653
- For further information on how to install Valgrind and use with Ruby, refer to [Valgrind and Ruby](http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/) by Evan Weaver.
820
+ For further information on how to install Valgrind and use with Ruby, refer to
821
+ [Valgrind and Ruby](http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/)
822
+ by Evan Weaver.
654
823
 
655
824
  Plugins for Debugging
656
825
  ---------------------
657
826
 
658
- There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:
659
-
660
- * [Footnotes](https://github.com/josevalim/rails-footnotes) Every Rails page has footnotes that give request information and link back to your source via TextMate.
661
- * [Query Trace](https://github.com/ntalbott/query_trace/tree/master) Adds query origin tracing to your logs.
662
- * [Query Reviewer](https://github.com/nesquena/query_reviewer) This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
663
- * [Exception Notifier](https://github.com/smartinez87/exception_notification/tree/master) Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
827
+ There are some Rails plugins to help you to find errors and debug your
828
+ application. Here is a list of useful plugins for debugging:
829
+
830
+ * [Footnotes](https://github.com/josevalim/rails-footnotes) Every Rails page has
831
+ footnotes that give request information and link back to your source via
832
+ TextMate.
833
+ * [Query Trace](https://github.com/ntalbott/query_trace/tree/master) Adds query
834
+ origin tracing to your logs.
835
+ * [Query Reviewer](https://github.com/nesquena/query_reviewer) This rails plugin
836
+ not only runs "EXPLAIN" before each of your select queries in development, but
837
+ provides a small DIV in the rendered output of each page with the summary of
838
+ warnings for each query that it analyzed.
839
+ * [Exception Notifier](https://github.com/smartinez87/exception_notification/tree/master)
840
+ Provides a mailer object and a default set of templates for sending email
841
+ notifications when errors occur in a Rails application.
842
+ * [Better Errors](https://github.com/charliesome/better_errors) Replaces the
843
+ standard Rails error page with a new one containing more contextual information,
844
+ like source code and variable inspection.
845
+ * [RailsPanel](https://github.com/dejan/rails_panel) Chrome extension for Rails
846
+ development that will end your tailing of development.log. Have all information
847
+ about your Rails app requests in the browser - in the Developer Tools panel.
848
+ Provides insight to db/rendering/total times, parameter list, rendered views and
849
+ more.
664
850
 
665
851
  References
666
852
  ----------
667
853
 
668
854
  * [ruby-debug Homepage](http://bashdb.sourceforge.net/ruby-debug/home-page.html)
669
855
  * [debugger Homepage](https://github.com/cldwalker/debugger)
856
+ * [byebug Homepage](https://github.com/deivid-rodriguez/byebug)
670
857
  * [Article: Debugging a Rails application with ruby-debug](http://www.sitepoint.com/debug-rails-app-ruby-debug/)
671
858
  * [Ryan Bates' debugging ruby (revised) screencast](http://railscasts.com/episodes/54-debugging-ruby-revised)
672
859
  * [Ryan Bates' stack trace screencast](http://railscasts.com/episodes/24-the-stack-trace)