02 Jun 2014, 17:51
Generic-user-small

Alex Kaplan (1 post)

Book version: B13.3.13.3—May 27, 2014

While using Elixir 0.13.3, the code snippets below are the only way that I could figure out how to achieve the record manipulation examples from the book. Possibly due to records needing to be declared inside of modules and general updates to Record API:

iex(17)> require Record
iex(18)> defmodule User do
...(18)> Record.defrecord :phone, country: "1", area: "555", local: nil
...(18)> end
iex(19)> require User

To create a new record with default values

iex> number1 = User.phone()

To create a new record with prepopulated values

iex> number2 = User.phone(local: "1212", country: "44")

To get a specific field in a record

iex> c = User.phone(number2, :country)

To update a value in a record

iex> User.phone(number2, country: "+" <> User.phone(number2, :country))

Cheers,

Alex

  You must be logged in to comment