Skip to content

Instantly share code, notes, and snippets.

@rreinhardt9
rreinhardt9 / suppressible.rb
Created July 15, 2022 17:51
Re-creating the "Suppressible" concern that was shown in this screencast from DHH: https://www.youtube.com/watch?v=m1jOWu7woKM
module Suppressible
extend ActiveSupport::Concern
class_methods do
def suppress
self.suppressed = true
yield
ensure
self.suppressed = false
end
@rreinhardt9
rreinhardt9 / bsearch.rb
Created December 27, 2017 16:07
A Binary Search Implementation
class Bsearch
def initialize(dictionary)
@dictionary = Array(dictionary)
end
def search(value)
search_in_chunk(@dictionary, value)
end
@rreinhardt9
rreinhardt9 / submodules.md
Last active September 22, 2019 17:54 — forked from manasthakur/submodules.md
Using git submodules to version-control Vim plugins

Using git-submodules to version-control Vim plugins

If you work across many computers (and even otherwise!), it's a good idea to keep a copy of your setup on the cloud, preferably in a git repository, and clone it on another machine when you need. Thus, you should keep the .vim directory along with your .vimrc version-controlled.

But when you have plugins installed inside .vim/bundle (if you use pathogen), or inside .vim/pack (if you use Vim 8's packages), keeping a copy where you want to be able to update the plugins (individual git repositories), as well as your vim-configuration as a whole, requires you to use git submodules.

Creating the repository

Initialize a git repository inside your .vim directory, add everything (including the vimrc), commit and push to a GitHub/BitBucket/GitLab repository:

cd ~/.vim
@rreinhardt9
rreinhardt9 / tokenizer.rb
Last active April 3, 2017 20:28
Securely hashed request
##
# Forms a tokenized request payload
class Tokenizer
##
# Initialize with a secret and a message to encode
# Tokenizer.new(secret: String, message: Hash)
def initialize(args)
@rreinhardt9
rreinhardt9 / presenter.rb
Created August 22, 2016 02:58
A parent class for presenters to inherit from
# Presenter is a parent class for all presenters to inherit from
#
# A Presenter is a view model and should be used to format and collect data
# that is to be presented in a view.
class Presenter
include Rails.application.routes.url_helpers
# Initialize your presenter with an object and optional view context
# In your controller, you can pass in the `view_context` variable
def initialize(object, view = nil)
@object = object
@rreinhardt9
rreinhardt9 / setup
Created April 14, 2016 16:01 — forked from unixmonkey/setup
#!/usr/bin/env ruby
require 'pathname'
# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file:
@rreinhardt9
rreinhardt9 / gemfile_template.rb
Last active November 10, 2015 19:48
Base Rails Gemfile Template
source "https://rubygems.org"
gem 'rails', '4.2.4'
gem 'bootstrap-sass', '~> 3.3.5'
gem 'coffee-rails', '~> 4.1.0'
gem 'haml-rails'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'pg'
class Capybara::Session
def wait_until(timeout = Capybara.default_wait_time)
Timeout.timeout(timeout) do
sleep(0.1) until value = yield
value
end
end
end
@rreinhardt9
rreinhardt9 / nvd3 Pie example
Created April 9, 2015 17:53
nvd3 Pie with legend on right side for debugging
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="../build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
<script src="../build/nv.d3.js"></script>
<script src="lib/stream_layers.js"></script>
<style>
// To debug a base64 with invalid characters, here called 'buggy'
for (var i = 0; i < buggy.length; i++) {
if (buggy[i].charCodeAt(0) > 255) {
console.warn('found character ' + buggy[i].charCodeAt(0) + ' "' + buggy[i] + '" at position ' + i);
}
}
// From here:
// http://stackoverflow.com/a/25171633