Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mctaylorpants's full-sized avatar

Alex Taylor mctaylorpants

View GitHub Profile
@mctaylorpants
mctaylorpants / neovim_eval_ruby.rb
Last active November 18, 2022 00:01
EvalRuby plugin for Neovim
# EvalRuby: Run Ruby without leaving Neovim
#
# Usage:
# - While on a line of Ruby, or in Visual mode
# with multiple lines selected, type :EvalRuby.
#
# Installation:
# 1. Install neovim-ruby: https://github.com/neovim/neovim-ruby
#
# 2. Put this file in your plugins directory
@mctaylorpants
mctaylorpants / theatre.js
Created May 23, 2020 05:43
YouTube Theatre 🎬
# 1. Load up a YouTube video
# 2. Hit "t" to launch "Theatre Mode"
# 3. Use this code to enter REAL theatre mode!
$("#columns").remove();$("#container").remove();$("body > ytd-app").setAttribute("style", "background: black")
def self.halting(callback_sequence, user_callback, halted_lambda, filter)
callback_sequence.before do |env|
target = env.target
value = env.value
halted = env.halted
unless halted
result_lambda = -> { user_callback.call target, value }
env.halted = halted_lambda.call(target, result_lambda)
def display_deprecation_warning_for_false_terminator
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Returning `false` in Active Record and Active Model callbacks will not implicitly halt a callback chain in Rails 5.1.
To explicitly halt the callback chain, please use `throw :abort` instead.
MSG
end
def deprecated_false_terminator # :nodoc:
Proc.new do |target, result_lambda|
terminate = true
catch(:abort) do
result = result_lambda.call if result_lambda.is_a?(Proc)
if Callbacks.halt_and_display_warning_on_return_false && result == false
display_deprecation_warning_for_false_terminator
else
terminate = false
end
@mctaylorpants
mctaylorpants / rails_5_1_callback_halting_monitor.rb
Last active May 2, 2020 14:03
Better callback deprecation warnings for Rails 5.0
# A monkey-patch to make detecting deprecated
# callbacks easier, because a stack trace is
# not the greatest when it comes to callbacks.
#
#
# Original code:
# https://github.com/rails/rails/blob/c4d3e202e10ae627b3b9c34498afb45450652421/activesupport/lib/active_support/callbacks.rb#L766-L788
require "active_support/callbacks"
module ActiveSupport
@mctaylorpants
mctaylorpants / downcase_filenames.rb
Last active February 28, 2020 18:47
Modify case-sensitive filenames on macOS
#!/usr/bin/env ruby
# Converts all characters in a filename to lowercase on macOS, where by default
# the filesystem is case-insensitive.
# Usage:
# Pass a glob pattern to the script:
# ruby downcase_filenames.rb spec/cassettes/**/*.yml
begin
@mctaylorpants
mctaylorpants / exercise.md
Created May 10, 2018 01:22
Haskell from First Principles - Chapter 15 - 'Mem' Exercise

This gist and its comments are a way of working through an exercise problem found in Chapter 15 of the Haskell Book.

Here's the code from the exercise. Our job is to implement the mempty and mappend functions, which are presented here as undefined:

module Chap15Ex where
import Data.Monoid

newtype Mem s a =
 Mem {
# Run the strategies for a given scope
def _run_strategies_for(scope, args) #:nodoc:
self.winning_strategy = @winning_strategies[scope]
return if winning_strategy && winning_strategy.halted?
# ...
(strategies || args).each do |name|
strategy = _fetch_strategy(name, scope)
next unless strategy && !strategy.performed? && strategy.valid?
module Devise
module Strategies
# Default strategy for signing in a user, based on their email and password in the database.
class DatabaseAuthenticatable < Authenticatable
def authenticate!
resource = password.present? && mapping.to.find_for_database_authentication(authentication_hash)
hashed = false
if validate(resource){ hashed = true; resource.valid_password?(password) }
remember_me(resource)