egman24
1/20/2013 - 9:07 AM

rails c

rails c

RESOURCES:
  http://railsonedge.blogspot.com/2008/05/intro-to-rails-console.html
  http://37signals.com/svn/posts/3176-three-quick-rails-console-tips
  http://slash7.com/2006/12/21/secrets-of-the-rails-console-ninjas/


# Want to make sure gems can be required, make sure to add them to group in Gemfile:
# http://www.ruby-forum.com/topic/1788517#1001063

  group :console do
    gem 'awesome_print'
    gem 'pry'
  end

# Want to load things by default during start of the console:
# irb/rails console.rc? (.irbrc)

  require 'awesome_print'
  require 'pry'


# Using awesome_print to make output clearer
# use 'ap' to print with color and structure

  > ap Object.methods

# Using pry to discover method implementations ( or search the rdocs? http://railsapi.com/doc/rails-v3.2.6/ ):

  > pry
  > show-method Object.method   

# Tips and Tricks:
  * to not mess with database, make sure to run: 'rails console --sandbox'
  * choose the environment to run against: 'rails console [environment]'
  * tab completion is very helpful to find all Classes/objects/instances -- and then available methods after '.'
  * '.class', '.superclass', '.subclasses', '.parent', '.parent_name',
    '.object_id',
    '.methods', 
    '.private_methods', 
    '.public_methods', 
    '.public_instance_methods',
    '.public_constant',
    '.public_class_method',
    '.protected_methods' 
    '.send(:method)', '.inspect'
  * Get list of all available Database objects in the specific project: 'ActiveRecord::Base.connection.tables' (create short alias in .irbrc)
  * .methods.grep(/^().*/) #find all methods associated with object/instance