4

Rails2Elixir - Object.methods or Getting a Module to List Its Methods

 3 years ago
source link: https://fuzzyblog.io/blog/rails2elixir/2016/03/14/elixir-equivalent-of-object-methods-or-getting-a-module-to-list-its-methods.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails2Elixir - Object.methods or Getting a Module to List Its Methods

Mar 14, 2016

One of my single favorite Ruby features is: 

Object.methods

or my personal favorite version of this:

(obj.methods - Object.methods).sort

So if you have a string in Ruby you can say:

(“foo”.methods - Object.methods).sort

Here’s the equivalent of this in elixir:

Module.__info__(:functions)

So if you wanted to find out what Map can do you’d use:

Map.__info__(:functions)

or Enum:

Enum.__info__(:functions)

And you’ll get back a long list of functions. Here’s the result for Enum:

[all?: 1, all?: 2, any?: 1, any?: 2, at: 2, at: 3, chunk: 2, chunk: 3, chunk: 4,
chunk_by: 2, concat: 1, concat: 2, count: 1, count: 2, dedup: 1, dedup_by: 2,
drop: 2, drop_while: 2, each: 2, empty?: 1, fetch: 2, fetch!: 2, filter: 2,
filter_map: 3, find: 2, find: 3, find_index: 2, find_value: 2, find_value: 3,
flat_map: 2, flat_map_reduce: 3, group_by: 2, group_by: 3, intersperse: 2,
into: 2, into: 3, join: 1, join: 2, map: 2, map_join: 2, map_join: 3,
map_reduce: 3, max: 1, max_by: 2, member?: 2, min: 1, min_by: 2, min_max: 1,
min_max_by: 2, partition: 2, ...]

You can also use the .module_info construct to get more information: 

Enum.module_info
[module: Enum,
exports: [__info__: 1, join: 2, flat_map_reduce: 3, reduce_while: 3,
sort_by: 3, sort_by: 2, filter: 2, uniq_by: 2, sort: 1, fetch!: 2, uniq: 2,
join: 1, reverse_slice: 3, to_list: 1, group_by: 3, scan: 2, find: 3,
concat: 2, sum: 1, at: 2, all?: 1, zip: 2, with_index: 2, split_while: 2,
count: 1, sort: 2, empty?: 1, random: 1, dedup: 1, map_join: 2, find: 2,
find_index: 2, reduce: 2, map_join: 3, count: 2, find_value: 2, min: 1,
concat: 1, flat_map: 2, shuffle: 1, at: 3, uniq: 1, unzip: 1, min_by: 2,
take_random: 2, take: 2, reverse: 2, find_value: 3, ...],
attributes: [vsn: [332892874748555839594542519652485069262]],
compile: [options: [:debug_info], version: '6.0.2',
time: {2016, 2, 21, 21, 48, 31},
source: '/private/tmp/elixir20160221-76790-1cjto8i/elixir-1.2.3/lib/elixir/lib/enum.ex'],
native: false,
md5: <<250, 112, 213, 232, 64, 152, 188, 238, 148, 116, 149, 143, 81, 114, 121, 206>>]

 Here’s a helpful Stack Overflow link on the subject.


Posted In: #rails2elixir #elixir


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK