Skip to content

Instantly share code, notes, and snippets.

@tonymarklove
tonymarklove / application_record.rb
Last active February 16, 2023 19:26
ActiveRecord pluck_to_hash - pluck Active Record attributes into a Ruby Hash
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.pluck_to_hash(*column_names, **aliased_columns)
column_names = self.column_names.map(&:to_sym) if column_names.blank? && aliased_columns.blank?
columns_to_pluck = column_names + aliased_columns.values
keys = column_names + aliased_columns.keys
pluck(*columns_to_pluck).map do |row|
@tonymarklove
tonymarklove / print_controller_tree.rb
Created September 16, 2020 14:13
Print the Rails controller inheritance tree
#!/usr/bin/env ruby
require File.expand_path('../../config/environment', __FILE__)
Rails.application.eager_load!
def build_tree(branch, ancestors)
return if ancestors.empty?
first, *tail = *ancestors
@tonymarklove
tonymarklove / .pryrc
Created June 5, 2020 09:07
Pry - Jump up the stack to next application code, ignoring gems
Pry::Commands.command /^up_app$/, "jump up the stack to next application code" do
start_frame = Byebug.current_context.frame.pos + 1
frame = (start_frame...Byebug.current_context.stack_size).find do |index|
!Byebug::Frame.new(Byebug.current_context, index).file.start_with?(Gem.dir)
end
_pry_.run_command "frame #{frame}"
end
@tonymarklove
tonymarklove / bisect_formatter.rb
Created November 12, 2019 08:26
When bisect isn't working properly get a print out of files in order (--require ./bisect_formatter.rb --format BisectFormatter)
class BisectFormatter < RSpec::Core::Formatters::ProgressFormatter
RSpec::Core::Formatters.register self, :example_started, :example_passed, :example_pending, :example_failed, :start_dump
def initialize(output)
@output = output
@files_processed = {}
end
def example_started(notification)
return if @files_processed.key?(notification.example.file_path)
#!/usr/bin/env ruby
require "io/console"
LINE_START = " * [pruned] origin/"
def error(message, error_code=-1)
STDERR.puts message
exit(error_code)
end
// ==UserScript==
// @name Zoho Estimate Plugin
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add button for converting Zoho estimates to purchase and sales orders.
// @author You
// @include https://books.zoho.eu/app
// @grant GM_log
// ==/UserScript==
@tonymarklove
tonymarklove / gjk.cpp
Last active August 29, 2015 14:14
An implementation of the GJK algorithm for finding intersections between convex shapes. As described by Casey Muntori (http://mollyrocket.com/849) and refined by Phill Djonov (http://vec3.ca/gjk/implementation/)
typedef Vec3 (*GjkSupportFunc)(Vec3 searchDirection, void* shape1, void* shape2);
struct Simplex {
Vec3 b, c, d; // a is always the latest point added, as the others get pushed down the stack.
int pointCount;
Vec3 searchDirection;
};
local_function Vec3 crossSelf(Vec3 a, Vec3 b) {