“ Hi! I'm Pierrick, a frontend developer and peanut butter addict. I'm currently working at Zagett, an awesome digital communication agency located in Paris. This is my blog. ”

Rendering the Buddhabrot with Ruby and GD

The Buddhabrot is a very cool (rendering) evolution of the classic Mandelbrot fractal. The one pictured below (source code provided at the end of the post) has been rendered with the GD2 library, a Ruby interface for the well-known GD graphics library.
This image required two hours to be fully rendered with 10 million samples.

It obviously needs lots of optimizations and graphic tweakings but hopefully, this post will remind me to work on it. Anyway, it was pretty funny to do.

Read more »

Permanently redirect a Ruby on Rails app without .htaccess files

Sometimes, especially with Rails apps, you don't have Apache, so you don't have those handy .htaccess files. Here is a quick snippet found somewhere on StackOverflow to ensure that any incoming request falls on your_domain and if not, redirects it permanently (with a 301 HTTP redirect) to your_domain.

I actually used it to redirect blog.hezad.com and www.hezad.com to hezad.com, it worked like a charm.

class ApplicationController < ActionController::Base  
  APP_DOMAIN = 'your_domain'

  # Ensure everything goes to APP_DOMAIN
  before_filter :ensure_domain

  def ensure_domain
    if Rails.env == 'production' and request.host != APP_DOMAIN
      redirect_to request.url.sub(/#{request.host}(\:#{request.port})?/, APP_DOMAIN), :status => 301
    end
  end
end

An important note:


You still have to configure your DNS servers so the old domains and/or subdomains point to your new domain and/or server IP. This snippet won't do it automagically. But it will help different services to be up to date with your content real URI.

Edit

  • Fixed a redirection bug
  • Added the Rails.env == 'production' test in the ensure_domain function.
Read more »

Git quick tips

I tend to forget those commands every single time. This post will help me keeping them in a safe place. Hopefully, it'll be useful to someone else.

Specifying a remote repository with custom SSH port for pushing

git remote add origin ssh://username@hostname:2222/path/to/git_repo
(Replace /path/to/git_repo with your remote repository path)

 

Remove an invalid remote branch

git remote rm origin
(Replace origin with the name of the remote branch you want to remove)

 

Removing, adding, and updating deleted, new and updated files in staged changes

git add -A

 

Listing existing remote branches

git remote -v

 

Additional references

Read more »

Emulating Python's "in" keyword with Javascript

I've always been a huge fan of Python's in keyword for its clarity when used on lists :

# If some value is in some list, then do something
if "some value" in ["another value", "a neat value", "some value"]:
    do_something()

# For each value in some list, do something.
for value in ["one value", "yet another value", "that's a lot of values!"]:
    do_something()

Well, as you know, Javascript also have it's own in keyword, and while it's semantically different from the Python's one, there is a neat shortcut we can write to use it almost the same way.

Read more »