Skip to content

Instantly share code, notes, and snippets.

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

David Montesdeoca backpackerhh

🏠
Working from home
View GitHub Profile
@varyform
varyform / fib.rb
Created April 17, 2014 21:56
fib with memo
# Fibonacci numbers WITH memoization.
# Initialize the memoization array.
@scratchpad = []
# Calculate the nth Fibonacci number, f(n).
def fibo(n)
if n <= 1
n
else
@main = ->
canvas = document.getElementsByTagName("canvas")[0]
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl")
compiler = new WebGLCompiler(gl, window.shaders)
program = compiler.createProgramWithShaders("main_vertex", "main_fragment")
gl.clearColor(1.0, 1.0, 1.0, 1.0)
gl.clear(gl.COLOR_BUFFER_BIT)
gl.useProgram(program)
require 'redis'
module Features
class << self
extend Forwardable
def_delegators :rollout, :active?, :activate_user, :deactivate_user, :activate, :deactivate
private
def rollout
@rollout ||= begin
@krisleech
krisleech / 01-business_case.rb
Created April 6, 2013 17:06
Decoupled Analytics
class SomeBusinessCase
include Wisper
def execute(attributes)
if true
publish(:some_business_case_successful, details)
else
publish(:some_business_case_failed, details)
end
end
class SomeService
def call
call_service_a
end
private
def service_a
# ... uses status objects
end
@paneq
paneq / bench.rb
Last active December 11, 2016 13:00
cost of using exceptions for control flow compared to one SQL statement (ruby 2.1.4, rails 4.1.7, sqlite) for rails-refactoring.com . Development mode executed in rails console.
require 'benchmark'
ActiveRecord::Base.logger = nil
Benchmark.bmbm do |bench|
bench.report("SQL query") do
1000.times { Whatever.count }
end
bench.report("exception hit") do
@glebm
glebm / cloud_front.rb
Created September 24, 2011 13:28
A module to invalidate cache on Amazon CloudFront
require 'openssl'
require 'net/http'
require 'net/https'
module CloudFront
extend self
def invalidate(path)
date = Time.now.utc
date = date.strftime("%a, %d %b %Y %H:%M:%S %Z")
@agirorn
agirorn / jQueryEvents.rb
Created September 7, 2011 11:28
Trigger change event in Capybara
module JQueryEventsHelpers
def trigger_change(jQuerySelector)
script = "$('#{jQuerySelector}').trigger('change');"
page.execute_script(script);
end
end
World(JQueryEventsHelpers)
@averyvery
averyvery / load-view-specific-css.rb
Last active October 30, 2017 14:27
Load view-specific CSS in Rails
def view_css
path = "views/#{params[:controller]}/#{params[:action]}"
stylesheet_link_tag(path) unless Rails.application.assets.find_asset(path).nil?
end
@vsavkin
vsavkin / exampe.rb
Last active February 1, 2018 10:15
Example of using EDR
#somewhere in a controller
CreateOrder.new(OrderRepository).create current_user, params
# where
class CreateOrder < UseCaseService
def initialize order_repo
@order_repo = order_repo