Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / weighted_random_sampling.md
Last active April 24, 2025 04:53
[TIx 8] Weighted Random Sampling in Ruby

One of the many reasons I love working with Ruby is it has a rich vocabulary that allows you to accomplish your goals with a minimal amount of code. If there isn't a method that does exactly what you want, it's usually possible to build an elegant solution yourself.

Let's take the example of simulating the rolling of a die.

We can represent a die as an array of its faces.

die = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@mhluska
mhluska / Gemfile
Last active August 29, 2015 14:27
middleman-livereload issue gemfile
source 'https://rubygems.org'
gem 'middleman'
gem 'middleman-blog'
gem 'middleman-dotenv'
gem 'middleman-deploy'
gem 'middleman-livereload'
gem 'octokit'
gem 'rake'
gem 'byebug'
@mhluska
mhluska / Gemfile.lock
Created August 13, 2015 17:30
middleman-livereload gemfile.lock
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.3)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.3.8)
@mhluska
mhluska / config.rb
Last active August 29, 2015 14:27
middleman-livereload issue config.rb
configure :development do
activate :livereload
activate :dotenv
end
set :css_dir, 'stylesheets'
set :js_dir, 'javascripts'
set :images_dir, 'images'
@janko
janko / 01-safe-download.rb
Last active February 18, 2025 08:40
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
# O(n), n = total number of bits
def count_bits1(num):
count = 0
while num > 0:
if num % 2 == 1:
count += 1
num //= 2
return count
@Skorch
Skorch / s3-upload-processor.js
Last active January 18, 2021 09:13
AWS Lambda function which receives an S3 upload event, fetches the custom headers, parses the encoded payload, and handles the API call
var async = require('async');
var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});
var request = require('request');
var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
var sns = new AWS.SNS();
var new_upload_arn = "arn:aws:sns:us-east-1:346805855669:vuedating_new_presenece";
//Lambda entry point
@mhluska
mhluska / bot.rb
Last active September 1, 2016 11:28
Basic Tinder bot with sentiment analysis
require 'dotenv'
require 'tinderbot'
require 'sentimental'
Dotenv.load!
class Bot
MAX_DAYS_SINCE_REPLY = 5
MAX_DAYS_SINCE_MESSAGE = 2
MAX_DISTANCE_MI = 100
#!/bin/sh
# See https://github.com/xiaohan2012/twitter-sent-dnn
if [ "${#}" -eq 0 ]; then
echo "Usage: ${0} message"
exit 1
fi
html=$(curl 'https://twitter-sentiment-cnn.herokuapp.com/' \
@mhluska
mhluska / match_images.py
Created October 9, 2016 04:55
Quick and dirty script to match similar images using image_match
from image_match.goldberg import ImageSignature
import glob
BASE_DIR='/Users/maros.hluska/Dropbox'
BASE_IMAGE='./crop.png'
gis = ImageSignature()
def filenames(extension):
return glob.iglob(BASE_DIR + '/**/*.' + extension, recursive=True)