1

In the Erlang shell, erl, I can use rr(Mod) to load the record definitions from the specified module. This allows me to see the field names when looking at a record in the shell.

What's the equivalent to rr(Mod) in the Elixir shell, iex?

For example, I've got an 'RSAPrivateKey' Erlang record, but when shown in iex, all I see is:

{:RSAPrivateKey,
 <<48, 130, 4, 164, 2, 1, 0, 2, 130, 1, 1, 0, 181, 223, 0, 179, 206, 108, 57,
   72, 227, 146, 53, 117, 218, 232, 204, 33, 153, 161, 201, 232, 23, 145, 201,
   134, 105, 53, 164, 223, 95, 111, 64, 29, 254, 114, 146, 33, ...>>,
 :not_encrypted}
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • I'm not too familiar with erlang records, but `Record.extract` and/or `Record.extract_all` perhaps? – zwippie Aug 26 '18 at 12:09

2 Answers2

1

You can get the field names with record_name(a_record):

iex(1)> c "user_record.ex"                  
[User]

iex(2)> import User
User

iex(3)> user1 = user()
{:user, "Meg", "25"}

iex(4)> user(user1)
[name: "Meg", age: "25"]

iex(5)> user2 = user(name: "Roger", age: 50)
{:user, "Roger", 50}

iex(6)> user(user2) 
[name: "Roger", age: 50]

user_record.ex:

defmodule User do
  require Record
  Record.defrecord :user, [name: "Meg", age: "25"]
end
Alexandre Hamez
  • 7,725
  • 2
  • 28
  • 39
7stud
  • 46,922
  • 14
  • 101
  • 127
0

According to Erlang docs:

rr(Module)
Reads record definitions from a module's BEAM file. If there are no record definitions in the BEAM file, the source file is located and read instead. Returns the names of the record definitions read. Module is an atom.

That said, if the code is already compiled to BEAMs, you might use Module.record_name/0 to get an info.

If the code is not yet compiled, you still might extract the record information from erlang header file record.hrl with Record.extract/2.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160