4 ms·
Maybe you already know it, but I find `something.method(:mystery_method).source_location` points me to the right place more often than not. I only mention it be
by grncdr 5y ago
Maybe you already know it, but I find `something.method(:mystery_method).source_location` points me to the right place more often than not. I only mention it because I have noticed a lot of people work productively in Ruby without leveraging it's dynamic/reflective features[0], but little idioms like the above are absolute lifesavers for me.
[0]: A big disadvantage of the above is it's not statically resolvable, so you couldn't really build an IDE go-to-definition with it.
- Lio 5y agoThe two tools I'd recommend for navigating ruby code in addition to #source_location are Solargraph[1] and, my favourite, good 'ol exuberant ctags[2]. Solargraph is an amazing project but ctags is still just so fast and low overhead that it's what I mostly use. ctag support is available for most editors and is built into Vim. You need to first generate a tag file (or have an editor plugin do it for you[3]) and then press Ctrl-] to go to definition. 1. https://solargraph.org/ https://solargraph.org/ 2. http://ctags.sourceforge.net/ http://ctags.sourceforge.net/ 3. https://github.com/tpope/gem-ctags https://github.com/tpope/gem-ctags
- nitrogen 5y agoAnd don't forget the option of just calling into pry at the line in question and typing ls and show-doc. Also ripgrep is incredibly useful for navigating new codebases.
- sanderjd 5y agoYes, grep + debugger / pry were how I solved this when I was still writing lots of Ruby. I find it much more productive to be able to get these answers statically, but I was too strong when I said "impossible"; there's always some way to find the right code, or the VM wouldn't be able to run it :)
- Lio 5y agoYes, you’re quite right! Thank you again, I must remember to use pry more. With ruby 3.1 I think we’re getting a new default debug.rb gem. It will be interesting to see if pry or the beat ideas from pry can be integrated with that.