Skip to content

Instantly share code, notes, and snippets.

Greg Navis gregnavis

Block or report user

Report or block gregnavis

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
gregnavis / routes.rb
Created Jan 15, 2019
An example how to protect user-uploaded data with a password in a Rails app. This example uses a custom password page.
View routes.rb
Rails.application.routes.draw do
resources :uploads, only: :show do
member do
post :download
gregnavis / routes.rb
Last active Jan 15, 2019
An example how to protect user-uploaded data with a password in a Rails app. This example uses HTTP Basic authentication.
View routes.rb
Rails.application.routes.draw do
resources :uploads, only: :show
gregnavis / fork.rb
Created Oct 29, 2018
This is a code snippet I used to delegate execution of test cases to forked processes.
View fork.rb
class ActiveSupport::TestCase
def run
read_io, write_io = IO.pipe
if fork
# Parent: load the result sent from the child
View extended_formatter.rb
class Logging::ExtendedFormatter
def initialize(class_name, formatter)
@class_name = class_name
@formatter = formatter
def call(severity, datetime, progname, msg), datetime, progname(progname), msg(msg))
gregnavis /
Created May 30, 2015
A simple Markdown-like parser
>>> parse_markdown('This is a paragraph.')
Markdown([Paragraph([Text('This is a paragraph.')])])
>>> parse_markdown('The first paragraph.\\n\\nThe second paragraph.')
Markdown([Paragraph([Text('The first paragraph.')]), Paragraph([Text('The second paragraph.')])])
>>> parse_markdown('Emphasis *is* supported.')
Markdown([Paragraph([Text('Emphasis '), Emphasis('is'), Text(' supported.')])])
gregnavis / super_method_proxy.rb
Created May 29, 2015
SuperMethodProxy calls the method of the parent class and allows skipping a method prepended by a module.
View super_method_proxy.rb
module Authentication
def login
puts 'Authentication#login'
class User
prepend Authentication
gregnavis / 20150521093555_create_one_time_switches.rb
Created May 21, 2015
Ensuring that something happens at most once
View 20150521093555_create_one_time_switches.rb
class CreateOneTimeSwitches < ActiveRecord::Migration
def change
create_table :one_time_switches do |t|
t.string :name, null: false
t.timestamp :created_at, null: false
t.index :name, unique: true
gregnavis / insert_query.rb
Created Apr 15, 2015
An InsertQuery object that can be used to insert models in bulk.
View insert_query.rb
class InsertQuery
def initialize(model_class, column_names, returning = nil)
@model_class = model_class
@column_names = column_names
@returning = returning
@rows = []
@executed = false
gregnavis / polynomial.rb
Last active May 13, 2019
Polynomial Manipulation in Ruby
View polynomial.rb
class Polynomial
# maps e.g. {x: 1, y: 2} => 3 which corresponds to term 3 * x ** 1 * y ** 2
attr_reader :coefficients
def initialize(coefficients = {})
puts @coefficients.inspect
@coefficients = coefficients.reject do |_index, coefficient|
coefficient == 0
gregnavis /
Created Feb 26, 2014
This is an example of an automatic call tracer that can report the whole call tree. I use it instead of adding lots of logging statements.
import bdb
# This is a custom debugger that calls the specified handler on each function
# call.
class _CallTracerBdb(bdb.Bdb):
def __init__(self, call_handler, return_handler):
self._call_handler = call_handler
self._return_handler = return_handler
You can’t perform that action at this time.