Skip to content

Instantly share code, notes, and snippets.

View DiegoSalazar's full-sized avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
# 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
@DiegoSalazar
DiegoSalazar / .bash_profile
Created October 9, 2013 19:23
bashprofile prompt with git branch
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\]"
@DiegoSalazar
DiegoSalazar / select_has_many_through.rb
Last active December 28, 2015 10:59
Using a drop down to add a relationship between has_many :through models.
# 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
@DiegoSalazar
DiegoSalazar / sleepy_threads.rb
Created November 19, 2013 15:56
Seeing threads sleep
trials = 10
seconds = 1
threads = []
start_time = Time.now.to_f
trials.times do |i|
threads << Thread.new { sleep seconds }
end
threads.each &:join
@DiegoSalazar
DiegoSalazar / addhost
Last active December 28, 2015 19:19
Quickly add a host to the hosts file.
#!/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
@DiegoSalazar
DiegoSalazar / loop_size.rb
Created December 29, 2015 15:06
detects and counts loop size in a singly linked list
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

Rails naming conventions

General Ruby conventions

Class names are CamelCase.

Methods and variables are snake_case.

Methods with a ? suffix will return a boolean.

@DiegoSalazar
DiegoSalazar / name_likeness.rb
Last active January 2, 2016 03:39
string likeness test using Levenshtein distance
# 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

  • Back button - Should say "Admin" when logged in.
  • Admin > Show Settings > Location Address should allow to enter physical address and location (venue) name.
  • Admin > Show Settings > Date & Time is for duration of actual show (day of show).
  • Admin > Show Settings > Below "Location Address" add section (not just a field, add a heading for it too) to enter show website address.
  • Admin > Show Settings > Judging - when selecting "Closed Judging" it should disable the participant voting options on Participant accounts.
  • Admin > Show Settings > Upload Show Logo - should allow to upload image. Most likely for flyer, not logo.
  • Admin > Car Classes > Add a Subclass - Does not work.
  • Admin > Car Classes > Add a Subclass - Add a description text field for each sub class.