Skip to content

Instantly share code, notes, and snippets.

View jbranchaud's full-sized avatar

Josh Branchaud jbranchaud

View GitHub Profile
@jbranchaud
jbranchaud / thing.rb
Created February 4, 2021 18:15
Returning out of a Ruby begin block
class Thing
def initialize(value)
@value = value
end
def call(other_value)
@other_value = other_value
puts final_value
end
@jbranchaud
jbranchaud / switch_pg_server_client_versions.bash
Last active July 6, 2022 17:37
Bash function for switching asdf postgres versions and stopping/starting servers
function switch_pg {
local version_to_run=$1
local currently_running_version=$(psql --no-psqlrc -t -c 'show server_version;' postgres | xargs)
# check if you're erroneously switching to the same version
if [ "$version_to_run" = "$currently_running_version" ]; then
echo "Postgres $version_to_run is already running."
return 1
fi

Congratulations! Josh Branchaud 🏆:

Check your terminal for next steps 👀

@jbranchaud
jbranchaud / advent_of_code_2020_day_9_part_one.sql
Last active December 10, 2020 01:25
Advent of Code 2020, Day 9, Part One, SQL
-- create temp table with sample input
insert into xmas_data (value) values (35), (20), (15), (25), (47), (40), (62), (55), (65), (95), (102), (117), (150), (182), (127), (219), (299), (277), (309), (576);
-- starter subquery
select
v1.id v1id,
v2.id v2id,
v1.value,
v2.value,
v1.value + v2.value sum
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var picArray [][]uint8
picArray = make([][]uint8, 0, dy)
for i := 0; i < dy; i++ {
var currX []uint8
currX = make([]uint8, 0, dx)
@jbranchaud
jbranchaud / pipe_inspect.ex
Last active March 11, 2017 21:59
easily inspecting a thing that is being piped through functions in Elixir
defmodule PipeInspect do
#
# Usage:
#
# Instead of temporarily breaking up a series of pipes to inspect the values:
# ```
# partial_result =
# 1..100_000
# |> Enum.map(&(&1 * 3))
#
@jbranchaud
jbranchaud / elixir_string_graphemes.ex
Created September 14, 2016 19:02
Elixir String Graphemes, Codepoints, Bytes
> string1 = "\u0065\u0301"
"é"
> string2 = "é"
"é"
> string1 == string2
false
> String.codepoints(string1)
["e", "́"]
> String.codepoints(string2)
["é"]
# Sorting Rows By Index
Welcome back to pgcasts, my name is Josh Branchaud. In this episode I am going to talk about a handy shortcut we can use when ordering rows in a select statement.
Let's say we have a users table:
```sql
create table users (
id serial primary key,
first varchar not null,
@jbranchaud
jbranchaud / vim-rails-migration-edit.rb
Created April 2, 2016 19:22
Error in timezone offset calculation
require 'date'
require 'minitest/autorun'
class TestMeme < MiniTest::Unit::TestCase
def time_to_timestamp(now:)
hours_in_seconds = now.strftime('%H').to_i * 3600
minutes_in_seconds = now.strftime('%M').to_i * 60
seconds = now.strftime('%S').to_i