Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Does Ruby have a 'which' like method

Reply
Thread Tools

Does Ruby have a 'which' like method

 
 
Scott G Smith
Guest
Posts: n/a
 
      10-07-2009
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
 
Reply With Quote
 
 
 
 
Rajinder Yadav
Guest
Posts: n/a
 
      10-07-2009
Scott G Smith wrote:
> 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


you should be able to do the same thing using: 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 Yadav

http://DevMentor.org
Do Good ~ Share Freely

 
Reply With Quote
 
 
 
 
Scott G Smith
Guest
Posts: n/a
 
      10-07-2009
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-07-2009
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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      10-07-2009

> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Does Ruby have anything like isprint? Martin Kahlert Ruby 3 12-02-2004 10:48 AM
Does javascript have a stop() method like Java applets? Tom Javascript 2 07-18-2004 02:21 PM



Advertisments