by efxhoy · 5y ago · 0 comments
I miss this so much. I'm a python dev now working on a big Rails monolith. Where does all this stuff come from?
Drop a debugger statement in above the code in question, then at the prompt turn the method into a Method object using something like:
bar_method = foo.method(:bar)
and then bar_method.source_location
Once you have a Method object you can pass it around as a stand alone function, rebind it or call too.#source_location only works for methods defined in ruby itself i.e. not C or Java but it's still a handy tool on big ruby projects.
Most of the time I just use ctags to navigate new codebases but we also have things like Solargraph via LSP too.
If you're using Pry as your REPL, you can use Pry's ls and show-source commands to get more of the info you want at once, with less typing. It's basically calling the Ruby introspection methods for you.
ls foo
show-source -d foo.barIt's the closest thing you get in Ruby to a Smalltalk environment.
90% of the time, I'm trying to read code in a very local context and understand it, which involves being able to trace an identifier to its source. I don't want to run it. Maybe I can't run it (it's a rarely executed path, the runtime env is complex, someone emailed me a copy of a source file, etc.).
Language developers, be like Python. Be like Java. Be boring and explicit.
I know literally zero Go; don’t even think I could write a hello world from memory. But I can and have submitted non-trivial patches to Go codebases to fix bugs, add features, and even a few race conditions.
[EDIT] Which sucks, because I really like Ruby.
So if you want to always be aware where things come from choose Sinatra, Padrino or Hanami.