rails/ruby 查找类的方法

总所周知,rails作为很强大的web框架,rails的语言ruby里的类会有很多方法,甚至一个类中一些方法可以实现同样的效果,那么这么多方法怎么查到自己想要的方法呢?

2.1.1 :001 > Array.methods
 => [:[], :try_convert, :wrap, :yaml_tag, :const_missing, :allocate, :new, :superclass, :class_attribute, :cattr_reader, :cattr_writer, :cattr_accessor, :superclass_delegating_accessor, :descendants, :subclasses, :duplicable?, :json_creatable?, :mongo_thread_local_accessor, :instance_methods, :private_instance_methods, :protected_instance_methods, :public_instance_methods, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, :include?, :name, :ancestors, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :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, :psych_yaml_as, :yaml_as, :deprecate, :alias_method_chain, :alias_attribute, :remove_possible_method, :redefine_method, :mattr_reader, :mattr_writer, :mattr_accessor, :parent_name, :parent, :parents, :local_constants, :local_constant_names, :delegate, :instance_method_names, :method_names, :attr_internal_reader, :attr_internal_writer, :attr_internal_accessor, :attr_internal, :anonymous?, :reachable?, :synchronize, :synchronize_with_deprecation, :synchronize_without_deprecation, :qualified_const_defined?, :qualified_const_get, :qualified_const_set, :delegate_blank_attributes, :pretty_print_cycle, :pretty_print, :_zk_logger_name, :debug_method, :post_mortem_method, :trace_execution_scoped, :trace_execution_unscoped, :trace_method_execution, :trace_method_execution_with_scope, :trace_method_execution_no_scope, :get_stats_scoped, :get_stats_unscoped, :add_method_tracer, :remove_method_tracer, :check_for_illegal_keys!, :check_for_push_scope_and_metric, :validate_options, :default_metric_name_code, :newrelic_method_exists?, :traced_method_exists?, :assemble_code_header, :method_without_push_scope, :method_with_push_scope, :code_to_eval, :shared_examples, :shared_context, :share_examples_for, :shared_examples_for, :share_as, :shared_example_groups, :describe, :delay, :delay_for, :delay_until, :unloadable, :in?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :blank?, :present?, :presence, :acts_like?, :try, :html_safe?, :`, :to_param, :to_query, :instance_values, :instance_variable_names, :to_json, :with_options, :as_json, :methods, :private_methods, :protected_methods, :public_methods, :singleton_methods, :pretty_print_instance_variables, :pretty_print_inspect, :require_or_load, :require_dependency, :require_association, :load_dependency, :load, :require, :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :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, :gem, :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly, :colored_benchmark, :colored_puts, :debugger, :breakpoint, :pretty_inspect, :binding_n, :ai, :awesome_inspect, :awesome_print, :suppress_warnings, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

看看在rails里,一个Array有那么多的类方法,它的实例方法也很多:

2.1.1 :002 > a = Array.new
 => [] 
2.1.1 :003 > a.methods
 => [:inspect, :to_s, :to_a, :to_h, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :bsearch, :pack, :extract_options!, :blank?, :to_sentence, :to_formatted_s, :to_default_s, :to_xml, :uniq_by, :uniq_by!, :from, :to, :second, :third, :fourth, :fifth, :forty_two, :in_groups_of, :in_groups, :split, :append, :prepend, :to_param, :to_query, :to_json, :as_json, :encode_json, :shelljoin, :pretty_print, :pretty_print_cycle, :abbrev, :original_grep, :grep, :entries, :sort_by, :find, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :any?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :lazy, :to_set, :sum, :index_by, :many?, :exclude?, :in?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :present?, :presence, :acts_like?, :try, :html_safe?, :`, :duplicable?, :instance_values, :instance_variable_names, :with_options, :methods, :private_methods, :protected_methods, :public_methods, :singleton_methods, :pretty_print_instance_variables, :pretty_print_inspect, :require_or_load, :require_dependency, :require_association, :load_dependency, :load, :require, :unloadable, :nil?, :===, :=~, :!~, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :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, :gem, :class_eval, :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly, :colored_benchmark, :colored_puts, :debugger, :breakpoint, :pretty_inspect, :binding_n, :ai, :awesome_inspect, :awesome_print, :suppress_warnings, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
这么多方法,乍一看眼花缭乱,我们开始看看怎么查找一个方法:

2.1.1 :009 > a.methods.grep /^leng/
 => [:length]

如果查找到了就返回一个数组。当然,这个方法的使用,前提是要求英文水平以及代码编写规范,哈哈哈,实际上,英文四级就够。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值