Skip to content

Instantly share code, notes, and snippets.

View chaadow's full-sized avatar
🎯
Focusing

Chedli Bourguiba chaadow

🎯
Focusing
View GitHub Profile
@igrigorik
igrigorik / webapp.rb
Created November 13, 2010 21:28
Inspired by @JEG2's talk at Rubyconf... Any ruby object, as a webapp! 'Cause we can. :-)
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
@markedmondson
markedmondson / faster_system_tests.rb
Last active August 10, 2023 07:53
Faster system test sign in
module SessionHelper
extend ActiveSupport::Concern
class ::SessionsBypassController < ActionController::Base
def show
session[:user_id] = params[:user_id]
session[:team_id] = params[:team_id]
session[:tenant] = Apartment::Tenant.current
@yeuem1vannam
yeuem1vannam / fork_with_new_connection.rb
Created October 12, 2012 06:58 — forked from danieldbower/fork_with_new_connection.rb
Forking Processes In Ruby On Rails
# Logic for forking connections
# The forked process does not have access to static vars as far as I can discern, so I've done some stuff to check if the op threw an exception.
def fork_with_new_connection
# Store the ActiveRecord connection information
config = ActiveRecord::Base.remove_connection
pid = fork do
# tracking if the op failed for the Process exit
success = true
class CategoriesController < ApplicationController
include Behaveable::ResourceFinder
include Behaveable::RouteExtractor
# Response type.
respond_to :json
# Get categories.
#
# GET (/:categorizable/:categorizable_id)/categories(.:format)
@VeerpalBrar
VeerpalBrar / consistent_hashing.rb
Last active February 3, 2023 09:38
A consistent hashing implementation in ruby
require 'digest'
class ConsistentHashing
def initialize(nodes)
nodes.map { |node| add_node(node) }
end
def find_cache(key)
puts
hash = hash_value(key)
@Burgestrand
Burgestrand / download-progress.rb
Created June 27, 2010 13:55
Ruby HTTP file download with progress measurement
require 'net/http'
require 'uri'
def download(url)
Thread.new do
thread = Thread.current
body = thread[:body] = []
url = URI.parse url
Net::HTTP.new(url.host, url.port).request_get(url.path) do |response|
@padde
padde / gist:2628262
Created May 7, 2012 15:05
Ruby Fixnum each_bit
class Fixnum
def each_bit
8.times do |p|
yield (self & 1 << p) >> p
end
end
end
@brianstorti
brianstorti / priority_queue.rb
Last active November 17, 2022 16:14
Priority queue implementation in Ruby
class PriorityQueue
attr_reader :elements
def initialize
@elements = [nil]
end
def <<(element)
@elements << element
bubble_up(@elements.size - 1)
@reshleman
reshleman / bug.rb
Created March 11, 2021 19:26
ActiveStorage bug w/ multiple `#attach` calls in a transaction
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@Burgestrand
Burgestrand / webapp.rb
Created January 25, 2011 03:06 — forked from igrigorik/webapp.rb
Turn any ruby Object into a web application!
require 'rack'
class Object
def to_webapp
def self.call(env)
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func || :inspect, *attrs)]
end
self
end