In ruby, is there a way to know in the console what a method does?

后端 未结 2 1989
死守一世寂寞
死守一世寂寞 2021-01-27 13:07

In Ruby, if i am looking for the methods of a class. ie:String.methods.sort and i have the following:

[:!, :!=, :!~, :<, :<=, :<=>, :==,         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 13:34

    If you use Pry, it has a handy shortcut, show-source:

    [1] pry(main)> show-source String.display
    
    From: io.c (C Method):
    Owner: Kernel
    Visibility: public
    Number of lines: 15
    
    static VALUE
    rb_obj_display(int argc, VALUE *argv, VALUE self)
    {
        VALUE out;
    
        if (argc == 0) {
            out = rb_stdout;
        }
        else {
            rb_scan_args(argc, argv, "01", &out);
        }
        rb_io_write(out, self);
    
        return Qnil;
    }
    

提交回复
热议问题