English 中文(简体)
是否有办法在鲁比拉印刷反对的公开方法
原标题:Is there a way in Ruby to print out the public methods of an Object

......不包含通用目标的所有公开方法? 我指的是,除进行阵列的减员外。 我只是想迅速审查某个物体向我提供的东西,而有时却不去查阅。

最佳回答

methods ,instance_methods ,public_methods, private_methods and protected_methods 所有人都接受一个血清参数,以确定贵国的物体父母的方法是否包含在内。

例如:

ruby-1.9.2-p0 > class MyClass < Object; def my_method; return true; end; end;
ruby-1.9.2-p0 > MyClass.new.public_methods
 => [:my_method, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] 
ruby-1.9.2-p0 > MyClass.new.public_methods(false)
 => [:my_method]

如@Marnen所指出的,这些方法在动态上作了界定(例如>method_missing)。 你对这些图书馆的唯一旁观是,希望你重新使用的图书馆有充分记录。

问题回答

你们是否正在寻找这一结果?

class Foo
  def bar
    p "bar"
  end
end

p Foo.public_instance_methods(false) # => [:bar]


ps I expect this was not the result you were after:

p Foo.public_methods(false)          # => [:allocate, :new, :superclass]

如果有的话,那将非常有用:由于Ruby能够用动态的文法伪造方法,公共方法往往不是你唯一的选择。 因此,你确实可以依靠<代码>instance_methods来告诉你,这非常有益。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签