Skip to content

Instantly share code, notes, and snippets.

View DiegoSalazar's full-sized avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / fibber.rb
Created July 2, 2016 01:49
Memoized Fibonacci in Ruby
# memoized fibonacci in Ruby
class Fibber
attr_accessor :hash
def initialize
@hash = {}
end
def memo_fib(n)
for number in 0..n
if number < 2
@hash[number] = number
@DiegoSalazar
DiegoSalazar / pre-commit
Created June 15, 2016 17:18
git pre-commit hook to block changes containing the string debug in a ruby comment
#!/bin/sh
. git-sh-setup # for die
git-diff-index -p -M --cached HEAD -- | grep '^+' | grep -n '# debug' && die 'Blocking commit: debug detected in code'
:
@DiegoSalazar
DiegoSalazar / jquery.rememberSelectedTab.js.coffee
Created May 24, 2016 00:57
Reload the last selected tab based on the window hash
$.fn.rememberSelectedTab = (tabSelector = "aria_controls")->
selectedTab = window.location.hash.replace "#", ""
if selectedTab && selectedTab != ""
$("[#{tabSelector}='#{selectedTab}']").click()
@each ->
$(@).find("ul.nav-tabs li").click ->
name = $(@).find("a").attr tabSelector
window.location.hash = name
@DiegoSalazar
DiegoSalazar / .heroku_helpers
Last active April 27, 2016 21:12
Heroku bash helpers. Save this as a file in your home dir and source it in your .bash_profile
#
# Heroku helpers
# Example Usage: hcommand app-name
#
# open a Heroku rails console for an app
hcon() {
appName=$(ensureHasArg $1)
heroku run rails console -a $appName
}
@DiegoSalazar
DiegoSalazar / hcon.sh
Created February 24, 2016 16:52
Open a heroku rails console for an app
hcon() {
appName=$1
if [ -z "$appName" ]
then
echo "fucking provide a Heroku app name"
exit 1
fi
heroku run rails console -a $appName
@DiegoSalazar
DiegoSalazar / code_question.rb
Last active January 31, 2017 19:19
What is it about Ruby that makes this work?
thing = if something?
"this"
else
"that"
end
puts thing
# => "this" or "that"

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 / 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
ROUTES = {
"GET /" => ["RootController", :show],
"GET /home" => ["HomeController", :index],
"GET /posts/:slug/comments/:id/edit" => ["CommentsController", :edit],
"POST /posts/:slug/comments" => ["CommentsController", :create],
"PUT /posts/:slug" => ["PostsController", :update]
}
router = Router.new ROUTES
window.lastScroll = 0;
$(function() {
var lastSnap, isAnimating,
snaps = $(".snap-to"),
$window = $(window);
var heights = snaps.map(function() {
return $(this).offset().top;
});