shou2017.com
JP / EN

ruby classのメソッドを調べる方法

Mon Oct 22, 2018
Sat Aug 10, 2024

例 Arrayクラス、Fileクラス

irbを起動して

Arrayクラス

irb(main):005:0> [].methods
=> [:to_h, :include?, :at, :fetch, :last, :push, :append, :pop, :shift, :unshift, :each_index, :join, :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, :select!, :keep_if, :values_at, :delete_at, :delete_if, :reject!, :transpose, :+, :assoc, :rassoc, :fill, :-, :compact, :flatten, :flatten!, :shuffle!, :uniq!, :shuffle, :permutation, :combination, :*, :repeated_permutation, :&, :sample, :repeated_combination, :product, :bsearch, :compact!, :bsearch_index, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :any?, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :sum, :uniq, :|, :insert, :pack, :<=>, :<<, :index, :rindex, :replace, :==, :clear, :[], :[]=, :empty?, :eql?, :reverse, :reverse!, :concat, :inspect, :prepend, :max, :min, :length, :size, :each, :to_ary, :delete, :to_a, :to_s, :slice, :slice!, :dig, :hash, :frozen?, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :lazy, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_variables, :method, :public_method, :singleton_method, :define_singleton_method, :public_send, :extend, :to_enum, :enum_for, :pp, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :class, :singleton_class, :clone, :dup, :itself, :yield_self, :taint, :untaint, :tainted?, :untrusted?, :untrust, :trust, :singleton_methods, :methods, :private_methods, :protected_methods, :!, :equal?, :instance_eval, :instance_exec, :!=, :__id__, :__send__]

Fileクラス

irb(main):009:0> File.methods
=> [:empty?, :join, :split, :size, :fnmatch, :fnmatch?, :directory?, :exist?, :exists?, :readable?, :readable_real?, :world_readable?, :writable?, :writable_real?, :world_writable?, :executable?, :executable_real?, :file?, :size?, :owned?, :grpowned?, :pipe?, :symlink?, :socket?, :blockdev?, :chardev?, :setuid?, :setgid?, :sticky?, :zero?, :identical?, :lstat, :ftype, :stat, :atime, :ctime, :birthtime, :mtime, :utime, :chown, :lchmod, :chmod, :lchown, :link, :symlink, :lutime, :readlink, :unlink, :rename, :umask, :mkfifo, :truncate, :delete, :expand_path, :absolute_path, :realpath, :realdirpath, :basename, :dirname, :extname, :path, :read, :sysopen, :for_fd, :popen, :foreach, :binread, :binwrite, :new, :pipe, :write, :select, :copy_stream, :open, :try_convert, :readlines, :allocate, :superclass, :<=>, :<=, :>=, :==, :===, :autoload, :autoload?, :included_modules, :include?, :name, :ancestors, :attr, :attr_reader, :attr_writer, :attr_accessor, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :freeze, :inspect, :deprecate_constant, :private_constant, :const_missing, :singleton_class?, :prepend, :class_exec, :module_eval, :class_eval, :include, :<, :>, :remove_method, :undef_method, :alias_method, :protected_method_defined?, :module_exec, :method_defined?, :public_method_defined?, :to_s, :public_class_method, :define_method, :private_method_defined?, :private_class_method, :instance_method, :public_instance_method, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_variables, :method, :public_method, :singleton_method, :define_singleton_method, :public_send, :extend, :to_enum, :enum_for, :pp, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :yield_self, :taint, :untaint, :tainted?, :untrusted?, :untrust, :frozen?, :trust, :singleton_methods, :methods, :private_methods, :protected_methods, :!, :equal?, :instance_eval, :instance_exec, :!=, :__id__, :__send__]

特定のものだけを調べたい場合は正規表現を使って

<!-- reが含まれるメソッドを調べる -->

irb(main):001:0> [].methods.grep /^re/
=> [:reject!, :repeated_permutation, :repeated_combination, :reject, :reverse_each, :replace, :reverse, :reverse!, :reduce, :remove_instance_variable, :respond_to?]
See Also