Rewbie Newbie

Documenting my path on becoming a Rails Developer.

A Few Tools for Navigating the Sea of Methods

When I encounter an unfamiliar object in Ruby, my instinct is to look and see what classes, modules and methods I am dealing with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Automobile
  attr_accessor :make, :model, :type
end

car = Automobile.new

Automobile.ancestors
car.class.ancestors   #ancestors can only be called on a class object
car.methods.sort
Automobile.instance_methods(false)
car.public_methods(false)
meth = car.method(:make)
meth.parameters
meth.owner
meth.source_location
#etc...

But typing numerous lines of code is not a practical solution to this reoccurring problem. Luckily more than a couple of people have been frustrated by this problem and they decided to provide a better solution.

Use the ‘looksee’ gem to get a better overview of the class and methods structure.

Simply call the ‘ls’ method on any object and looksee will list the inheritance structure along with what methods each class inheritance provides to the object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#This codeblock assumes both the class defintion for Automobile and #the car instance are in memory.

require 'looksee'

car.ls

###console output follows###

=> BasicObject
  !       __send__       instance_exec             singleton_method_undefined
  !=      equal?         method_missing
  ==      initialize     singleton_method_added
  __id__  instance_eval  singleton_method_removed
Kernel
  !~                       gem                         puts
  <=>                      gem_original_require        raise
  ===                      gets                        rand
  =~                       global_variables            readline
  Array                    hash                        readlines
  Complex                  initialize_clone            remove_instance_variable
  Float                    initialize_copy             require
  Integer                  initialize_dup              require_relative
  Rational                 inspect                     respond_to?
  String                   instance_of?                respond_to_missing?
  __callee__               instance_variable_defined?  select
  __method__               instance_variable_get       send
  `                        instance_variable_set       set_trace_func          
  abort                    instance_variables          singleton_class         
  at_exit                  is_a?                       singleton_methods       
  autoload                 iterator?                   sleep                   
  autoload?                kind_of?                    spawn                   
  binding                  lambda                      sprintf                 
  block_given?             load                        srand                   
  caller                   local_variables             syscall                 
  catch                    loop                        system                  
  class                    method                      taint                   
  clone                    methods                     tainted?                
  define_singleton_method  nil?                        tap                     
  display                  object_id                   test                    
  dup                      open                        throw                   
  enum_for                 p                           to_enum                 
  eql?                     pp                          to_s                    
  eval                     pretty_inspect              trace_var               
  exec                     print                       trap                    
  exit                     printf                      trust                   
  exit!                    private_methods             untaint                 
  extend                   proc                        untrace_var             
  fail                     protected_methods           untrust                 
  fork                     public_method               untrusted?              
  format                   public_methods              warn                    
  freeze                   public_send               
  frozen?                  putc                      
PP::ObjectMixin
  pretty_print        pretty_print_inspect           
  pretty_print_cycle  pretty_print_instance_variables
Looksee::ObjectMixin
  edit  ls
Object
  DelegateClass  Digest  P  __binding__  __p  log  pry
Automobile
  make  make=  model  model=  type  type=

Use ‘awesome_print’ to show method inheritance and argument requirements for each method

When you ‘ap’ object.methods, awesome_print prints out a 3 column table that is alphabetically sorted by method name. The first column acts as an index, the second column shows the method names and the argument requirements for each method and the third column indicates which class or module the method came from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'awesome_print'

ap car.methods

### console output follows ###

[
    [ 0]                               !()                 Automobile (BasicObject)
    [ 1]                              !=(arg1)             Automobile (BasicObject)
    [ 2]                              !~(arg1)             Automobile (Kernel)
    [ 3]                             <=>(arg1)             Automobile (Kernel)
    [ 4]                              ==(arg1)             Automobile (BasicObject)
    [ 5]                             ===(arg1)             Automobile (Kernel)
    [ 6]                              =~(arg1)             Automobile (Kernel)
    [ 7]                     __binding__()                 Automobile (Object)
    [ 8]                          __id__()                 Automobile (BasicObject)
    [ 9]                        __send__(*arg1)            Automobile (BasicObject)
    [10]                              ai(*options)         Automobile (Kernel)
    [11]                 awesome_inspect(*options)         Automobile (Kernel)
    [12]                   awesome_print(object, *options) Automobile (Kernel)
    [13]                           class()                 Automobile (Kernel)
    [14]                           clone()                 Automobile (Kernel)
    [15]         define_singleton_method(*arg1)            Automobile (Kernel)
    [16]                         display(*arg1)            Automobile (Kernel)
    [17]                             dup()                 Automobile (Kernel)
    [18]                            edit(name)             Automobile (Looksee::ObjectMixin)
    [19]                        enum_for(*arg1)            Automobile (Kernel)
    [20]                            eql?(arg1)             Automobile (Kernel)
    [21]                          equal?(arg1)             Automobile (BasicObject)
    [22]                          extend(*arg1)            Automobile (Kernel)
    [23]                          freeze()                 Automobile (Kernel)
    [24]                         frozen?()                 Automobile (Kernel)
    [25]                            hash()                 Automobile (Kernel)
    [26]                initialize_clone(arg1)             Automobile (Kernel)
    [27]                  initialize_dup(arg1)             Automobile (Kernel)
    [28]                         inspect()                 Automobile (Kernel)
    [29]                   instance_eval(*arg1)            Automobile (BasicObject)
    [30]                   instance_exec(*arg1)            Automobile (BasicObject)
    [31]                    instance_of?(arg1)             Automobile (Kernel)
    [32]      instance_variable_defined?(arg1)             Automobile (Kernel)
    [33]           instance_variable_get(arg1)             Automobile (Kernel)
    [34]           instance_variable_set(arg1, arg2)       Automobile (Kernel)
    [35]              instance_variables()                 Automobile (Kernel)
    [36]                           is_a?(arg1)             Automobile (Kernel)
    [37]                        kind_of?(arg1)             Automobile (Kernel)
    [38]                              ls(*args)            Automobile (Looksee::ObjectMixin)
    [39]                            make()                 Automobile
    [40]                           make=(arg1)             Automobile
    [41]                          method(arg1)             Automobile (Kernel)
    [42]                         methods(*args)            Automobile (Object)
    [43]                           model()                 Automobile
    [44]                          model=(arg1)             Automobile
    [45]                            nil?()                 Automobile (Kernel)
    [46]                       object_id()                 Automobile (Kernel)
    [47]                  pretty_inspect()                 Automobile (Kernel)
    [48]                    pretty_print(q)                Automobile (PP::ObjectMixin)
    [49]              pretty_print_cycle(q)                Automobile (PP::ObjectMixin)
    [50]            pretty_print_inspect()                 Automobile (PP::ObjectMixin)
    [51] pretty_print_instance_variables()                 Automobile (PP::ObjectMixin)
    [52]                 private_methods(*args)            Automobile (Object)
    [53]               protected_methods(*args)            Automobile (Object)
    [54]                             pry(*object, *hash)   Automobile (Object)
    [55]                   public_method(arg1)             Automobile (Kernel)
    [56]                  public_methods(*args)            Automobile (Object)
    [57]                     public_send(*arg1)            Automobile (Kernel)
    [58]                     respond_to?(*arg1)            Automobile (Kernel)
    [59]             respond_to_missing?(arg1, arg2)       Automobile (Kernel)
    [60]                            send(*arg1)            Automobile (Kernel)
    [61]                 singleton_class()                 Automobile (Kernel)
    [62]               singleton_methods(*args)            Automobile (Object)
    [63]                           taint()                 Automobile (Kernel)
    [64]                        tainted?()                 Automobile (Kernel)
    [65]                             tap()                 Automobile (Kernel)
    [66]                         to_enum(*arg1)            Automobile (Kernel)
    [67]                            to_s()                 Automobile (Kernel)
    [68]                           trust()                 Automobile (Kernel)
    [69]                            type()                 Automobile
    [70]                           type=(arg1)             Automobile
    [71]                         untaint()                 Automobile (Kernel)
    [72]                         untrust()                 Automobile (Kernel)
    [73]                      untrusted?()                 Automobile (Kernel)
]

Use ‘pry’ to dig deeper into the method

Use the ‘stat’ function in pry to find further info about a specific method. Also use ‘show-source’ to pull up the source code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
binding.pry

#'stat' and 'show-source' only work in the pry environment

[1] pry(main)> stat car.make
Method Information:
--
Name: make
Alias: None.
Owner: Automobile
Visibility: public
Type: Bound
Arity: 0
Method Signature: make()
Source Location: example.rb:2
[2] pry(main)> show-source car.make

From: example.rb @ line 2:
Owner: Automobile
Visibility: public
Number of lines: 1

attr_accessor :make, :model, :type
[3] pry(main)>