如何在没有继承方法的情况下获取类的公共方法?

时间:2022-09-11 20:59:40

Given any object I can call #public_methods and see all the methods it will respond to. However, I find it would sometimes be handy to get a quick list of all the public methods that are not inherited, i.e. the stuff that's really part of this class.

给定任何对象我可以调用#public_methods并查看它将响应的所有方法。但是,我发现有时候获得一个没有继承的所有公共方法的快速列表是很方便的,即那些真正属于这个类的东西。

I found in "Easy way to list public methods for a Ruby object" that if I use:

我找到了“简单的方法列出Ruby对象的公共方法”,如果我使用:

(Foo.public_methods - Object.public_methods).sort

I can filter out a lot of basic Ruby stuff. I'd like to be able to filter everything that was inherited all the way up the chain. If I know the parent class I can filter using it, but I'd like to come up with a generic command that could return an array of the uninherited public methods for any object.

我可以过滤掉很多基本的Ruby东西。我希望能够过滤掉链条上一直延续的所有内容。如果我知道父类我可以使用它进行过滤,但我想提出一个通用命令,它可以返回任何对象的未公开公共方法的数组。

2 个解决方案

#1


47  

Just pass false for the inherited argument of public_methods:

只是为public_methods的继承参数传递false:

"hello".public_methods.include?(:dup) # => true
"hello".public_methods(false).include?(:dup) # => false

Not an answer to your question, but in case you didn't know, irb does autocompletion, so it's easy to get the list of public methods (especially if you know the beginning of the method you are looking for). Just hit tab; hitting it twice will list all possibilities (including inherited ones, though):

不是你的问题的答案,但如果你不知道,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)
...

Using pry makes it even easier to see the methods available, broken down by level of inheritance:

使用pry可以更容易地看到可用的方法,按继承级别细分:

[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_

#2


5  

Take a look at Module#instance_methods

看看Module#instance_methods

You can use the following:

您可以使用以下内容:

class A 
  def method_1
     puts "method from A"
  end
end

class B < A
  def method_2
    puts "method from B"
  end
end

B.instance_methods        # => [:method_1, :method_2, ...]
B.instance_methods(false) # => [:method_2]

#1


47  

Just pass false for the inherited argument of public_methods:

只是为public_methods的继承参数传递false:

"hello".public_methods.include?(:dup) # => true
"hello".public_methods(false).include?(:dup) # => false

Not an answer to your question, but in case you didn't know, irb does autocompletion, so it's easy to get the list of public methods (especially if you know the beginning of the method you are looking for). Just hit tab; hitting it twice will list all possibilities (including inherited ones, though):

不是你的问题的答案,但如果你不知道,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)
...

Using pry makes it even easier to see the methods available, broken down by level of inheritance:

使用pry可以更容易地看到可用的方法,按继承级别细分:

[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_

#2


5  

Take a look at Module#instance_methods

看看Module#instance_methods

You can use the following:

您可以使用以下内容:

class A 
  def method_1
     puts "method from A"
  end
end

class B < A
  def method_2
    puts "method from B"
  end
end

B.instance_methods        # => [:method_1, :method_2, ...]
B.instance_methods(false) # => [:method_2]