Wednesday, December 1, 2010

Tips to inspect the code in Rails


  • Inspect


It inspects the value. It formats the array with square brace, hash with curly braces with arrows between the key value pairs

inspecting parameter.

  • Type


It identifying the type of the instance variable.

  • Debug


It give nice, easy to read inspection of the value and displayed in a formatted way.

These are all some good ways to see what is happening inside your application code.

Example

action inside the controller

def current_status

@my_array = [1,'king', 200]

@my_hash = {'name'=> 'Pinto'}

end

in view

<%= @my_array.inspect %>

<%= @my_hash.inspect %>

<%= params.inspect %>

Result:

[1,"king", 200]

{'name'=> 'Pinto'}

{"action"=>current_status, "controller"=>sample}

To inspect the data type use 'type':

in view

<%= @my_array.type %>

<%= @my_hash.type %>

Result:

Array

Hash

Another inspection method 'debug' :

in view

<%= debug(@my_array) %>

<%= debug(@my_hash) %>

Result:

---

-1

-king

-200

---

name: Pinto

0 comments:

Post a Comment