What ruby and rails methods are available and where are they defined?

The Array class is in ruby core. It includes the Enumerable module so mixes in the methods defined there. However, there is also a Enumerable module defined in Rails. So, when using arrays you get both for free.

How do you know when something is mixed in? How would I know I should also see what’s defined in the rails Enumerable module? For example, method sum is defined in the rails Enumerable module.

How do you do the following:

I want to be able to type: Array.methods and have it include the mixed in methods and tell me where they are mixed in from.

OR i want to be able to go to script/console and type: Array:: followed by two tabs to have it list out that the ‘find’ and ‘sum’ methods are available and tell me where they are mixed in from.

Is there such a tool?

The above are questions I asked a friend.

Here are some things we figured out:

>> p Array.ancestors
[Array, ActiveSupport::CoreExtensions::Array::RandomAccess, ActiveSupport::CoreExtensions::Array::Grouping, ActiveSupport::CoreExtensions::Array::ExtractOptions, ActiveSupport::CoreExtensions::Array::Conversions, ActiveSupport::CoreExtensions::Array::Access, <strong>Enumerable</strong>, Object, ActiveSupport::Dependencies::Loadable, InstanceExecMethods, Base64::Deprecated, Base64, Kernel]
=&gt; nil
>> Array.instance_methods.grep /sum/
=> ["sum"]
>> Array.instance_methods.grep /sort/
=> ["sort", "sort_by", "sort!"]

To see what’s available in instance_methods but not in methods

(However, Array, before is instantiated, is of type Class not Array. So Array.methods displays a list of methods in Class. So it doesn’t really make sense to do this)

>> Array.instance_methods - Array.methods
=> ["zip", "pop", "find_index", "rassoc", "each_with_object", "enum_slice", "map!", "minmax", "from", "shuffle!", "replace", "values_at", "empty?", "group_by", "shuffle", "in_groups", "shift", "yaml_initialize", "take", "find_all", "index_by", "uniq", "min_by", "each_cons", "entries", "to_default_s", "delete_at", "first", "choice", "unshift", "take_while", "select", "max_by", "enum_cons", "to_formatted_s", "sort", "delete_if", "join", "permutation", "each_index", "uniq!", "drop", "reject", "compact", "minmax_by", "sum", "to_ary", "all?", "sort_by", "combination", "drop_while", "many?", "indexes", "collect", "in_groups_of", "second", "slice", "at", "reject!", "grep", "any?", "transpose", "extract_options!", "&amp;", "product", "rand", "compact!", "cycle", "indices", "map", "member?", "third", "*", "split", "flatten", "insert", "fetch", "each_with_index", "+", "count", "one?", "to_xml", "clear", "-", "each", "inject", "slice!", "fourth", "reverse", "length", "last", "enum_with_index", "none?", "find", "fill", "sort!", "min", "to_set", "index", "flatten!", "size", "reduce", "|", "fifth", "nitems", "reverse_each", "to_sentence", "push", "detect", "assoc", "pack", "collect!", "max", "each_slice", "to", "reverse!", "rindex", "partition", "delete", "[]=", "concat", "forty_two", "&lt;&lt;"]

Array.instance_methods displays methods assuming Array is instantiated.

a = Array.new
a.methods

this returns methods in Array since ‘a’ is an instantiated Array.

So, Array.instance_methods and a.methods are the same thing.

>> (Array.instance_methods - Array.<span>methods</span>).grep /sum/
=> ["sum"]

How to tell where a method came from:

>> Array.new.method(:sum)
=> #Method: Array(Enumerable)#sum>

OR

Was experimenting… You can also do this, as an alternative to the above . use instance_method (without s – singular):

>> Array.instance_method(:sum)
=> #<UnboundMethod: Array(Enumerable)#sum>
VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
What ruby and rails methods are available and where are they defined?, 5.0 out of 5 based on 1 rating
Facebook Twitter Email

One Comment to “What ruby and rails methods are available and where are they defined?”