rails 4.1.6 → 4.2.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +12 -10
- data/guides/CHANGELOG.md +64 -17
- data/guides/Rakefile +21 -6
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/javascripts/guides.js +6 -0
- data/guides/assets/stylesheets/main.css +4 -1
- data/guides/bug_report_templates/action_controller_gem.rb +3 -3
- data/guides/bug_report_templates/action_controller_master.rb +3 -2
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +27 -21
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides.rb +2 -2
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +4 -4
- data/guides/source/3_0_release_notes.md +8 -8
- data/guides/source/3_1_release_notes.md +6 -3
- data/guides/source/3_2_release_notes.md +6 -3
- data/guides/source/4_0_release_notes.md +6 -3
- data/guides/source/4_1_release_notes.md +9 -10
- data/guides/source/4_2_release_notes.md +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -8
- data/guides/source/action_controller_overview.md +25 -8
- data/guides/source/action_mailer_basics.md +97 -29
- data/guides/source/action_view_overview.md +142 -191
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +371 -17
- data/guides/source/active_record_basics.md +25 -24
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +95 -220
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +264 -268
- data/guides/source/active_record_validations.md +23 -13
- data/guides/source/active_support_core_extensions.md +115 -123
- data/guides/source/active_support_instrumentation.md +10 -18
- data/guides/source/api_documentation_guidelines.md +63 -17
- data/guides/source/asset_pipeline.md +259 -120
- data/guides/source/association_basics.md +96 -80
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +32 -7
- data/guides/source/command_line.md +52 -30
- data/guides/source/configuring.md +161 -33
- data/guides/source/contributing_to_ruby_on_rails.md +198 -114
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +440 -286
- data/guides/source/development_dependencies_install.md +47 -36
- data/guides/source/documents.yaml +19 -7
- data/guides/source/engines.md +217 -196
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +359 -219
- data/guides/source/i18n.md +110 -66
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +109 -62
- data/guides/source/layout.html.erb +5 -11
- data/guides/source/layouts_and_rendering.md +26 -26
- data/guides/source/maintenance_policy.md +6 -3
- data/guides/source/nested_model_forms.md +7 -4
- data/guides/source/plugins.md +27 -27
- data/guides/source/rails_application_templates.md +21 -3
- data/guides/source/rails_on_rack.md +8 -5
- data/guides/source/routing.md +113 -73
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +40 -34
- data/guides/source/testing.md +199 -119
- data/guides/source/upgrading_ruby_on_rails.md +289 -31
- data/guides/source/working_with_javascript_in_rails.md +19 -17
- data/guides/w3c_validator.rb +2 -0
- metadata +42 -95
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
@@ -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 @
|
29
|
+
<%= debug @article %>
|
30
30
|
<p>
|
31
31
|
<b>Title:</b>
|
32
|
-
<%= @
|
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
|
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 @
|
58
|
+
<%= simple_format @article.to_yaml %>
|
59
59
|
<p>
|
60
60
|
<b>Title:</b>
|
61
|
-
<%= @
|
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
|
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
|
-
<%= @
|
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
|
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 `
|
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
|
156
|
+
class ArticlesController < ApplicationController
|
157
157
|
# ...
|
158
158
|
|
159
159
|
def create
|
160
|
-
@
|
161
|
-
logger.debug "New
|
162
|
-
logger.debug "
|
163
|
-
|
164
|
-
if @
|
165
|
-
flash[:notice] =
|
166
|
-
logger.debug "The
|
167
|
-
redirect_to(@
|
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,21 +177,21 @@ end
|
|
177
177
|
Here's an example of the log generated when this controller action is executed:
|
178
178
|
|
179
179
|
```
|
180
|
-
Processing
|
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", "
|
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"=>"
|
186
|
-
New
|
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
|
-
|
189
|
-
|
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
|
193
|
-
Redirected to
|
194
|
-
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/
|
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.
|
@@ -211,7 +211,7 @@ logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "
|
|
211
211
|
|
212
212
|
### Impact of Logs on Performance
|
213
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:
|
214
|
+
particularly when logging to disk. However, there are a few subtleties:
|
215
215
|
|
216
216
|
Using the `:debug` level will have a greater performance penalty than `:fatal`,
|
217
217
|
as a far greater number of strings are being evaluated and written to the
|
@@ -240,430 +240,560 @@ The contents of the block, and therefore the string interpolation, is only
|
|
240
240
|
evaluated if debug is enabled. This performance savings is only really
|
241
241
|
noticeable with large amounts of logging, but it's a good practice to employ.
|
242
242
|
|
243
|
-
Debugging with the `
|
243
|
+
Debugging with the `byebug` gem
|
244
244
|
---------------------------------
|
245
245
|
|
246
|
-
When your code is behaving in unexpected ways, you can try printing to logs or
|
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.
|
247
251
|
|
248
|
-
The debugger can also help you if you want to learn about the Rails source 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.
|
249
256
|
|
250
257
|
### Setup
|
251
258
|
|
252
|
-
You can use the `
|
259
|
+
You can use the `byebug` gem to set breakpoints and step through live code in
|
260
|
+
Rails. To install it, just run:
|
253
261
|
|
254
262
|
```bash
|
255
|
-
$ gem install
|
263
|
+
$ gem install byebug
|
256
264
|
```
|
257
265
|
|
258
|
-
|
266
|
+
Inside any Rails application you can then invoke the debugger by calling the
|
267
|
+
`byebug` method.
|
259
268
|
|
260
269
|
Here's an example:
|
261
270
|
|
262
271
|
```ruby
|
263
272
|
class PeopleController < ApplicationController
|
264
273
|
def new
|
265
|
-
|
274
|
+
byebug
|
266
275
|
@person = Person.new
|
267
276
|
end
|
268
277
|
end
|
269
278
|
```
|
270
279
|
|
271
|
-
|
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:
|
272
287
|
|
273
288
|
```
|
274
|
-
|
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)
|
275
302
|
```
|
276
303
|
|
277
|
-
|
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:
|
278
309
|
|
279
310
|
```bash
|
280
|
-
$ rails server --debugger
|
281
311
|
=> Booting WEBrick
|
282
|
-
=> Rails 4.
|
283
|
-
=>
|
284
|
-
|
285
|
-
|
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
|
286
319
|
|
287
|
-
TIP: In development mode, you can dynamically `require 'debugger'` instead of restarting the server, even if it was started without `--debugger`.
|
288
320
|
|
289
|
-
|
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
|
290
324
|
|
291
|
-
|
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 }
|
292
336
|
|
293
|
-
|
337
|
+
(byebug)
|
338
|
+
```
|
294
339
|
|
295
|
-
|
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`
|
296
342
|
|
297
|
-
```bash
|
298
|
-
@posts = Post.all
|
299
|
-
(rdb:7)
|
300
343
|
```
|
344
|
+
(byebug) help
|
301
345
|
|
302
|
-
|
346
|
+
byebug 2.7.0
|
303
347
|
|
304
|
-
```
|
305
|
-
(rdb:7) help
|
306
|
-
ruby-debug help v0.10.2
|
307
348
|
Type 'help <command-name>' for help on a specific command
|
308
349
|
|
309
350
|
Available commands:
|
310
|
-
backtrace delete enable help next
|
311
|
-
break disable eval info
|
312
|
-
catch display exit
|
313
|
-
condition down finish
|
314
|
-
continue edit frame
|
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
|
315
356
|
```
|
316
357
|
|
317
|
-
TIP: To view the help menu for any command use `help <command-name>` at the
|
318
|
-
|
319
|
-
|
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.
|
320
362
|
|
321
|
-
|
363
|
+
To see the previous ten lines you should type `list-` (or `l-`)
|
322
364
|
|
323
365
|
```
|
324
|
-
(
|
325
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
326
|
-
1 class PostsController < ApplicationController
|
327
|
-
2 # GET /posts
|
328
|
-
3 # GET /posts.json
|
329
|
-
4 def index
|
330
|
-
5 debugger
|
331
|
-
=> 6 @posts = Post.all
|
332
|
-
7
|
333
|
-
8 respond_to do |format|
|
334
|
-
9 format.html # index.html.erb
|
335
|
-
10 format.json { render json: @posts }
|
336
|
-
```
|
366
|
+
(byebug) l-
|
337
367
|
|
338
|
-
|
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|
|
339
379
|
|
340
380
|
```
|
341
|
-
(rdb:7) l
|
342
|
-
[11, 20] in /PathTo/project/app/controllers/posts_controller.rb
|
343
|
-
11 end
|
344
|
-
12 end
|
345
|
-
13
|
346
|
-
14 # GET /posts/1
|
347
|
-
15 # GET /posts/1.json
|
348
|
-
16 def show
|
349
|
-
17 @post = Post.find(params[:id])
|
350
|
-
18
|
351
|
-
19 respond_to do |format|
|
352
|
-
20 format.html # show.html.erb
|
353
|
-
```
|
354
|
-
|
355
|
-
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.
|
356
381
|
|
357
|
-
|
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=`
|
358
385
|
|
359
386
|
```
|
360
|
-
(
|
361
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
362
|
-
1 class PostsController < ApplicationController
|
363
|
-
2 # GET /posts
|
364
|
-
3 # GET /posts.json
|
365
|
-
4 def index
|
366
|
-
5 debugger
|
367
|
-
6 @posts = Post.all
|
368
|
-
7
|
369
|
-
8 respond_to do |format|
|
370
|
-
9 format.html # index.html.erb
|
371
|
-
10 format.json { render json: @posts }
|
372
|
-
```
|
387
|
+
(byebug) list=
|
373
388
|
|
374
|
-
|
375
|
-
|
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 }
|
376
400
|
|
377
|
-
|
378
|
-
(rdb:7) list=
|
379
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
380
|
-
1 class PostsController < ApplicationController
|
381
|
-
2 # GET /posts
|
382
|
-
3 # GET /posts.json
|
383
|
-
4 def index
|
384
|
-
5 debugger
|
385
|
-
=> 6 @posts = Post.all
|
386
|
-
7
|
387
|
-
8 respond_to do |format|
|
388
|
-
9 format.html # index.html.erb
|
389
|
-
10 format.json { render json: @posts }
|
401
|
+
(byebug)
|
390
402
|
```
|
391
403
|
|
392
404
|
### The Context
|
393
405
|
|
394
|
-
When you start debugging your application, you will be placed in different
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
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
|
410
430
|
...
|
411
431
|
```
|
412
432
|
|
413
|
-
|
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.
|
414
437
|
|
415
438
|
```
|
416
|
-
(
|
417
|
-
|
418
|
-
|
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)
|
419
454
|
```
|
420
455
|
|
421
|
-
The available variables are the same as if you were running the code line by
|
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.
|
422
458
|
|
423
|
-
|
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.
|
424
463
|
|
425
464
|
### Threads
|
426
465
|
|
427
|
-
The debugger can list, stop, resume and switch between running threads by using
|
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:
|
428
469
|
|
429
470
|
* `thread` shows the current thread.
|
430
|
-
* `thread list` is used to list all threads and their statuses. The plus +
|
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.
|
431
473
|
* `thread stop _n_` stop thread _n_.
|
432
474
|
* `thread resume _n_` resumes thread _n_.
|
433
475
|
* `thread switch _n_` switches the current thread context to _n_.
|
434
476
|
|
435
|
-
This command is very helpful, among other occasions, when you are debugging
|
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.
|
436
480
|
|
437
481
|
### Inspecting Variables
|
438
482
|
|
439
|
-
Any expression can be evaluated in the current context. To evaluate an
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
[
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
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)
|
458
528
|
```
|
459
529
|
|
460
530
|
And then ask again for the instance_variables:
|
461
531
|
|
462
532
|
```
|
463
|
-
(
|
533
|
+
(byebug) instance_variables.include? "@articles"
|
464
534
|
true
|
465
535
|
```
|
466
536
|
|
467
|
-
Now `@
|
537
|
+
Now `@articles` is included in the instance variables, because the line defining it
|
538
|
+
was executed.
|
468
539
|
|
469
|
-
TIP: You can also step into **irb** mode with the command `irb` (of course!).
|
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.
|
470
543
|
|
471
|
-
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.
|
472
546
|
|
473
547
|
```
|
474
|
-
var
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
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
|
479
554
|
```
|
480
555
|
|
481
|
-
This is a great way to inspect the values of the current context variables. For
|
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.
|
482
558
|
|
483
559
|
```
|
484
|
-
(
|
485
|
-
|
560
|
+
(byebug) var local
|
561
|
+
(byebug)
|
486
562
|
```
|
487
563
|
|
488
564
|
You can also inspect for an object method this way:
|
489
565
|
|
490
566
|
```
|
491
|
-
(
|
492
|
-
@
|
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}
|
493
572
|
@attributes_cache = {}
|
494
|
-
@
|
573
|
+
@changed_attributes = nil
|
574
|
+
...
|
495
575
|
```
|
496
576
|
|
497
|
-
TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate
|
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.
|
498
579
|
|
499
|
-
You can use also `display` to start watching variables. This is a good way of
|
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.
|
500
582
|
|
501
583
|
```
|
502
|
-
(
|
503
|
-
1: @
|
584
|
+
(byebug) display @articles
|
585
|
+
1: @articles = nil
|
504
586
|
```
|
505
587
|
|
506
|
-
The variables inside the displaying list will be printed with their values after
|
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).
|
507
591
|
|
508
592
|
### Step by Step
|
509
593
|
|
510
|
-
Now you should know where you are in the running trace and be able to print the
|
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.
|
511
597
|
|
512
|
-
Use `step` (abbreviated `s`) to continue running your program until the next
|
598
|
+
Use `step` (abbreviated `s`) to continue running your program until the next
|
599
|
+
logical stopping point and return control to the debugger.
|
513
600
|
|
514
|
-
|
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.
|
515
603
|
|
516
|
-
You
|
604
|
+
TIP: You can also use `step n` or `next n` to move forwards `n` steps at once.
|
517
605
|
|
518
|
-
The difference between `next` and `step` is that `step` stops at the next line
|
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.
|
519
609
|
|
520
|
-
For example, consider
|
610
|
+
For example, consider the following situation:
|
521
611
|
|
522
612
|
```ruby
|
523
|
-
|
524
|
-
|
525
|
-
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
|
526
615
|
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
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)
|
532
627
|
```
|
533
628
|
|
534
|
-
|
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.
|
535
632
|
|
536
633
|
```
|
537
|
-
|
538
|
-
|
539
|
-
>> require "debugger"
|
540
|
-
=> []
|
541
|
-
>> author = Author.first
|
542
|
-
=> #<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">
|
543
|
-
>> author.find_recent_comments
|
544
|
-
/PathTo/project/app/models/author.rb:11
|
545
|
-
)
|
546
|
-
```
|
634
|
+
(byebug) next
|
635
|
+
Next went up a frame because previous frame finished
|
547
636
|
|
548
|
-
|
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
|
549
648
|
|
550
|
-
|
551
|
-
(rdb:1) list
|
552
|
-
[2, 9] in /PathTo/project/app/models/author.rb
|
553
|
-
2 has_one :editorial
|
554
|
-
3 has_many :comments
|
555
|
-
4
|
556
|
-
5 def find_recent_comments(limit = 10)
|
557
|
-
6 debugger
|
558
|
-
=> 7 @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
|
559
|
-
8 end
|
560
|
-
9 end
|
649
|
+
(byebug)
|
561
650
|
```
|
562
651
|
|
563
|
-
|
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.
|
564
654
|
|
565
655
|
```
|
566
|
-
(
|
567
|
-
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
|
568
|
-
@attributes_cache = {}
|
569
|
-
```
|
656
|
+
(byebug) step
|
570
657
|
|
571
|
-
|
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
|
572
669
|
|
670
|
+
(byebug)
|
573
671
|
```
|
574
|
-
(rdb:1) next
|
575
|
-
/PathTo/project/app/models/author.rb:12
|
576
|
-
@recent_comments
|
577
|
-
(rdb:1) var instance
|
578
|
-
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
|
579
|
-
@attributes_cache = {}
|
580
|
-
@comments = []
|
581
|
-
@recent_comments = []
|
582
|
-
```
|
583
|
-
|
584
|
-
Now you can see that the `@comments` relationship was loaded and @recent_comments defined because the line was executed.
|
585
672
|
|
586
|
-
|
673
|
+
This is one of the best ways to find bugs in your code, or perhaps in Ruby on
|
674
|
+
Rails.
|
587
675
|
|
588
676
|
### Breakpoints
|
589
677
|
|
590
|
-
A breakpoint makes your application stop whenever a certain point in the program
|
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.
|
591
680
|
|
592
|
-
You can add breakpoints dynamically with the command `break` (or just `b`).
|
681
|
+
You can add breakpoints dynamically with the command `break` (or just `b`).
|
682
|
+
There are 3 possible ways of adding breakpoints manually:
|
593
683
|
|
594
684
|
* `break line`: set breakpoint in the _line_ in the current source file.
|
595
|
-
* `break file:line [if expression]`: set breakpoint in the _line_ number inside
|
596
|
-
|
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
|
597
694
|
|
598
695
|
```
|
599
|
-
|
600
|
-
|
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
|
+
|
601
711
|
```
|
602
712
|
|
603
|
-
Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you
|
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.
|
604
715
|
|
605
716
|
```
|
606
|
-
(
|
717
|
+
(byebug) info breakpoints
|
607
718
|
Num Enb What
|
608
|
-
|
719
|
+
1 y at /PathTo/project/app/controllers/articles_controller.rb:11
|
609
720
|
```
|
610
721
|
|
611
|
-
To delete breakpoints: use the command `delete _n_` to remove the breakpoint
|
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.
|
612
725
|
|
613
726
|
```
|
614
|
-
(
|
615
|
-
(
|
727
|
+
(byebug) delete 1
|
728
|
+
(byebug) info breakpoints
|
616
729
|
No breakpoints.
|
617
730
|
```
|
618
731
|
|
619
732
|
You can also enable or disable breakpoints:
|
620
733
|
|
621
|
-
* `enable breakpoints`: allow a list
|
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.
|
622
737
|
* `disable breakpoints`: the _breakpoints_ will have no effect on your program.
|
623
738
|
|
624
739
|
### Catching Exceptions
|
625
740
|
|
626
|
-
The command `catch exception-name` (or just `cat exception-name`) can be used to
|
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.
|
627
744
|
|
628
745
|
To list all active catchpoints use `catch`.
|
629
746
|
|
630
747
|
### Resuming Execution
|
631
748
|
|
632
|
-
There are two ways to resume execution of an application that is stopped in the
|
633
|
-
|
634
|
-
|
635
|
-
* `
|
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.
|
636
763
|
|
637
764
|
### Editing
|
638
765
|
|
639
766
|
Two commands allow you to open code from the debugger into an editor:
|
640
767
|
|
641
|
-
* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
|
642
|
-
|
768
|
+
* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
|
769
|
+
environment variable. A specific _line_ can also be given.
|
643
770
|
|
644
771
|
### Quitting
|
645
772
|
|
646
|
-
To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
|
773
|
+
To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
|
774
|
+
`exit`.
|
647
775
|
|
648
|
-
A simple quit tries to terminate all threads in effect. Therefore your server
|
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.
|
649
778
|
|
650
779
|
### Settings
|
651
780
|
|
652
|
-
|
653
|
-
|
654
|
-
* `set reload`: Reload source code when changed.
|
655
|
-
* `set autolist`: Execute `list` command on every breakpoint.
|
656
|
-
* `set listsize _n_`: Set number of source lines to list by default to _n_.
|
657
|
-
* `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:
|
658
782
|
|
659
|
-
|
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.
|
660
789
|
|
661
|
-
|
790
|
+
You can see the full list by using `help set`. Use `help set _subcommand_` to
|
791
|
+
learn about a particular `set` command.
|
662
792
|
|
663
|
-
|
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:
|
664
795
|
|
665
796
|
```bash
|
666
|
-
set autolist
|
667
797
|
set forcestep
|
668
798
|
set listsize 25
|
669
799
|
```
|
@@ -671,35 +801,59 @@ set listsize 25
|
|
671
801
|
Debugging Memory Leaks
|
672
802
|
----------------------
|
673
803
|
|
674
|
-
A Ruby application (on Rails or not), can leak memory - either in the Ruby code
|
804
|
+
A Ruby application (on Rails or not), can leak memory - either in the Ruby code
|
805
|
+
or at the C code level.
|
675
806
|
|
676
|
-
In this section, you will learn how to find and fix such leaks by using tool
|
807
|
+
In this section, you will learn how to find and fix such leaks by using tool
|
808
|
+
such as Valgrind.
|
677
809
|
|
678
810
|
### Valgrind
|
679
811
|
|
680
|
-
[Valgrind](http://valgrind.org/) is a Linux-only application for detecting
|
812
|
+
[Valgrind](http://valgrind.org/) is a Linux-only application for detecting
|
813
|
+
C-based memory leaks and race conditions.
|
681
814
|
|
682
|
-
There are Valgrind tools that can automatically detect many memory management
|
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.
|
683
819
|
|
684
|
-
For further information on how to install Valgrind and use with Ruby, refer to
|
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.
|
685
823
|
|
686
824
|
Plugins for Debugging
|
687
825
|
---------------------
|
688
826
|
|
689
|
-
There are some Rails plugins to help you to find errors and debug your
|
690
|
-
|
691
|
-
|
692
|
-
* [
|
693
|
-
|
694
|
-
|
695
|
-
* [
|
696
|
-
|
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.
|
697
850
|
|
698
851
|
References
|
699
852
|
----------
|
700
853
|
|
701
854
|
* [ruby-debug Homepage](http://bashdb.sourceforge.net/ruby-debug/home-page.html)
|
702
855
|
* [debugger Homepage](https://github.com/cldwalker/debugger)
|
856
|
+
* [byebug Homepage](https://github.com/deivid-rodriguez/byebug)
|
703
857
|
* [Article: Debugging a Rails application with ruby-debug](http://www.sitepoint.com/debug-rails-app-ruby-debug/)
|
704
858
|
* [Ryan Bates' debugging ruby (revised) screencast](http://railscasts.com/episodes/54-debugging-ruby-revised)
|
705
859
|
* [Ryan Bates' stack trace screencast](http://railscasts.com/episodes/24-the-stack-trace)
|