Class names are CamelCase
.
Methods and variables are snake_case
.
Methods with a ?
suffix will return a boolean.
# A program that takes any single number (max) and returns all the numbers below (max) that is an exact square | |
def exact_squares(max) | |
s = [] | |
while max > 0 | |
s << max if is_exact_square? max | |
max -= 1 | |
end |
# A program that takes any single number (max) and returns all the numbers below (max) that are divisible by five or two but not by both five and two. | |
def mini_fizzbuzz(max) | |
s = [] | |
max.downto 0 do |i| | |
s << i if (i % 2 == 0 || i % 5 == 0) && !(i % 2 == 0 && i % 5 == 0) | |
end | |
s | |
end |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
GREEN="\[\033[0;32m\]" | |
BLUE="\[\033[0;34m\]" | |
LIGHT_RED="\[\033[1;31m\]" | |
LIGHT_GREEN="\[\033[1;32m\]" | |
WHITE="\[\033[1;37m\]" | |
LIGHT_GRAY="\[\033[0;37m\]" | |
COLOR_NONE="\[\e[0m\]" |
# models | |
class Genotype < ActiveRecord::Base | |
has_many :genotypes_colors | |
has_many :colors, through: :genotypes_colors | |
# to post nested attributes in a form using fields_for | |
accepts_nested_attributes_for :genotypes_colors | |
# if you're using Rails 3 attr_accessible | |
attr_accessible :genotypes_colors_attributes, ...rest of accessible attribs |
trials = 10 | |
seconds = 1 | |
threads = [] | |
start_time = Time.now.to_f | |
trials.times do |i| | |
threads << Thread.new { sleep seconds } | |
end | |
threads.each &:join |
#!/bin/bash | |
# quickly add a host to the hosts file | |
hostname="$1" | |
ipaddress="$2" | |
hostsfile="$3" | |
if [ -z "$ipaddress" ]; then | |
ipaddress="127.0.0.1" | |
fi |
def loop_size(node) | |
0.tap do |idx| | |
begin | |
if node.instance_variable_get :@idx | |
return idx - node.instance_variable_get(:@idx) | |
else | |
node.instance_variable_set :@idx, idx | |
idx += 1 | |
end | |
end while node = node.next |
# install the levenshtein gem first with: | |
# sudo gem install levenshtein-ffi | |
require 'levenshtein' | |
def names_are_close?(s1, s2, tolerance = 1) | |
Levenshtein.distance(s1, s2) == tolerance | |
end | |
name_pairs = [ |
Carshow App Tasks