Here I’ve put together a bunch of my Today I Learned posts from way back when. I started writing these when I worked for Hashrocket.
For those not familiar with the TIL format, it’s a short blog post wherein you describe something you learned recently. It doesn’t have to be anything terribly impressive. It could be some obscure Ruby method you discovered or a neat thing you can do with Git. It’s a fun way to catalogue the small facts you learn everyday.
Today I Learned: Squeaky Clean Anaconda Environments
originally published 7/24/2017 on the Hashrocket TIL blog
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
Today I Learned: Keep Your Brews Bubbly
originally published 8/25/2017 on the Hashrocket TIL blog
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.
Today I Learned: Viewing the Git Leaderboard
Originally published 8/31/2017 on the Hashrocket TIL blog
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 Space X or something.
Today I Learned: Ruby Binding Class
originally published 10/16/2017 on the Hashrocket TIL blog
Ruby’s Binding class allows you to access classes, variables, and methods outside of your 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
Today I Learned: Hi, Sierra
originally published 11/25/2017 on the Hashrocket TIL blog
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
Today I Learned: Aliasing an Elixir Module Within Itself
originally published 1/26/2018 on the Hashrocket TIL blog
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
Today I Learned: Prying Open Your Phoenix App
originally published 1/29/2018 on the Hashrocket TIL blog
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
Today I Learned: Phoenix Select Form Helper
originally published 1/30/2018 on the Hashrocket TIL blog
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
Today I Learned: Little Bits
originally published 4/25/2018 on the Hashrocket TIL blog
4 bits (or half a byte) is referred to as a “nibble”.
Today I Learned: Truncating Floats in Elixir
originally published 4/26/2018 on the Hashrocket TIL blog
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
Today I Learned: Elixir String Manipulation
originally published 4/26/2018 on the Hashrocket TIL blog
To get the list of strings 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
Today I Learned: Capturing Frames from Video Files
originally published 8/6/2018
First head over here to download FFMPEG. If you happen to be a Mac user, you can use Homebrew:
brew install ffmpeg
I would check out the Homebrew page for all of the options available (of which, there are quite a few).
To extract frames from a video file, you use the following command:
ffmpeg -i my_vid.webm -r 10 -frames:v 30 -f image2 foo-%03d.jpeg
The above command extracts 10 frames per second and saves them as jpeg files with names ‘foo-001.jpeg’, ‘foo-002.jpeg’, and so on. The maximum number of images that will be created is 30. `image2` refers to the demuxer used.
You can read more about this in the documentation.