2
2011
Find where a method is defined in Ruby
There are times when you might be working on a codebase with other people and spot a method which you do not know about. You may want to locate the source of that method to learn and understand more about its functionality.
So, Here is a quick tip on how to find where a method is defined or located.
I was working on a project where i found this piece of code -
var = Class.method :foo.eql => foo, :bar.eql => bar[:baz]
I did not know about the eql method, so in order to locate its source file I simply added a piece of code -
puts :foo.method(:eql).source_location.to_s
To break that down, method is a method of Object class that returns a Method class’s object. So :foo.method(:eql) returns a Method object and then calling source_location on it gives an array where the first element is usually the source file’s location and the second element is the line number.
To know more, check out these 2 links -
http://www.ruby-doc.org/core/classes/Object.html#M001038
http://www.ruby-doc.org/core/classes/Method.html
:foo.method(:eql).source_location basically returned an array # => ["/usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.0.2/lib/dm-core/core_ext/symbol.rb", 4]
Hope that helps. Cheers!
Popularity: 1% [?]
Related Posts
Leave a comment
Subscribe
Recent Posts
- Login into phpmyadmin without username and password
- 10+ tips to localise your php application
- 40+ Techniques to enhance your php code – Part 3
- 40+ Techniques to enhance your php code – Part 2
- 40+ Techniques to enhance your php code – Part 1
- CSSDeck – Collection of Pure CSS Creations
- Execute shell commands in PHP
- Php get list of locales installed on system
- Sound cracking in Ubuntu 11.10
- PHP script to perform IP whois
An article by f00




