Getting a List of Methods Available in Classes in Ruby
Here’s an example of how to get a list of methods available in a specific Ruby class.
require_relative '../lib/require'
# require 'csv'
# require 'yaml'
def original_methods(target_class="CSV")
eval %(@target_class_methods=#{target_class}.methods)
@target_class_ancestors=['Module','Class','Object','Kernel','BasicObject']
@target_class_ancestors.each {|ancestor|
eval %(@target_class_methods-=#{ancestor}.methods)
}
return @target_class_methods
end
puts original_methods("YAML")
In this example, we’re getting a list of methods defined in the specified class (in this case, “YAML”).
Notes
The list of methods retrieved includes methods defined in that class and its parent classes, but problems may occur if there are duplicate names. Therefore, please be aware that it may not be completely accurate.
Please understand these limitations when using this method in a hurry.