Skip to content

Instantly share code, notes, and snippets.

Ruby on Rails coding standard

{{toc}}

Code style

  • Use UTF-8. It’s 21 century, 8bit encodings dead now.
  • Use 2 space indent, not tabs
  • Use Unix-style line endings
  • Keep lines not longer than 80 chars
module MyApp
class Application < Rails::Application
require Rails.root + 'lib/custom_public_exceptions'
config.exceptions_app = CustomPublicExceptions.new Rails.public_path
end
end
@harssh
harssh / ransack.rb
Created November 22, 2013 11:34 — forked from ledermann/ransack.rb
# Patch for ransack (https://github.com/ernie/ransack) to use scopes
# Helps migrating from Searchlogic or MetaSearch
# Place this file into config/initializer/ransack.rb of your Rails 3.2 project
#
# Usage:
# class Debt < ActiveRecord::Base
# scope :overdue, lambda { where(["status = 'open' AND due_date < ?", Date.today]) }
# end
#
# Ransack out of the box ignores scopes. Example:
@harssh
harssh / gist:7598651
Created November 22, 2013 11:46 — forked from meinac/gist:7222298
#paste this code in your ransack.rb initializer
#if you want to use scopes on search or sort please use Class.ransack_with_scopes instead of Class.search
Ransack::Adapters::ActiveRecord::Base.class_eval do
def ransack(params = {}, options = {}, scope_used_on_sort = false)
Ransack::Search.new(self, params, options, scope_used_on_sort)
end
def ransack_with_scopes(params = {}, options = {})
scope_used_on_sort = false
ransack_scope = self
ransack_params = {}

Originally published in June 2008

When hiring Ruby on Rails programmers, knowing the right questions to ask during an interview was a real challenge for me at first. In 30 minutes or less, it's difficult to get a solid read on a candidate's skill set without looking at code they've previously written. And in the corporate/enterprise world, I often don't have access to their previous work.

To ensure we hired competent ruby developers at my last job, I created a list of 15 ruby questions -- a ruby measuring stick if you will -- to select the cream of the crop that walked through our doors.

What to expect

Candidates will typically give you a range of responses based on their experience and personality. So it's up to you to decide the correctness of their answer.

require 'action_dispatch/middleware/static'
module Middleware
class FileHandler < ActionDispatch::FileHandler
def initialize(root, assets_path, cache_control)
@assets_path = assets_path.chomp('/') + '/'
super(root, cache_control)
end
def match?(path)
#!/usr/bin/env ruby
require 'gosu' # gem install gosu
$width, $height = 200, 200
$number_of_v_lines,$number_of_h_lines = 10, 10
$chars = ('a'..'z').to_a
class Entity
def initialize(x,y,vel, win)
@pos, @vel = {x:x, y:y}, vel
@harssh
harssh / nginx-config-spdy-example
Created April 21, 2014 06:26 — forked from loopj/nginx-config-spdy-example
Nginx SPDY configuration
server {
listen 443 ssl spdy default_server;
server_name example.com;
ssl_certificate /etc/ssl/certs/my.ssl.crt;
ssl_certificate_key /etc/ssl/private/my.ssl.key;
root /mysite/current/public;
passenger_enabled on;
gem 'kaminari' # Pagination
# Because we have some old legacy users in the database, we need to override Devises method for checking if a password is valid.
# We first ask Devise if the password is valid, and if it throws an InvalidHash exception, we know that we're dealing with a
# legacy user, so we check the password against the SHA1 algorithm that was used to hash the password in the old database.
#SOURCES OF SOLUTION:
# http://stackoverflow.com/questions/6113375/converting-existing-password-hash-to-devise
# https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/crypto_providers/sha512.rb
# https://github.com/plataformatec/devise/blob/master/lib/devise/encryptors/authlogic_sha512.rb
alias :devise_valid_password? :valid_password?
def valid_password?(password)