只需传递false
的inherited
论点public_methods
:
"hello".public_methods.include?(:dup) # => true
"hello".public_methods(false).include?(:dup) # => false
不是你的问题的答案,但如果你不知道,irb
自动完成,所以很容易获得公共方法的列表(特别是如果你知道你正在寻找的方法的开头)。只需点击标签;点击两次将列出所有可能性(但包括继承的可能性):
> "nice".d<tab><tab>
"nice".delete "nice".delete! "nice".display "nice".downcase
"nice".downcase! "nice".dump "nice".dup "nice".define_singleton_method
> "nice".<tab><tab>
Display all 162 possibilities? (y or n)
...
Usingpry
可以更容易地查看可用的方法,按继承级别细分:
[1] pry(main)> cd "nice"
[2] pry("nice"):1> ls
Comparable#methods: < <= > >= between?
String#methods: % * + << <=> == === =~ [] []= ascii_only? bytes bytesize byteslice capitalize capitalize! casecmp center chars chomp chomp! chop chop! chr clear codepoints concat count crypt delete delete! downcase downcase! dump each_byte each_char each_codepoint each_line empty? encode encode! encoding end_with? eql? force_encoding getbyte gsub gsub! hash hex include? index insert inspect intern length lines ljust lstrip lstrip! match next next! oct ord partition prepend replace reverse reverse! rindex rjust rpartition rstrip rstrip! scan setbyte shellescape shellsplit size slice slice! split squeeze squeeze! start_with? strip strip! sub sub! succ succ! sum swapcase swapcase! to_c to_f to_i to_r to_s to_str to_sym tr tr! tr_s tr_s! unpack upcase upcase! upto valid_encoding?
locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_