# Does Ruby have a 'which' like method

**URL:** <https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853>\
**Category:** Ruby\
**Created:** [October 7, 2009, 3:50am UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853 "2009-10-07T03:50:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_G\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Scott\_G\_Smith](https://www.ruby-forum.com/u/Scott_G_Smith)\
**Post date:** [October 7, 2009, 3:50am UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853/1 "2009-10-07T03:50:33Z")

</div>

Most UNIX shells have a which command.

It tells me the file which corresponds to a shell command.

Demo:  
which date  
/bin/date

In ruby,  
Suppose I’m dealing with a String.

This String has had some methods mixed-in.

For example,

s = ‘hello’  
p(s.length)

Is there a way I can find the module or string.rb which defined the  
length() method?

I’d like it to work like this:

p(s.which(‘length’))  
/my/rubyfiles/string\_module.rb

Thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![Scott\_G\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Scott\_G\_Smith](https://www.ruby-forum.com/u/Scott_G_Smith)\
**Post date:** [October 7, 2009, 4:44am UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853/2 "2009-10-07T04:44:59Z")

</div>

Scott G Smith wrote:

> I’d like it to work like this:
> 
> p(s.which(‘length’))  
> /my/rubyfiles/string\_module.rb

you should be able to do the same thing using: [http://www.railsapi.com](http://www.railsapi.com)

make sure to select ‘Rails v2.3.4 or Rails v2.3.4 + Ruby v1.9’ before  
clicking  
on the ‘Browse Online’ otherwise you will get Rails APIs by default

> Thanks,  
> Scott

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)  
Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![Scott\_G\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Scott\_G\_Smith](https://www.ruby-forum.com/u/Scott_G_Smith)\
**Post date:** [October 7, 2009, 9:46am UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853/3 "2009-10-07T09:46:28Z")

</div>

Rajinder,  
your pointer to sdoc is a good one.  
Thanks!

It turns out the answer to my question was actually answered by me  
when I was taking out the trash.

The answer is: Just use the debugger.  
When I “step into” a method,  
the debugger gives me the file name of that method.

I demonstrate with a screendump:

# Tue Oct 06 23:50 /software maco$ Tue Oct 06 23:50 /software maco$ rails cars4sale create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create config/locales create db create doc create lib create lib/tasks create log create public/images create public/javascripts create public/stylesheets create script/performance create test/fixtures create test/functional create test/integration create test/performance create test/unit create vendor create vendor/plugins create tmp/sessions create tmp/sockets create tmp/cache create tmp/pids create Rakefile create README create app/controllers/application\_controller.rb create app/helpers/application\_helper.rb create config/database.yml create config/routes.rb create config/locales/en.yml create config/initializers/backtrace\_silencers.rb create config/initializers/inflections.rb create config/initializers/mime\_types.rb create config/initializers/new\_rails\_defaults.rb create config/initializers/session\_store.rb create config/environment.rb create config/boot.rb create config/environments/production.rb create config/environments/development.rb create config/environments/test.rb create script/about create script/console create script/dbconsole create script/destroy create script/generate create script/runner create script/server create script/plugin create script/performance/benchmarker create script/performance/profiler create test/test\_helper.rb create test/performance/browsing\_test.rb create public/404.html create public/422.html create public/500.html create public/index.html create public/favicon.ico create public/robots.txt create public/images/rails.png create public/javascripts/prototype.js create public/javascripts/effects.js create public/javascripts/dragdrop.js create public/javascripts/controls.js create public/javascripts/application.js create doc/README\_FOR\_APP create log/server.log create log/production.log create log/development.log create log/test.log Tue Oct 06 23:50 /software maco$ cd cars4sale Tue Oct 06 23:50 /software/cars4sale maco$ script/generate scaffold Car make:string model:string yr:string exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/cars exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/cars/index.html.erb create app/views/cars/show.html.erb create app/views/cars/new.html.erb create app/views/cars/edit.html.erb create app/views/layouts/cars.html.erb create public/stylesheets/scaffold.css create app/controllers/cars\_controller.rb create test/functional/cars\_controller\_test.rb create app/helpers/cars\_helper.rb create test/unit/helpers/cars\_helper\_test.rb route map.resources :cars dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/car.rb create test/unit/car\_test.rb create test/fixtures/cars.yml create db/migrate create db/migrate/20091007065137\_create\_cars.rb Tue Oct 06 23:51 /software/cars4sale maco$ rake db:migrate (in /software/cars4sale) == CreateCars: migrating

# – create\_table(:cars) -\> 0.0018s == CreateCars: migrated (0.0020s)

Tue Oct 06 23:51 /software/cars4sale maco$  
Tue Oct 06 23:54 /software/cars4sale maco$ cat debug\_this.rb

car1 = Car.new(:make =\> “Chevy”, :model =\> “Camaro”, :yr =\> “1970”)  
car1.save  
debugger  
cars = Car.find :all  
Tue Oct 06 23:54 /software/cars4sale maco$  
Tue Oct 06 23:54 /software/cars4sale maco$  
Tue Oct 06 23:54 /software/cars4sale maco$ rdebug script/runner  
debug\_this.rb  
/software/cars4sale/script/runner:2  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
(rdb:1) c  
debug\_this.rb:5  
cars = Car.find :all  
(rdb:1) s  
/pt/r1/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/  
base.rb:608  
options = args.extract\_options!  
(rdb:1) l  
[603, 612] in /pt/r1/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/  
active\_record/base.rb  
603 # person = Person.find(1, :lock =\> true)  
604 # person.visits += 1  
605 # person.save!  
606 # end  
607 def find(\*args)  
=\> 608 options = args.extract\_options!  
609 validate\_find\_options(options)  
610 set\_readonly\_option!(options)  
611  
612 case args.first  
(rdb:1) quit

Really quit? (y/n) y

Tue Oct 06 23:55 /software/cars4sale maco$  
Tue Oct 06 23:55 /software/cars4sale maco$

So for the Car class, its .find() method is in this file at line 608:

/pt/r1/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/  
base.rb

---

<div class="post-metadata">

**Author:** ![Scott\_G\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Scott\_G\_Smith](https://www.ruby-forum.com/u/Scott_G_Smith)\
**Post date:** [October 7, 2009, 10:10pm UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853/4 "2009-10-07T22:10:37Z")

</div>

On 10/07/2009 09:42 AM, Scott G Smith wrote:

> It turns out the answer to my question was actually answered by me  
> when I was taking out the trash.
> 
> The answer is: Just use the debugger.  
> When I “step into” a method,  
> the debugger gives me the file name of that method.

An alternative is to use set\_trace\_func: IIRC that also provides file  
name and line number so you can identify the source.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Scott\_G\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Scott\_G\_Smith](https://www.ruby-forum.com/u/Scott_G_Smith)\
**Post date:** [October 7, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/does-ruby-have-a-which-like-method/176853/5 "2009-10-07T23:02:50Z")

</div>

> Is there a way I can find the module or string.rb which defined the  
> length() method?

In 1.9 (and REE?) it’s

> > p = ‘’  
> > p.method(:length).source\_location

That won’t help if it’s from a .c file, but will if it’s from a .rb  
file.

-r
