`
sillycat
  • 浏览: 2559783 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Rails Study(14)Debugging Rails Applications

阅读更多
Rails Study(14)Debugging Rails Applications

1. View Helpers for Debugging
To inspect the contents of a variable. We have 3 ways:
debug
to_yaml
inspect

1.1 debug
if we have this kind of code in a view:
<%= debug @post %>
<p>
  <b>Title:</b>
  <%=h @post.title %>
</p>

I will see something like this:
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05
body:..
title: Rails debuging guide

1.2 to_yaml
<%= simple_format @post.to_yaml %>
<p>
  <b>Title:</b>
  <%=h @post.title %>
</p>

The to_yaml method converts the method to YAML format leaving it more readable, and then the simple_format helper is used to render each line as in the console.
--- !ruby/object:Post
attributes:
updated_at: 2008

1.3 inspect
It will print the object value as a string when working with arrays or hashes.
<%= [1,2,3,4,5].inspect %>
<p>
  <b>Title:</b>
  <%=h @post.title %>
</p>

1.4 Debugging RJS
The flag to enable RJS debugging in your configuration files is config.action_view.debug_rjs:
config.action_view.debug_rjs = true

or set this at any time
ActionView::Base.debug_rjs = true

2. The Logger
Rails maintains a separate log file for each runtime environment.

2.1 What is the Logger?
In evnrionment.rb
Rails.logger = Logger.new(STDOUT)
Rails.logger = Log4r::Logger.new("Application Log")

By default, each log is created under Rails.root/log/ and the log file name is environment_name.log.

2.2 Log Levels
If you want to know the current log level you can call the Rails.logger.level method.

The available log levels are: :debug, :info, :warn, :error, and :fatal. To change the default log level, use
config.log_level = Logger::WARN #In any environment initializer, or
Rails.logger.level = 0 #at any time

2.3 Sending Messages
To write message in logger.

logger.debug "Person attributes hash: #{@person.attributes.inspect}"
logger.info "Processing the request..."
logger.fatal "terminating application!!!"

3 Debugging with ruby-debug
3.1 Setup
>gem install ruby-debug
error messages:
ERROR:  Error installing ruby-debug:
        rbx-require-relative requires Ruby version ~> 1.8.7.
solutions:
>gem install ruby-debug19
add this to Gemfile
ruby-debug19

Inside any Rails application you can invoke the debugger by calling the debugger method. For example:
class PeopleController < ApplicationController
def new
  debugger
  @person = Person.new
end
end

if you see the message like this:
***** Debugger requested, but was not available: Start server with --debugger to enable *****
That is not right, you need to start your application like this:
>rails server --debugger

3.2 The Shell
(rdb:7) help ---- show the help information

(rdb:7) list ------- list where we are about the codes

(rdb:7) l ----------- list the below lines in the codes

3.3 The Context
3.4 Threads
3.5 Inspecting Variables
Any expression can be evaluated in the current context. To evaluate an expression, just type it!

@posts = Post.find(:all)
(rdb:11) instance_variables
>(rdb:11) instance_variables.include? "@posts"

>(rdb:1) display @recent_comments

3.6 Step by Step
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. For example:
class Author < ActiveRecord::Base
has_one :editorial
has_many :comments

def find_recent_comments(limit = 10)
  debugger
  @recent_comments ||= comments.find(
    :all,
    :conditions => [ "created_at > ?", 1.week.ago],
    :limit => limit
  )
end
end

>rails console
>> require "ruby-debug"
>> (rdb:1) list
>> (rdb:1) var instance
>> (rdb:1) next

3.7 Breakpoints
3.8 Catching Exceptions
3.9 Resuming Excecution
3.10 Editing
3.11 Quitting
3.12 Settings

4 Debugging Memory Leaks
4.1 BleakHouse
BleakHouse is a library for finding memory leaks.
>gem install bleak_house
error messages:
ERROR:  Error installing bleak_house:
        ERROR: Failed to build gem native extension.

        d:/tool/Ruby192/bin/ruby.exe extconf.rb
-%{ BUILDING RUBY }%-
build_ruby.rb:33:in `chdir': No such file or directory - /tmp/ (Errno::ENOENT)
        from build_ruby.rb:33:in `<main>'
solutions:
do not try to install this on win7, try on redhat or ubuntu.

To setup my applicatiion for profiling, add the following at the bottom of config/environment.rb
require 'bleak_house' if ENV['BLEAK_HOUSE']

start server like this:
>RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house rails server

Make sure to run a couple hundred requests to get better data samples. Press CTRL-C and the bleak house will produce a dumpfile in /tmp:

4.2 Valgrind
Valgrind is a Linux-only application for detecting C-based memory leaks and race conditions.

5 Plugins for Debugging

references:
http://guides.rubyonrails.org/debugging_rails_applications.html
http://www.tatvartha.com/2011/08/upgrading-to-ruby-1-9-rbx-require-relative-requires-ruby-version-1-8-7/

分享到:
评论

相关推荐

    [Rails] Crafting Rails Applications (英文版)

    [Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...

    component base rails applications

    本书《Component-Based Rails Applications》主要介绍了如何使用Rails引擎(Rails Engine)进行基于组件的Rails应用开发,以及如何对应用程序的大型模块进行拆分和模块化。以下是书中一些核心知识点的详细说明: 1....

    Crafting Rails 4 Applications.pdf

    一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读

    [Rails] Crafting Rails 4 Applications (英文版)

    [Pragmatic Bookshelf] Crafting Rails 4 Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Get ready to see Rails as you've never seen it before. Learn how to ...

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    综上所述,《Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf》是一个全面的资源,无论你是Rails新手还是资深开发者,都能从中学习到如何为Rails应用编写高质量的测试。从理论到实践,从单元测试...

    Crafting Rails 4 Applications

    ### 关于《Crafting Rails 4 Applications》的关键知识点解析 #### 标题解析:Crafting Rails 4 Applications - **Rails 4**:Rails 4是Ruby on Rails框架的一个版本,该版本在2013年发布。Ruby on Rails(简称...

    Crafting Rails Applications

    本书《Crafting Rails Applications》是一本专为中级至高级Rails开发者编写的指南,旨在帮助读者深入了解Rails框架的工作原理及其高级特性。作者José Valim不仅是一位资深的Ruby on Rails开发者,也是Rails核心团队...

    Rails 101 入门电子书

    ### Rails 101 入门电子书知识点详解 #### 一、简介 《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

    rails2-sample

    从给定的文件信息来看,我们正在探讨的是一本关于Ruby on Rails的书籍,书名为《Simply Rails2》,作者是Patrick Lenz。本书旨在为初学者提供深入理解Ruby on Rails框架的指南,从基础概念到高级主题均有涵盖,是...

    Rails101_by_rails4.0

    《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    ### Ruby on Rails Guides v2 - Ruby on Rails 4.2.5 #### 一、重要概念及基础假设 - **重要概念**:本指南旨在帮助读者深入理解Ruby on Rails(以下简称Rails)4.2.5版本的核心功能与最佳实践。 - **基础假设**:...

    Ajax on Rails

    Learn to build dynamic, interactive web applications using the two most important approaches to web development today: Ajax and the phenomenally efficient Ruby on Rails platform. This book teaches ...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    rails指南 中文版

    Rails指南中文版是针对Ruby on Rails框架的一份详尽教程,旨在帮助开发者深入理解并熟练掌握这个强大的Web应用开发工具。Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-...

Global site tag (gtag.js) - Google Analytics