Skip to content

Instantly share code, notes, and snippets.

@zeisler
zeisler / gist:ed4784f95b8ec4061392
Created March 1, 2015 21:05
How to Delete your hard drive in Ruby
$ sudo irb
irb(main):002:0> require 'fileutils'
=> true
irb(main):003:0> FileUtils.rm_rf("#{nil}/")

#Report Calc API

##API Abstraction

###Http API

There would be thin html presenter layer that would call the api and hand that off to the view. The api would have a JSON presenter layer.

Pros

class Sharer
def initalizer(stuff_name)
@stuff_name = stuff_name
end
def stuff
@stuff_name
end
class MyClass
def initalize(arg1: arg2:, **additional_args)
post_initalize(**additional_args)
end
def post_initalize(*)
end
end
@zeisler
zeisler / thread.rb
Created January 22, 2016 21:06
Pass attributes to child thread so that the current request id can be logged
require "thread"
class Thread
alias_method :_initialize, :initialize
def initialize(*args, &block)
inheritable_attributes = Thread.current.inheritable_attributes
_initialize(*args) do
Thread.current.inheritable_attributes = inheritable_attributes
block.call
@zeisler
zeisler / robbyrussell-with-travis.zsh-theme.bash
Last active January 6, 2017 00:59
Add Travis status to prompt, given travis command exists
local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg[cyan]%}%c%{$reset_color%}$(travis_status)$(git_prompt_info)'
function travis_status_dir {
GIT_ROOT=`git rev-parse --show-toplevel`
echo "${GIT_ROOT}/tmp/.travis"
}
function travis_status_file {
GIT_SHA=`git rev-parse HEAD`
@zeisler
zeisler / stack_overflow_debugger.rb
Created December 7, 2017 00:31 — forked from myronmarston/stack_overflow_debugger.rb
Stack overflow debugger (since Ruby doesn't provide the whole stack in this case)
max_stack_frames = 500
TooManyStackFrames = Class.new(StandardError)
TracePoint.new(:call) do |tp|
if caller.size >= max_stack_frames
raise TooManyStackFrames, "Stack has exceeded #{max_stack_frames} frames"
end
end.enable
@zeisler
zeisler / call_object.rb
Last active February 2, 2018 00:11
Converts Object.new(<args>).call to Object.call(<args>)
require "reverse_parameters"
# Converts Object.new(<args>).call to Object.call(<args>)
# module must be extend after initialize method in order to transfer parameters
module CallObject
def create_callable
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def self.call(#{ReverseParameters.new(instance_method(:initialize)).parameters})
self.new(#{ReverseParameters.new(instance_method(:initialize)).arguments}).call
end
RUBY
//
// AppDelegate.swift
// watchman_temp
//
// Created by Dustin Zeisler on 11/7/18.
// Copyright © 2018 Dustin Zeisler. All rights reserved.
//
import Cocoa
@zeisler
zeisler / migrate.rb
Created March 15, 2019 21:52
CLI for managing Rails Migrations
#!/usr/bin/env ruby
require "./config/environment"
require "active_record/migration"
require "tty-prompt"
migration = TTY::Prompt.new(interrupt: :exit)
migration_version = migration.select("Select Migration", filter: true) do |menu|
ActiveRecord::Base.connection.migration_context.migrations_status.reverse.each do |status, version, name|
menu.choice "#{status.center(8)} #{version.ljust(14)} #{name}", version
end