Skip to content

Instantly share code, notes, and snippets.

View dsggregory's full-sized avatar

Scott Gregory dsggregory

View GitHub Profile
@dsggregory
dsggregory / exec.go
Created March 13, 2022 15:05
Golang example of an exec streaming the output
package myexec
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
@dsggregory
dsggregory / send_tempfile.rb
Created February 16, 2017 13:38
Rails sending a temporary file in the way of send_file()
# Respond from a controller with a Tempfile and have the middleware unlink and close it.
#
# Send a file as ActionController::DataStreaming.send_file() does. This however, handles a Tempfile as input
# and unlinks/closes once the middleware is done with it. You cannot use send_file() to stream a temporary
# file and also control when it is unlinked in your controller because the RACK middleware actually performs
# the sending of the file AFTER the controller has returned. Rack may even pass the filename to the web server
# and have it make the actual delivery (X-Sendfile).
#
# Largely ripped directly from ActionController::DataStreaming.
@dsggregory
dsggregory / atol_test.c
Created January 13, 2015 14:24
Supporting unsigned int string conversions without going negative
/* NOTES:
1. On 64bit without stdlib.h included, atol and strtoul of >MAXINT
returns "18446744071562067968" which is ULONG_MAX-INT_MAX on 64bit
2. On 32bit
a. with or without stdlib, >MAXINT returns MAXINT for atol()
b. W/wo stdlib, strtoul does the correct conversion
SUGGESTIONS:
1. Build for 64bit but make sure stdlib.h is included
2. Example
@dsggregory
dsggregory / ipaddr.rb
Last active April 4, 2020 04:56
Store an IPv4 or IPv6 network number address in a varbinary(16) column using rails ActiveRecord. This is a decent example of using code to perform the v4/v6 conversion instead of the database. VARBINARY(16) allows one to store the numeric value of an IP address so that you can perform range queries and manage a consistent form with respect to al…
require 'ipaddr'
class Ip < ActiveRecord::Base
attr_accessible :ip, :addr, :other_columns
# The 'addr' accessor is what you use to properly set/get the 'ip' column.
# This sets an instance variable to store the IPAddr class representing the
# ip column in either IPv4 or IPv6.
attr_accessor :addr
validates_presence_of :addr, :message => "Invalid or empty IP Address"