View available methods ruby

扶醉桌前 提交于 2020-02-01 01:40:27

问题


how can I view all the available methods on an object in ruby. I'm using the aptana IDE when I type File. no methods are displayed. I'm coming from an eclipse/java background.

Thanks


回答1:


There are several methods:

obj.methods
obj.public_methods
obj.private_methods
obj.protected_methods
obj.singleton_methods

Update

  1. To get the object methods apart from all inherited methods you can do:

    obj.methods(false)

  2. As Tempus mentioned in the comments, the following command is very helpful to get the current object methods apart from the Object(base class) inherited methods:

    obj.methods - Object.methods




回答2:


You can pass true to the methods if you want to ignore the methods defined in superclasses:

obj.methods(true)
obj.public_methods(true)
obj.private_methods(true)
obj.protected_methods(true)
obj.singleton_methods(true)

Or, if you only want to remove the most common methods that are defined in the Object class, you want to append either - Object.methods or - Object.instance_methods, depending on whether obj is a class or an instance of a class.




回答3:


I would also note that obj.methods does not return the method names sorted, so you might want to do obj.methods.sort .




回答4:


If you have a Ruby interpreter running, <object>.methods will show the available methods on the object. In Eclipse when I typed File. nothing happened until I did a Content Assist command (CTRL+space). At that point I did get a list of methods.




回答5:


You can invoke File.methods in the interactive Ruby interpreter to see all the available methods. I'm not sure if there's a way in the IDE you're using, as I haven't used it before.



来源:https://stackoverflow.com/questions/1901863/view-available-methods-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!