Skip to content

Instantly share code, notes, and snippets.

View ibanez270dx's full-sized avatar
:shipit:
shippin' it

Jeff Miller ibanez270dx

:shipit:
shippin' it
View GitHub Profile
@ibanez270dx
ibanez270dx / application_record.rb
Created April 28, 2021 04:22
Debug calls to ActiveRecord::Persistence::ClassMethods.create
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
after_create -> do
$caller_counter ||= 0
$caller_counter += 1
message = "call ##{$caller_counter}:".bold
message += caller.select{ |trace| trace.include?(Rails.root.to_s) }.map.with_index{ |trace, index| "#{index+1}. #{trace}" }.join("\n")
message += """created #{self.class.class_name} with id=`#{id}'\n\n""".send(%w(blue cyan yellow magenta white green red).sample).bold
puts message
@ibanez270dx
ibanez270dx / hash.rb
Last active August 22, 2019 06:32
Hash#digtect extension
class Hash
# Hash#digtect is a convenient way to find deeply nested values in a hash hierarchically.
# I.E., it returns the first present (not nil or empty string) diggable value from a
# specified list of keys. Think of it as a combination of Hash#dig and Hash#detect.
#
# Given the following Hash object:
#
# hash = {
# "deeply": { "nested": { "property": "string" } },
@ibanez270dx
ibanez270dx / confirmation.rb
Last active September 20, 2021 20:36
Capistrano Deploy Tasks
namespace :deploy do
task :confirmation do
color = case fetch(:stage)
when :production then 31
when :staging then 33
else 34
end
puts <<~WARN
@ibanez270dx
ibanez270dx / points.java
Created March 11, 2019 21:57
100 points interview
import java.util.Map;
import java.util.HashMap;
import java.lang.Character;
public class points {
private static Map<Character, Integer> pointsMap = null;
private static void populateMap() {
pointsMap.put('a', 1);
@ibanez270dx
ibanez270dx / action_view.rb
Created October 30, 2018 07:55
Return human age by date of birth
module ActionView::Helpers::DateHelper
def age(date_of_birth, **options)
from_time = normalize_distance_of_time_argument_to_time(date_of_birth)
to_time = normalize_distance_of_time_argument_to_time(options.delete(:to_time) || Time.now)
from_time, to_time = to_time, from_time if from_time > to_time
distance_in_minutes = ((to_time - from_time) / 60.0).round
case distance_in_minutes
@ibanez270dx
ibanez270dx / .pryrc
Created October 27, 2018 20:54
Add pbcopy to pry
# Copy and paste sourced from: https://coderwall.com/p/qp2aha/ruby-pbcopy-and-pbpaste
def pbcopy(input)
str = input.to_s
IO.popen('pbcopy', 'w') { |f| f << str }
puts "copied to clipboard"
true
rescue
puts $!
end
@ibanez270dx
ibanez270dx / benchmark.rb
Last active September 8, 2018 20:54
Return time taken in milliseconds to process a block of code, along with its result.
module Benchmark
# Similar to Benchmark.realtime, but returns the result of the block along with time taken in milliseconds.
# Ex: Benchmark.perform{ sleep(3); "hello" } => [3003.2030000002123, "hello"]
#
# Benchmark.realtime | https://ruby-doc.org/stdlib-2.5.0/libdoc/benchmark/rdoc/Benchmark.html#method-c-realtime
# Process.clock_gettime | http://ruby-doc.org/core-2.5.0/Process.html#method-c-clock_gettime
def self.perform
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@ibanez270dx
ibanez270dx / config.yml
Last active June 17, 2018 21:45
CircleCI 2.0 w/ Ruby + RSpec, MySQL, Redis, ElasticSearch
version: 2
jobs:
build:
parallelism: 2 # however many CPUs you need/pay for
#############################################
# Container Setup
#############################################
docker:
- image: circleci/ruby:2.5.0
@ibanez270dx
ibanez270dx / colors-syntax.less
Last active May 7, 2018 06:08
Afterglow theme overrides
// afterglow-syntax/styles/colors.less
// Add this to colors.scss in afterglow ui and syntax packages
@white : rgb(255, 255, 255);
@ghost : rgb(250, 250, 255);
@snow : rgb(249, 249, 254);
@vapor : rgb(246, 246, 251);
@white-smoke : rgb(245, 245, 250);
@silver : rgb(232, 232, 237);
@smoke : rgb(230, 225, 225);
@ibanez270dx
ibanez270dx / confirmation.rb
Last active March 2, 2018 03:26
Capistrano Deploy Confirmation
# /lib/capistrano/tasks/confirmation.cap
namespace :deploy do
task :confirmation do
color = case fetch(:stage)
when :production then 31
when :staging then 33
else 34
end