Ruby 戻り値の型を調べる

クラスを調べる

RubyやRailsでメソッドの戻り値の型を調べるにはObjectクラスのclassメソッドを使います。

Arrayクラスのインスタンスに対して調べてみます。Arrayと返ってきます。

array = Array.new
array.class
=> Array

使えるメソッドを調べる

使えるメソッドを調べるには、Objectクラスのmethodsメソッドを使います。

Arrayクラスのインスタンスに対して調べてみます。いろいろなメソッドが返ってきます。

array = Array.new
array.methods
=> [:to_json, :fill, :assoc, :rassoc, :uniq, :uniq!, :compact, :compact!, :flatten, :to_h, :flatten!, :shuffle!, :shuffle, :include?, :combination, :repeated_permutation, :permutation, :product, :sample, :repeated_combination, :bsearch_index, :bsearch, :select!, :second_to_last, :third, :fourth, :from, :forty_two, :third_to_last, :&, :*, :+, :-, :fifth, :sort, :count, :find_index, :select, :reject, :collect, :map, :shelljoin, :to_sentence, :pack, :as_json, :to, :first, :any?, :without, :in_groups, :in_groups_of, :to_formatted_s, :to_default_s, :to_xml, :inquiry, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :extract_options!, :insert, :|, :index, :rindex, :replace, :clear, :deep_dup, :<=>, :<<, :==, :pretty_print_cycle, :[], :[]=, :append, :split, :pretty_print, :reverse, :empty?, :eql?, :concat, :reverse!, :second, :prepend, :inspect, :delete, :length, :size, :to_param, :each, :to_query, :slice, :slice!, :to_ary, :blank?, :to_a, :to_s, :dig, :hash, :at, :fetch, :last, :push, :pop, :shift, :unshift, :frozen?, :each_index, :join, :rotate, :rotate!, :sort!, :collect!, :map!, :sort_by!, :keep_if, :values_at, :delete_at, :delete_if, :reject!, :transpose, :to_v8, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :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, :slice_after, :slice_when, :chunk_while, :sum, :lazy, :to_set, :index_by, :many?, :exclude?, :pluck, :`, :to_yaml, :to_yaml_properties, :psych_to_yaml, :present?, :presence, :acts_like?, :duplicable?, :in?, :presence_in, :instance_values, :instance_variable_names, :with_options, :html_safe?, :require_dependency, :unloadable, :require_or_load, :load_dependency, :pretty_print_instance_variables, :pretty_print_inspect, :to_ruby, :try, :try!, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :public_method, :singleton_method, :class_eval, :is_a?, :debugger, :extend, :byebug, :suppress_warnings, :define_singleton_method, :method, :pretty_inspect, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :display, :object_id, :send, :gem, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]