icr icon indicating copy to clipboard operation
icr copied to clipboard

Feature: implement `ls` keyword to list all object methods (like in Pry)

Open vifreefly opened this issue 5 years ago • 3 comments

Ruby's Pry gem has a nice ls method to list all object methods: https://github.com/pry/pry/wiki/State-navigation#learning-about-your-context-with-the-ls-command .

It will be really helpful to have this feature in Icr.

vifreefly avatar Jan 02 '19 12:01 vifreefly

Hey. Thanks for the suggestion. At the moment it won't be possible to have, because Crystal does not have such reflection mechanisms like Ruby has, e.g. methods, like Object#methods, Object#instance_variables, etc.

I'll close the issue for now. Please reopen it, if you have an idea how this could be achieved.

greyblake avatar Jan 06 '19 16:01 greyblake

@greyblake please re-open, it's possible with macros! Something like ls String could do a pp {{ String.methods.map(&.name.stringify) }} to list all methods name.

It's a very basic example, but you can get other information on each method (it's signature, body, etc..) with some more macro code :)

bew avatar Jan 06 '19 20:01 bew

Doing some ruby now, and actually used ls on an ActiveRecord object, it's pretty cool in pry!!!

About ls for icr, would be nice to have:

  • ls String: to show all methods defined in String (but not in Comparable / Object)
  • ls -a String (or similar, with a crystal syntax): to show all available methods in String, including methods from included modules, base classes, etc.. (Comparable / Object / Reference...)

With full methods signature, and maybe a mention when the method is generated from a macro (not possible yet from macros, but maybe we can find a workaround)

bew avatar Feb 06 '19 14:02 bew