简体   繁体   English

打印 ruby​​ 文件中定义的所有方法

[英]Print all methods defined in a ruby file

I'd like to print all free methods (outside of classes or modules).我想打印所有免费方法(在类或模块之外)。

Consider this example.考虑这个例子。

def func_a
end

def func_b
end

puts "All methods: #{the_magic_method.join}"

The output should contain my two methods.输出应包含我的两种方法。


I know, for classes (eg string) I could use String.methods .我知道,对于类(例如字符串),我可以使用String.methods

When free functions are defined in Ruby, they become private methods on the Object class.当在 Ruby 中定义自由函数时,它们成为 Object 类上的私有方法。 there is a default instance of the Object class which is the self context for ruby code outside of a class block, called the main instance. Object 类有一个默认实例,它是类块之外的 ruby​​ 代码的自上下文,称为主实例。

initial_methods = private_methods

def func_a
end

def func_b
end

final_methods = private_methods
new_methods = final_methods - initial_methods


puts "#{new_methods.join}"
User.methods(false)

只返回类中定义的方法。

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

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