文档章节

Ruby on Rails Guide to Debugging

Kolosek
 Kolosek
发布于 2018/03/29 17:00
字数 642
阅读 2
Rails">收藏 0

Even the best Rails developers in the world have to debug their code from time to time. Unlike other frameworks, Rails makes it easy to debug your code, so you can limit your downtime and get your applications up and running. Debugging has never been easier!

Byebug

Let's start our debugging session by installing the byebug gem. This gem enables you to temporarily stop the code execution at a breakpoint, which is marked with the keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you will be able to type in commands.

With the byebug, you can see values inside variables defined before the breakpoint, simply by typing their name. The same logic applies to all of the methods accessible in the given code block.

byebug

This Gem offers a huge set of commands, you can find the whole list here. We will just mention the most useful ones:

  • next command enables you to go to the next line, skipping all of the methods invoked by executing current line (unless inside any of them different breakpoint exists),
  • step command is pretty similar to the next command, with the difference that step will go into each invoked method (step-by-step),
  • break command stops the execution of the code,
  • continue command continues the code execution.

All of the debugging gems provide similar functionalities but use a slightly different syntax and semantics. Another popular debugging gem in Rails is pry. If you are a beginner programmer be sure to check it out as well!

Debugging in Production

Debugging gems shouldn't be used in the production - they should be used only in the development mode. You install it as a dev dependencySo, how to debug in production?

Rollbar

One way to do this is by implementing a rollbar. The rollbar provides error tracking software, and it can be integrated into ruby as well, you just need to install it. The whole purpose of the rollbar is to provide you with useful logs of errors which occurred in production.

rollbar

There are multiple reasons you would want this in your application:

  • It reports* all of the unhandled errors and exceptions.
  • It allows manual logging.
  • It stores a bunch of useful information, like http request, requested users, code which invoked an error and many more.
  • It sends email notifications each time some unhandled exception happened on your production server, as well as with scrum software so that each new entry will be automatically transformed into the bugissue or whatever notation supported scrum software uses.

Manually

If you don't use a rollbar, an alternative is to manually go through error logs when bugs happen. This approach is a bit slower since there is no notification that something went wrong, and it can be harder to reproduce the bug on your local machine just by looking at log files.

Even if there are no annoying bugs showing in your code, it is not a time to rest! To make it sure everything is still working as it should, be sure to write some test using RSpec and/or Capybara!

Tips and Tricks

  • You shouldn't fix production errors directly on the production site, it should first be reproduced and fixed on the local machine before pushing to the production.
  • You can fix production errors on the production site only if the error is caused by some server configurations, specific dataset or deeper code bugs from the production database.
  • Backup each production version before pushing a new one, so in the case of emergency, you can simply revert back to the previous version.
  • Use staging (test) servers to check and test your code in a production environment.

Hope this article will help you in your future bug hunt!

This article is originally published on Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
2015 年 Ruby 大盘点

2015 年 Ruby 圈发生了很多有趣的事,让我们跟随 Glenn Goodrich 来回顾一下 15 年 Ruby 的年度标志性事件。 2015 将要结束,这一年对于 Ruby 来说非常重要。如果回顾一下本年度的标志性事件...

OneAPM蓝海讯通
2016/01/21
24
0
TorqueBox 2.3.0 发布,Ruby 应用平台

TorqueBox 2.3.0 发布了,这次发布经过了多次不可预期的延迟,但希望这次的新特性和 bug 修复列表还是让我们值得为之等待,相关链接: Download TorqueBox 2.3.0 (ZIP) Browse Getting Star...

红薯
2013/01/26
315
0
7 本免费的 Ruby 语言编程书籍

找了几本免费的 Ruby 语言编程书籍,在此分享给有同样需求的朋友。 Learn to Program by Chris Pine 对新手很友好的 Ruby 编程书籍,适合没有任何编程经验,同时又想要学习 Ruby 的读者。 Pr...

红薯
2011/05/04
1K
6
ruby on rails document

ruby doc ruby github rails guide

Tmac
2014/07/29
33
0
rvm,gems,rubygems,bundle,gemfile,git,rake,怎么这么多概念,都是干什么用的?

本文系转载 原文:http://yinghuayuan8866.blog.163.com/blog/static/2245702720122909571/ 作者:寒秋雪狼——生活很美 RVM(Ruby Version Manager): is a command-line tool which allow......

Guzai
2012/11/14
2K
1

没有更多内容

加载失败,请刷新页面

加载更多

启动参数

常用启动参数,通过 -Dxx.yy=zz注入应用参数 -Deureka.instance.metadata-map.starkGroup=test3 -Dserver.port=8989 本地调试过程中,可改变端口来启动多个相同服务。修改启动的VM参数即可...

ZH-JSON
5分钟前
3
0
ES配置修改

查看配置 GET /_cluster/settings 修改配置 PUT /_cluster/settings{ "persistent" : { "xpack" : { "monitoring" : { "collection" : { "enabled" : ......

messud4312
17分钟前
2
0
Spring事务传播属性有那么难吗?看这一篇就够了

Spring事务传播属性有那么难吗?看这一篇就够了 笔者文笔功力尚浅,如有不妥,请慷慨指出,必定感激不尽 学习东西要知行合一,如果只是知道理论而没实践过,那么掌握的也不会特别扎实,估计过...

不学无数的程序员
17分钟前
2
0
VMware vSphere ESXi主机的访问控制

在vShpere中,访问ESXi主机的途径很多,如下: ESXi DCUI ESXi Shell ESXi SSH ESXi Host Client vCenter --> vSphere web client / vSphere Client VMware vSphere ESXi主机的访问控制,除了......

大别阿郎
42分钟前
4
0
大神讲解CGI、FastCGI和PHP-FPM关系图解

参考资料 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM:http://www.nowamagic.net/librarys/veda/detail/1319 php中fastcgi和php-fpm是什么东西:https://www.zybuluo.com/phper/note/50231 ......

网络小虾米
51分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部