在此示例中,我正在定义一个方法。
我的问题是:为什么我的method1
没有列在最后一条语句的输出中?
irb(main):001:0> module Foo
irb(main):002:1> def method1
irb(main):003:2> "abc"
irb(main):004:2> end
irb(main):005:1> end
=> :method1
irb(main):007:0> Foo.methods
=> [:<=>, :autoload, :autoload?, :<=, :>=, :==, :===, :included_modules, :include?, :name, :ancestors, :attr, :attr_reader, :attr_writer, :attr_accessor, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :freeze, :inspect, :deprecate_constant, :private_constant, :const_missing, :singleton_class?, :prepend, :class_exec, :module_eval, :class_eval, :include, :<, :>, :remove_method, :undef_method, :alias_method, :protected_method_defined?, :module_exec, :method_defined?, :public_method_defined?, :to_s, :define_method, :public_class_method, :private_method_defined?, :private_class_method, :public_instance_method, :instance_method, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_variables, :method, :public_method, :define_singleton_method, :singleton_method, :public_send, :extend, :pp, :to_enum, :enum_for, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :yield_self, :itself, :tainted?, :taint, :untrust, :untaint, :trust, :untrusted?, :methods, :frozen?, :singleton_methods, :protected_methods, :private_methods, :!, :equal?, :instance_eval, :instance_exec, :!=, :__id__, :__send__]
irb(main):008:0>
答案 0 :(得分:1)
因为它是实例方法,而不是Foo
上的方法。您可以使用.instance_methods
找到它:
irb(main):002:0> Foo.instance_methods
=> [:method1]