Ruby: List Object Methods
In Ruby, practically everything is a “Object” in the sense of Object Oriented programing (OOP).
Even numbers and strings are objects.
What this effective means is that for any “thing” in Ruby, you can append it with .name
where the name is a method (function).
e.g. 3
is a thing in Ruby.
So, it has many methods, for example: to_i
(convert to integer), to_s
(convert to string), and you can call it like this: 3.to_i
.
To find the methods of a thing, use the method methods
.
Example:
String object methods
# ruby # methods for a string object p "rabbit".methods == [:unicode_normalized?, :encode!, :unicode_normalize, :ascii_only?, :unicode_normalize!, :to_r, :to_c, :encode, :include?, :%, :*, :+, :unpack, :unpack1, :count, :partition, :+@, :-@, :<=>, :<<, :sum, :==, :===, :next, :=~, :[], :[]=, :empty?, :casecmp, :eql?, :insert, :casecmp?, :match?, :bytesize, :match, :next!, :succ!, :index, :upto, :replace, :rindex, :chr, :clear, :byteslice, :getbyte, :setbyte, :freeze, :scrub, :scrub!, :dump, :inspect, :intern, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :undump, :length, :size, :downcase!, :succ, :swapcase!, :hex, :capitalize!, :split, :chars, :oct, :grapheme_clusters, :concat, :codepoints, :lines, :bytes, :to_str, :end_with?, :start_with?, :reverse, :reverse!, :sub, :to_s, :to_i, :to_f, :rjust, :center, :prepend, :crypt, :ord, :chomp, :strip, :to_sym, :ljust, :delete_prefix, :delete_suffix, :lstrip, :gsub, :scan, :chomp!, :sub!, :gsub!, :rstrip, :delete_prefix!, :chop, :lstrip!, :rstrip!, :chop!, :delete_suffix!, :strip!, :tr_s, :delete, :squeeze, :tr!, :tr, :delete!, :squeeze!, :each_line, :each_byte, :tr_s!, :each_codepoint, :each_grapheme_cluster, :slice, :slice!, :each_char, :encoding, :force_encoding, :b, :valid_encoding?, :rpartition, :hash, :between?, :clamp, :<=, :>=, :<, :>, :singleton_class, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :display, :class, :frozen?, :tap, :then, :yield_self, :public_send, :extend, :clone, :method, :public_method, :singleton_method, :define_singleton_method, :!~, :nil?, :respond_to?, :object_id, :send, :to_enum, :enum_for, :__send__, :!, :instance_eval, :instance_exec, :!=, :equal?, :__id__]
Integer object methods
# ruby # methods for a integer object p 5.methods == [:anybits?, :nobits?, :downto, :times, :pred, :pow, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :next, :-@, :digits, :[], :odd?, :bit_length, :even?, :upto, :%, :chr, :&, :*, :+, :inspect, :-, :/, :size, :succ, :<, :>, :ord, :to_int, :to_s, :to_i, :to_f, :to_r, :integer?, :numerator, :denominator, :rationalize, :gcd, :div, :divmod, :fdiv, :^, :gcdlcm, :lcm, :coerce, :modulo, :remainder, :abs, :magnitude, :zero?, :floor, :ceil, :round, :truncate, :|, :~, :allbits?, :dup, :+@, :infinite?, :finite?, :eql?, :singleton_method_added, :quo, :real?, :arg, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :angle, :phase, :conj, :to_c, :conjugate, :clone, :i, :nonzero?, :step, :positive?, :negative?, :between?, :clamp, :singleton_class, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :display, :hash, :class, :frozen?, :tap, :then, :yield_self, :public_send, :extend, :method, :public_method, :singleton_method, :define_singleton_method, :=~, :!~, :nil?, :respond_to?, :freeze, :object_id, :send, :to_enum, :enum_for, :__send__, :!, :instance_eval, :instance_exec, :!=, :equal?, :__id__]
Array object methods
# ruby xx = [3,4,5] # methods for a array object p xx.methods == [:to_h, :include?, :&, :pack, :*, :+, :-, :shuffle!, :shuffle, :sample, :at, :fetch, :last, :union, :difference, :intersection, :intersect?, :push, :append, :pop, :shift, :unshift, :each_index, :join, :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, :select!, :filter!, :keep_if, :values_at, :delete_at, :delete_if, :reject!, :transpose, :fill, :sort, :assoc, :rassoc, :compact!, :flatten, :uniq!, :find_index, :count, :select, :filter, :combination, :reject, :collect, :map, :repeated_permutation, :flatten!, :permutation, :bsearch, :bsearch_index, :repeated_combination, :product, :first, :all?, :any?, :one?, :none?, :minmax, :deconstruct, :reverse_each, :zip, :|, :take, :take_while, :drop, :cycle, :drop_while, :<=>, :<<, :uniq, :compact, :==, :sum, :[], :[]=, :empty?, :eql?, :insert, :index, :rindex, :replace, :clear, :max, :min, :inspect, :length, :size, :each, :reverse, :concat, :reverse!, :to_ary, :prepend, :to_a, :to_s, :delete, :slice, :slice!, :dig, :hash, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :find, :chain, :lazy, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :filter_map, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :tally, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :singleton_class, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :display, :class, :frozen?, :tap, :then, :yield_self, :public_send, :extend, :clone, :method, :public_method, :singleton_method, :define_singleton_method, :===, :=~, :!~, :nil?, :respond_to?, :freeze, :object_id, :send, :to_enum, :enum_for, :__send__, :!, :instance_eval, :instance_exec, :!=, :equal?, :__id__]
List Class Methods
xx = Class.new p xx.methods # [:allocate, :superclass, :subclasses, :new, :public_instance_method, # :define_method, :<=>, :<=, :>=, :==, :===, :included_modules, :include?, # :ancestors, :attr, :attr_reader, :attr_writer, :attr_accessor, :instance_methods, # :public_instance_methods, :protected_instance_methods, :private_instance_methods, # :constants, :freeze, :inspect, :const_set, :const_get, :const_source_location, # :const_defined?, :class_variable_set, :class_variables, :remove_class_variable, # :class_variable_get, :const_missing, :class_variable_defined?, :<, # :private_constant, :>, :singleton_class?, :public_constant, :deprecate_constant, # :prepend, :include, :module_exec, :to_s, :module_eval, :class_exec, # :class_eval, :remove_method, :undef_method, :alias_method, :method_defined?, # :public_method_defined?, :private_method_defined?, :name, :protected_method_defined?, # :public_class_method, :private_class_method, :autoload?, :autoload, # :instance_method, :singleton_class, :dup, :itself, :taint, :tainted?, # :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, # :protected_methods, :private_methods, :public_methods, :instance_variables, # :instance_variable_get, :instance_variable_set, :instance_variable_defined?, # :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :display, # :hash, :class, :frozen?, :tap, :then, :yield_self, :public_send, :extend, # :clone, :method, :public_method, :singleton_method, :define_singleton_method, # :=~, :!~, :nil?, :eql?, :respond_to?, :object_id, :send, :to_enum, # :enum_for, :__send__, :!, :instance_eval, :instance_exec, :!=, :equal?, # :__id__]