Today I Learned

hashrocket A Hashrocket project

11 posts by ifuaniemeka twitter @ianiemeka

Elixir String Manipulation

To get the list of string that compose a longer string, use String.codepoints/1

iex> cdp =  String.codepoints("abcdefg")
["a", "b", "c", "d", "e", "f", "g"]
iex> Enum.at(cdp, 0)
"a"
Elixir

To get the list of codepoints that represent each letter in the string, use String.to_charlist/1

iex> chr = String.to_charlist("abcdefg")
'abcdefg'
iex> Enum.at(chr, 0)
97
Elixir

Phoenix Select Form Helper

The select form helper allows you to easily add a select input to your forms.

= form_for @changeset, resource_path(@conn, :create), fn f ->
  = select f, :book_id, @books
  = submit "Save Post", class: "btn"
Elixir

Among the types of arguments that the select helper can accept are two-item tuples. The first item in the tuple is used as the label for the option and the second item is used as the value for the option.

Media.list_books returns a list of structs representing all of the books in the database. We'll need to get from a list of structs to a list of tuples.

To do that, we'll pipe the list of Book structs to Enum.map and use an anonymous function to generate two-value tuples from those structs.

def new(conn, _params)
  ...
  books =
    Media.list_books
      |> Enum.map(&{"#{&1.title} by #{&1.author}", &1.id})

  render conn, "new.html", changeset: changeset, books: books
end
Elixir

Aliasing an Elixir Module Within Itself

I was attempting to compile a Phoenix application and I got this error:

Post.__struct__/0 is undefined, cannot expand struct Post
Bash

The issue was in a function I defined in the module.

def changeset(%Post{}= post, attrs \\ %{}) do
    ...
end
Elixir

I assumed that you would get references to a module within said module for free. That's not the case. There are two ways to fix the error. One is to use the full module name in the parameter list.

def changeset(%Forum.Post{}= post, attrs \\ %{}) do
    ...
end
Elixir

Alternatively, I can alias the module within itself.

alias Forum.Post
Elixir

Hi, Sierra

I recently upgraded my Mac to High Sierra and got this gross message when I tried to use Git:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

My machine was expecting Git to be implemented as an Xcode command line tool. To fix the problem, I simply installed Xcode.

xcode-select --install

Ruby Binding Class

Ruby's Binding class allows you to access classes, variables, and methods outside of the current scope.

class Foo
    def bar
        my_var = 20
        binding()
    end
end

Normally, if you made reference to my_var outside of that method, you'd get an error. Binding objects allow you to do this:

my_foo = Foo.new

foo_bar = my_foo.bar
puts foo_bar.eval('my_var')

# 20

Viewing the Git Leaderboard

Everyone knows that writing code is a competition in which she with the most commits wins. So, obviously, you want to periodically check the leaderboard to see who the 10x developer on the team is.

How do you do that?

git shortlog
Bash

This will give you a list of all the contributors to the repository and the number of commits they have made. You can add the -s flag (for 'summary') to just see names and numbers.

Once you have acquired the evidence that you are indeed the mythical 10x developer, demand a raise from your employer and threaten to go work for Google if you don't get it.

If you already work for Google, threaten to, I don't know, get a job at SpaceX or something.

Keep Your Brews Bubbly

Life is too short to have Homebrew problems. Run these commands to keep your brews bubbly.

Checks your system to make sure that future installs go smoothly:

brew doctor
Bash

Upgrades Homebrew to the latest version:

brew update
Bash

Gets a list of what packages are outdated:

brew outdated
Bash

Looks through your installed packages and deletes any old versions that may still be hanging around:

brew cleanup
Bash

You could alternatively add the --dry-run flag to cleanup to see all the outdated packages that would be removed.

Deletes old symlinks:

brew prune
Bash

Updates packages to the latest version:

brew upgrade
Bash

You can add the --cleanup flag to delete older versions of the packages you are updating.