Debugging Methods

Here are few methods that we can use for debugging:

1) Kernal#Caller

 caller.inspect

shows you where you have been.

It will give you “file:line in method”

2) Method

It is a class.

m = "Hello World".method(:upcase)
m.class
=> Method
m.call
=> "HELLO WORLD"

3) source_location

It will give address and line number. It shows you where you are going.

Dog.new.method(:bark).source_location

It will give you address with line number of this above(:bark) method.

4) ancestors

"foo".class.ancestors
=> [String, Comparable, Object, Kernal, BasicObject]

In class method you can simply call it like:

self.class.ancestors

5) Module#method_defined?

"foo".class.method_defined?(:upcase)
=> true
99.class.method_defined?(:upcase)
=> false

6) Method#instance_method

Instead of

User.new.method(:github_url).source_location

you can use:

User.instance_method(:github_url).source_location

.

here we don’t need to create instance.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s