Skip to content

Instantly share code, notes, and snippets.

Avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / .heroku_helpers
Last active Apr 27, 2016
Heroku bash helpers. Save this as a file in your home dir and source it in your .bash_profile
View .heroku_helpers
#
# 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 Feb 24, 2016
Open a heroku rails console for an app
View hcon.sh
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 Jan 31, 2017
What is it about Ruby that makes this work?
View code_question.rb
thing = if something?
"this"
else
"that"
end
puts thing
# => "this" or "that"
View gist:fb0aa1163a6a2fe4a6d1

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 Dec 29, 2015
detects and counts loop size in a singly linked list
View loop_size.rb
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
View named_segments_router_tests.rb
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
View snap.js
window.lastScroll = 0;
$(function() {
var lastSnap, isAnimating,
snaps = $(".snap-to"),
$window = $(window);
var heights = snaps.map(function() {
return $(this).offset().top;
});
@DiegoSalazar
DiegoSalazar / convert_params_to_form_data.rb
Created Apr 8, 2015
Converts a nested hash into a flat hash with bracketed keys as needed by http multipart form data
View convert_params_to_form_data.rb
# Flatten a hash's keys down and format them like http form data
# e.g. { user: { id: 1, posts: [{ name: 'test' }] }, debug: true }
# becomes
# { 'user[id]' => 1, 'user[posts][0][name]' => 'test', 'debug' => true }
def convert_params_to_form_data(params, form_data = {}, prefix = '')
bracketeer = ->(p, k) { "#{p}#{p.empty? ? '' : '['}#{k}#{p.empty? ? '' : ']'}" }
params.each_with_object form_data do |(key, value), form_data|
case value when Hash
convert_params_to_form_data value, form_data, bracketeer.call(prefix, key)
View multipart-form-data.txt
User-Agent: curl/7.21.2 (x86_64-apple-darwin)
Host: localhost:8080
Accept: */*
Content-Length: 1143
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------83ff53821b7c
------------------------------83ff53821b7c
Content-Disposition: form-data; name="img"; filename="a.png"
Content-Type: application/octet-stream
@DiegoSalazar
DiegoSalazar / temp_converter.rb
Created Mar 13, 2015
Temperature conversion, naive solution
View temp_converter.rb
def convertTemp(temp, fs, ts)
@conversions ||= {
'C' => {
'C' => ->(t) { t },
'F' => ->(t) { t * 9 / 5 + 32 },
'K' => ->(t) { t + 273.15 },
'R' => ->(t) { (t + 273.15) * 9 / 5 },
'De' => ->(t) { (100 - t) * 3 / 2 },
'N' => ->(t) { t * 33 / 100 },
'Re' => ->(t) { t * 4 / 5},
You can’t perform that action at this time.