Skip to content

Instantly share code, notes, and snippets.

View braindeaf's full-sized avatar
🏠
Working from home

RobL braindeaf

🏠
Working from home
View GitHub Profile
module Haml::Filters::CodeConcern
extend ActiveSupport::Concern
def render(text)
<<~CODE
<div class="sa-code-snippet-wrapper">
<div class="sa-code-snippet">
<div class="code-title">HAML Snippet</div>
<button class="copy-code-button">Copy</button>
<pre class="chroma"><code>#{Haml::Helpers.preserve text}</code></pre>
</div>
@braindeaf
braindeaf / array.rb
Last active April 14, 2021 08:00
Extending Ruby Array to add consecutive method.
# I want to take this array and return consecutive groups of numbers that are above or equal to 10.
[7.5, 10, 10, 10, 9, 10, 11]
# Enter, slice_when
[7.5, 10, 10, 10, 9, 10, 11].slice_when { |i, j| i >= 10 && j < 10 || j >= 10 && i < 10 }.select { i >= 10 }
# I don't want to repeat myself all the time, so boring
#
days: 182
users: 122
@braindeaf
braindeaf / gist:66b391e0f7a7d896a45925c167168498
Created October 9, 2020 07:08
My First Solver - are you proud of me?
#!/usr/local/bin/python3
from __future__ import print_function
from ortools.linear_solver import pywraplp
t = 'WhatIsX'
s = pywraplp.Solver(t, pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
x = s.NumVar(0, 1000, 'x')
s.Add(x + 2*x + 3*x + 4*x == 10) # it's almost algebraic syntax
@braindeaf
braindeaf / active_record_extensions.rb
Created August 8, 2017 14:29
Active Record LEFT OUTER JOIN with support for Polymorphic associations
# based on the original Gist from mildmojo
# https://gist.github.com/mildmojo/3724189
#
module ActiveRecord
module LeftJoinExtension
extend ActiveSupport::Concern
module ClassMethods
# Simple left join taking advantage of existing Rails & Arel code
def left_joins(*args)
@braindeaf
braindeaf / dave.rb
Created March 28, 2016 11:57
Faraday HTTP Request with different source IP
conn = Faraday.new(url: 'http://www.bunyons.com')
conn.post do |req|
req.url '/dave/onion'
req.headers['Referer'] = 'http://www.something.com'
req.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
req.headers['X-Forwarded-For'] = '86.185.170.5'
req.headers['X-Requested-With'] = 'XMLHttpRequest'
req.body = { plasticpantsid: 2403, arbitudeabuthnot: 'faff' }
end
  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros
// This code is MIT licensed: http://creativecommons.org/licenses/MIT/
// Zenbe Inc (2009).
// Ensure our Zenbe namespaces exist.
window.zen = window.zen || {};
window.zen.util = window.zen.util || {};
/**
* The DropManager class provides a pleasant API for observing HTML5 drag-n-drop
* events, cleaning up the data that they return, and triggering the appropriate
>> t = TMail::Mail.new
=> #<TMail::Mail port=#<TMail::StringPort:id=0x3ff92369be30> bodyport=nil>
>> t.to = "RobL <someaddress@robl.me>"
=> "RobL <someaddress@robl.me>"
>> t.to
=> ["someaddress@robl.me"]
--
>> t.to_addrs
@braindeaf
braindeaf / gist:70842
Created February 26, 2009 13:29
Extracting routes in the same format as they are declared
routes = []
ActionController::Routing::Routes.routes.each do |route|
routes << route.segments.inject('') { |str,s| str << s.to_s }
end
routes