Skip to content

Instantly share code, notes, and snippets.

View chaadow's full-sized avatar
🎯
Focusing

Chedli Bourguiba chaadow

🎯
Focusing
View GitHub Profile
@vovahost
vovahost / main.dart
Created February 27, 2019 14:20
CancelableOperation and CancelableCompleter usage example
import 'package:async/async.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test("CancelableOperation with future", () async {
var cancellableOperation = CancelableOperation.fromFuture(
Future.value('future result'),
onCancel: () => {print('onCancel')},
);
superman 127.0.0.1 Kim whoami
database 127.0.0.1 Kim sleep 1 && echo "Database says Hello!"
localhost 127.0.0.1 Kim date
@Burgestrand
Burgestrand / retry.rb
Created April 3, 2013 10:55
Retry a block a given number of times if necessary, protecting against a whitelist of exceptions
class Fixnum
# Runs a block, catching any exceptions matching the exceptions in the
# input parameters. After +self+ number of tries, the exception is
# re-raised and the attempts will terminate.
#
# @yield +self+
# @param [Exception, …] exs Exceptions that will be caught and recovered from.
# @return Whatever the block returns.
def tries(*exs)
exs = [StandardError] if exs.empty?
@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
@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.
@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)
@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
@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|
@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)
class CategoriesController < ApplicationController
include Behaveable::ResourceFinder
include Behaveable::RouteExtractor
# Response type.
respond_to :json
# Get categories.
#
# GET (/:categorizable/:categorizable_id)/categories(.:format)