简体   繁体   English

如何在ruby中打印Fixnum类的所有方法

[英]How to print all methods of Fixnum class in ruby

I want to print all methods of Fixnum class in ruby. 我想在ruby中打印Fixnum类的所有方法。 When i typed Fixnum.methods it give this type of output that i don't understand. 当我输入Fixnum.methods它给出了我不理解的这种输出。

[:allocate, :superclass, :<=>, :module_exec, :class_exec, :<=, :>=, :==, :===, :include?, :included_modules, :ancestors, :name, :public_instance_methods, :instance_methods, :private_instance_methods, :protected_instance_methods

You probably wanted the instance methods: 您可能想要实例方法:

Fixnum.instance_methods

To filter the ones from Object : 要过滤Object的那些:

Fixnum.instance_methods - Object.instance_methods

Consider installing pry 考虑安装pry

pry is a better version of irb that allows to do browse classes and object "as if they were folders". pry是一个更好的irb版本,允许浏览类和对象“好像它们是文件夹”。 See for example 例如,参见

$ pry
[1] pry(main)> ls Fixnum
Object.methods: yaml_tag
Fixnum#methods: 
  %   +   <    ==   >>   bit_length  fdiv       odd?  to_f   ~
  &   -   <<   ===  []   div         inspect    ord   to_s 
  *   -@  <=   >    ^    divmod      magnitude  size  zero?
  **  /   <=>  >=   abs  even?       modulo     succ  |    
[2] pry(main)>
...

That's a ruby array and method names are expressed in symbols (another ruby stuff). 这是一个ruby数组,方法名称用符号表示(另一个ruby东西)。 You could do 你可以做到

Fixnum.methods.each { |method| puts method }

From ruby 2.4.0 and later 来自ruby 2.4.0及更高版本

Integer.methods.each { |method| puts method }

The output you get is an array of method names as symbols. 您获得的输出是一组方法名称作为符号。 So, if you want to print them do it like this: 所以,如果你想打印它们就像这样:

Fixnum.methods.each { |method| puts method }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM