在 Ruby 中打印一个类的所有方法名称? [英] Print all method names of a class in Ruby?

查看:71
本文介绍了在 Ruby 中打印一个类的所有方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想列出一个类的所有方法,我可以使用 Class_Name.methods,例如,列出 FixNum 类中的所有方法:

If I want to list all methods for a class I can use Class_Name.methods, so, for example, to list all methods in the FixNum class:

> 5.methods
 => [:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :quo, :remainder, :real?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

这个列表真的很难读.我也试过 5.methods.sort 但这并没有使它更具可读性.

This list is really hard to read. I also tried 5.methods.sort but that does not make it more readable.

我在日常编程中经常使用方法列表.有没有办法漂亮地打印出来,这样它就更容易阅读了?

I frequently use the list of methods during my everyday programming. Is there a way to pretty-print this, so it becomes easier to read?

推荐答案

试试这个单行:

puts 5.methods.sort

这篇关于在 Ruby 中打印一个类的所有方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆