Neural Networks by 3Blue1Brown

3Blue1Brown has amazing videos on Youtube. Three of them are about neural networks:



Deep Learning Introduction

Nowadays I’ve been mostly coding in ruby at work and in my free time at, but I’m learning deep learning by means of Andrew Ng’s Coursera specialization and I love it.

Basically, a neural network (NN) is a box (function) which predicts a result:

For example, X is an image and Yhat is the prediction (cat or no cat).

In this case, X is a vector with all the pixels of the image:

Before being able to use it, we need to train the neural network with a group of images that are previously labeled as cat or not (training data). The training process finds a function that maps (X, Y) and provides the parameters W and b:

Read the rest of this entry »

Wikidata queries

Let’s play with Wikidata (

Return 20 items which are instance of human which have a twitter account:
SELECT ?item ?twitter
?item wdt:P31 wd:Q5 .
?item wdt:P2002 ?twitter .
} LIMIT 20

Screen Shot 2016-06-12 at 11.22.35

Or all Nobel Peace laureates:
Read the rest of this entry »

redirect_back in Rails 5

redirect_back in Rails 5 is handy:

redirect_back(fallback_location: whatever_path)

fallback_location is used when HTTP_REFERER is not present.

In Rails 4 we had to use redirect_to :back and rescue RedirectBackError:

class MyController < ApplicationController
    rescue_from ActionController::RedirectBackError, with: :redirect_to_default

    def action
      redirect_to :back


    def redirect_to_default
      redirect_to root_path


My first post in Medium

I wrote a post in Medium about and why I think it matters. You can also find the code of AgreeList on GitHub.

Rails 5 tutorial: How to create a Chat with Action Cable

David Heinemeier Hansson recorded a screencast building a chat with Action Cable in Rails 5. Here we have a summary:

gem install rails -v 5.0.0.beta1

rails new chat --skip-spring

cd chat

rails g controller rooms show

rails g model message content:text

rails db:migrate

# app/controllers/rooms_controller.rb

class RoomsController < ApplicationController
  def show
    @messages = Message.all

# app/views/rooms/show.html.erb

<h1>Chat room</h1>
<div id="messages">
  <%= render @messages %>
  <label>Say something:</label><br>
  <input type="text" data-behavior="room_speaker">

# app/view/messages/_message.html.erb

mkdir app/views/messages
<div class=“message”>
  <p><%= message.content %></p>

rails g channel room speak

# app/channels/room_channel.rb

class RoomChannel < ApplicationCable::Channel
  def subscribed
    stream_from "room_channel"

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed

  def speak(data)
    # ActionCable.server.broadcast "room_channel", message: data['message']
    Message.create! content: data['message']

In order to turn on action cable on config/routes.rb we add:

mount ActionCable.server => '/cable'

And on app/assets/javascripts/ we add:

@App ||= {}
App.cable = ActionCable.createConsumer()

# app/assets/javascripts/channels/ = App.cable.subscriptions.create "RoomChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    $('#messages').append data['message']
    # Called when there's incoming data on the websocket for this channel

  speak: (message) ->
    @perform 'speak', message: message

$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
  if event.keyCode is 13 # return = send = ""

# app/models/message.rb

class Message < ApplicationRecord
  after_create_commit { MessageBroadcastJob.perform_later self }

And finally we create app/jobs/message_broadcast_job.rb:

rails g job MessageBroadcast
class MessageBroadcastJob < ApplicationJob
  queue_as :default

  def perform(message)
    ActionCable.server.broadcast 'room_channel', message: render_message(message)

    def render_message(message)
      ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })

The code is on GitHub:

And the video of DHH:

Template Method Pattern

The Template Method Pattern consists of a method that is used as a default value. It is then overwritten in classes that inherit or include it:

class Bicycle
  attr_reader :size, :chain, :tire_size

  def initialize(args={})
    @size = args[:size]
    @chain = args[:chain] || default_chain
    @tire_size = args[:tire_size] || default_tire_size

  def default_chain # <- common default

This way classes that inherit from Cycle can overwrite default_chain, removing the need of super calls and achieve less coupling:

class MountainBike < Bicycle
  def default_chain

Seen in POODR.