简体   繁体   中英

How to print all methods of Fixnum class in ruby

I want to print all methods of Fixnum class in ruby. When i typed Fixnum.methods it give this type of output that i don't understand.

[: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 :

Fixnum.instance_methods - Object.instance_methods

Consider installing pry

pry is a better version of irb that allows to do browse classes and object "as if they were folders". 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). You could do

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

From ruby 2.4.0 and later

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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