Page 1 of 2

Learn to Program with Elixir

Learn to Program

I’ve been helping people become programmers at The Flatiron School for the last few years. I’ve taught hundreds of people how to program using Ruby. I love Ruby. It’s simple. Low ceremony and most importantly, it gets out of your way and lets you code.

I love teaching with Ruby because it introduces abstract thought without needing to know hieroglyphics. The skills you learn easily (code organization, programmatic thinking, debugging) transfers over to all other programming...

Read more…

Guard this with your life... Or authenticating APIs with Guardian

Authentication in Phoenix

Conceptually, authentication isn’t hard. You collect a username and password, check it against your database and if it matches, WIN!

getinsertpic.com

… then you get into the business of persisting that information across requests, not to mention all of the security concerns of storing a password safely. In this post we’ll be covering how to authenticate users, provide them with a token, and use that token on subsequent requests to identify our user. There are a TON of moving parts...

Read more…

LTrees in Phoenix

Article Tracker

Say you want to build a Phoenix app for tracking Articles. Your one killer feature is that you can store articles in a hierarchy instead of a bunch loose tags like other sites. We’ll be focusing on the model layer here, but we’ll see how we’d construct the query for our tree

You want to be able to click an article with Technology > Futurism > AI > Global Domination, then click on AI in the header, and be taken to a list of articles with Technology > Futurism > AI which might...

Read more…

Processes in Elixir

Processes

The unit of concurrency in Elixir is a process. A process is capable of responding to messages, and maintaining state. The only way to interact with a process is to send it a message.

Steven Nunez (Hostile Developer)

Creating a process

We’ll be climbing the ladder of abstraction but let’s start at the bottom.

getinsertpic.com

We use the spawn function to create a new process. We pass it a function and that code will run in an isolated process.

1
2
3
4
pid = spawn(fn ->
  IO.puts("I run in a...
Read more…

Hey! Watch it!...or how to monitor files in Elixir

File watchers and Elixir

I’m working on a feature that reloads a config file if the file is updated. The easiest way I could figure out was to watch the file, and reload the code. Thinking in JavaScript, I’d expect something like:

1
2
3
4
// fake
Dir.watch('configs/', function(changedFile){
  console.log(`${changedFile} was updated`);
})

But if you’ve been working with Elixir for a bit, you’ll know there’s gonna be a whole bunch of message passing to get this working. In the end, we wind up...

Read more…

Let's build something with Rack

Slow down!

When it’s time to build a web app, we go for the big guns. gem 'rails' in the Gemfile and we’re off to races. What if we didn’t? What if we tried to carefully craft every line in our app? What if we only added Sprockets when we needed it? What if we want granular control of our app’s security? Or what if we just want a better idea of what’s actually happening when a user visits our site?

Rack - The Bow Drill of Web Apps

Let’s build a simple web app using just rack and rack middleware...

Read more…

Let's learn about Rack

Why learn rack?

In Ruby, we’re surrounded by magic. From Sinatra abstracting the request cycle away, to rails making you believe unicorns exist in your terminal, there is no shortage of magical abstraction in Ruby. I’ve found that uncovering the magic, peeling back the layers of metaprogramming and seeing what’s happening is helpful for when your magic turns around and starts attacking you.

We’ll be learning a bit of rack, and then move on to building an app with nothing but the most basic of...

Read more…

Rails Foreign Key Constraints - The Ugly Way

Render unto Caesar…

Let the database do it’s job. For a while, Rails development focused on pushing logic into models that would be better suited being put in as a table constraint. Articles like this one are great at outlining the proper approach. The problem with this is that it’s not built into the Rails tools, leaving you writing ugly code in your migrations. Not very ruby-esque.

Foreign Keys

Databases have a built in way for managing data integrity between 2 tables. The Foreign Key constraint

Read more…

Working with Templates in Ruby ERB

Templates in ERB

This post is a bit of a thought experiment. Looking into this led me down the route of some interesting ruby solutions.

The problem

Ruby ships with the erb library, but it natively doesn’t come with a way to wrap one ERB file in another. This makes composing files difficult. There’s no way to use something like rails’ yield method.

Some Basic ERB

Here’s how we’d render a simple erb template.

1
2
3
require 'erb'
template = "1 and 1 is <%= 1 + 1 %>"
ERB.new(template).result
Read more…

Elixir - Tuples vs Lists

Immutability Primer

Elixir is a Functional programming language with immutable types. You might see code like the following and think that we’re mutating a value.

1
2
3
names = ["Steven", "Kelly", "Michael"]
names = ["Telly"] ++ names
names # => ["Telly", "Steven", "Kelly", "Michael"]

Elixir is giving us a new copy of the array. How it got this might be a bit confusing though. For starters, this is how Elixir stored that original names list.

1
names = ["Steven" | ["Kelly" | ["Michael"]]]
Read more…

Next page