crystal-community / icr

Interactive console for Crystal programming language
MIT License
505 stars 40 forks source link

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

Open vifreefly opened 5 years ago

vifreefly commented 5 years ago

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.

greyblake commented 5 years ago

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.

bew commented 5 years ago

@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 commented 5 years ago

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:

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)