🖥

Ruby | How to show only the newly added method excluding the inheritan

に公開

Conclusion

methods false to the arguments of methods / instance_methods .

 Example.methods(false) 

 Example.instance_methods(false) 

problem

For example, if you have the following class:

 class Example < BasicObject def self.some_method end end 

When I try to list the class method.

 Example.methods 

Result is.

 [:some_method, :allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, :include, :prepend, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, : __send__ , : __id__] 

Many!

Solution

When executed with an argument of false.

 Example.methods(false) 

Result is.

 [:some_method] 

Super nice!

Supplement

The same is true for the display of instance methods.

 Example.instance_methods Example.instance_methods(false) 

Aside

I learned how to use this in "Meta-programming Ruby (second edition)". I forgot how to write specifically.

I was looking for another method in the way of methods_without_ancestors (example). Gugu didn't find it well. The result was an argument difference of the same method.

environment

  • Ruby 2.2.4

Original by

Ruby | 継承元を除外して、新しく追加したメソッドだけを表示する方法

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion