CSC/ECE 517 Fall 2013/ch1 1w12 vn: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 29: Line 29:


attributes:
attributes:
id: 2
id: 2



Revision as of 03:00, 17 September 2013

Introduction

Rails is an open-source web application framework which makes use of Ruby programming language. With its huge library of gems and support for MVC architecture, it provides an easy and clean approach for creating pages, talking to web server and dealing with databases. Debugging is always an important part of any application development for which Rails provides a tremendously good support despite being an interpreted language as opposed to C or Java or any other compiled language.

Debugging Options

Rails provides a range of options to make debugging easier. Some of these options have been discussed in the sections below.

Debug Helpers

One of the easiest ways to debug is to simply output the value of different variables which provides us the first look into what could be going wrong. This could be done in all of models, views and controllers. Rails provides three methods-debug, to_yaml and inspect to achieve this task. These methods create human-readable data from any object [1]. The examples below show the output when these methods are used in a view.

Code Output
<%= @users.to_yaml %> --- - !ruby/object:User attributes: id: 2 name: Jimmy Page email: jimmy.page@gmail.com created_at: 2013-09-14 02:37:16.322638000 Z updated_at: 2013-09-16 20:38:04.250678000 Z
<%= @users.inspect %> #<ActiveRecord::Relation [#<User id: 2, name: "Jimmy Page", email: "jimmy.page@gmail.com", created_at: "2013-09-14 02:37:16", updated_at: "2013-09-16 20:38:04">]>
<%= debug @users %> ---

- !ruby/object:User

attributes: id: 2

name: Jimmy Page

email: jimmy.page@gmail.com

created_at: 2013-09-14 02:37:16.322638000 Z

updated_at: 2013-09-16 20:38:04.250678000 Z

Logger

Logger class in Ruby helps us save information at runtime . A log file gives information like : various sql queries related to the database, methods executed on the controller, attributes of the controller. Default logger for Rails is ActiveSupport::Logger and log files varies for different runtime environments. There are 5 logging levels and the default logger level for development and production environment is debug and info respectively.

Error Name Error Level Comments
FATAL 4 (highest) An error that cannot be handled and results in a program crash
ERROR 3 An error that cannot be handled
WARN 2 A warning
INFO 1 Information about system operation
DEBUG 0 (lowest) Information for developers


The table below lists down various useful commands/methods for debugging purposes.

Purpose Command/Method/Procedure
Change default log level config.log_level = 0

Rails.logger.level = 0 The above changes are done in environment.rb file.

Configure a new logger config.logger = Logger.new(..)

We can also specify a logger x in our environment file as Rails.logger = x::Logger.new(..)

Clear log messages rake log:clear
Display log file information on terminal window(UNIX) tail -f log/development.log
Put custom messages for debug in models/controllers/views logger.debug

logger.debug_variables

Put custom messages for debug in any other custom class RAILS_DEFAULT_LOGGER
Change format of logger messages Override method

format_messages(level, time, progname, message)

Ruby-debug Gem

This is another way to debug Ruby application where we need to dig deep into our code for finding the root cause of a problem. We use debugger gem for this process. Tool used earlier for debugging was: “script-breakpointer”. Before staring debugging, your web-server must be started with the option –debugger. Since Rails 2.0, Rails has had built-in support for debugging.

Steps to be followed are mentioned below  :

1. Install debugger gem :

 gem ruby-debug –y  (Here, -y is for dependencies)

2. Go to config file and add below command :

 require ‘ruby-debug’

3. Invoke the debugger, call ‘debugger’ method on file. Put a breakpoint using ‘debugger’ in a file , find the bug and start the server again.

 rails server --debugger

4. Now, a debugger prompt will be opened which will look like this:

5. Use ‘help’ for available commands. We can also get a list of directives with "help", or "help backtrace" for the usage of specific directive. 'list', to take a look at where the application stops. Some useful commands are mentioned below :

a. Backtrace : to check all the previous code. To move inside the trace, use frame_n_command b. Thread : to switch between threads, list the ongoing thread, its status, to stop a thread, resume and to switch its context. c. List : lists all the code d. info breakpoints _n_ or info break _n : list breakpoints

Some other useful commands are ‘exit’, ‘finish’,‘step’, ‘continue’, ‘next’, ‘print’.

You can use ‘var’ method to print the variables and ‘display’ to watch the variables. To stop displaying, we use ‘undisplay_variableNumber’. You can further debug into the code with ‘irb’ command. We can open as many irb sessions.

Pry Gem

Graphical Debugger

Rubymine, a commercial IDE by JetBrains, has a graphical debugger for ruby and rails code. It provides important and useful features such as smart breakpoints, dedicated view for watches and stack, expression evaluator, etc. Some of the key features related to debugging are presented below.

  • Breakpoint

One can put breakpoint by simply clicking on the start of the line in the window or pressing Ctrl+Shift+F8. Breakpoint feature highlights the corresponding line in the code.

  • Frames, Variables and Watches

Rubymine debugger shows a separate window for frames, variables and watches when the execution hits a breakpoint. User can look in the corresponding window for more information.

  • Evaluate code and expressions

Users can type in expression or code and evaluate it when the breakpoint is hit or on the fly.


  • Options

Rubymine debugger provides a lot of other options for debugging.


Conclusion

References

[1] http://guides.rubyonrails.org/debugging_rails_applications.html

[2] http://railscasts.com/episodes/54-debugging-with-ruby-debug

[3] http://pryrepl.org/

[4] http://railscasts.com/episodes/56-the-logger

[5] http://www.jetbrains.com/ruby/features/ruby_debugger.html

[6] http://en.wikipedia.org/wiki/RubyMine