Elixir String Manipulation
To get the list of string that compose a longer string, use String.codepoints/1
To get the list of codepoints that represent each letter in the string, use String.to_charlist/1
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"
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
To remove everything after the decimal point in a floating-point number, use Kernel.trunc/1.
iex> Kernel.trunc(3.141)
3
iex> Kernel.trunc(-3.141)
-3
Alternatively, you can simply type trunc
iex> trunc(3.141)
3
4 bits (or half a byte) is referred to as a "nibble".
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"
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
I wanted to do some debugging in a Phoenix app, so I threw require IEx
and IEx.pry()
into my code, only to receive the following error:
Cannot pry
...
Is an IEx shell running?
In order to pry into your code, you'll have to start the server from within the Elixir shell.
iex -S mix phx.server
I was attempting to compile a Phoenix application and I got this error:
Post.__struct__/0 is undefined, cannot expand struct Post
The issue was in a function I defined in the module.
def changeset(%Post{}= post, attrs \\ %{}) do
...
end
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
Alternatively, I can alias the module within itself.
alias Forum.Post
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'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
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
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.
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
Upgrades Homebrew to the latest version:
brew update
Gets a list of what packages are outdated:
brew outdated
Looks through your installed packages and deletes any old versions that may still be hanging around:
brew cleanup
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
Updates packages to the latest version:
brew upgrade
You can add the --cleanup
flag to delete older versions of the packages you are updating.
To delete packages, caches, and other files that aren't being used in any of your environments run:
conda clean -a -y
The -a flag is to delete all unused files and the -y option runs the clean
command without asking for confirmation.
If you want to know what files would be deleted before actually deleting them, run:
conda clean -a --dry-run