Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / weighted_random_sampling.md
Last active February 21, 2024 19:02
[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 = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@gaqzi
gaqzi / upload-archive-to-phonegap-build.sh
Created September 4, 2013 17:29
Takes a zip/tar.gz and uploads it to PhoneGap build while showing a progress bar.
#!/bin/bash
TOKEN=phonegap-build-access-token
APP_ID=phonegap-build-app-id
PROGRESS_FILE=/tmp/$TOKEN-progress
echo "" > $PROGRESS_FILE
tail -f $PROGRESS_FILE &
curl -X PUT -F file=@$1 https://build.phonegap.com/api/v1/apps/$APP_ID?auth_token=$TOKEN -o /tmp/$TOKEN-progress --progress-bar
kill $!
@spilth
spilth / ruby-rails-resources.md
Last active December 1, 2016 12:40
A collection of useful reference and learning resources for Ruby and Rails.
@coldnebo
coldnebo / rails_trace.rb
Last active December 1, 2018 08:10
This Rack middleware for Rails3 lets you see a call-trace of the lines of ruby code in your application invoked during a single request. Only code within your app is considered (i.e. in the /app folder). This expands on my previous attempt (https://gist.github.com/3077744). Example of output in comments below.
require 'singleton'
# outputs a colored call-trace graph to the Rails logger of the lines of ruby code
# invoked during a single request.
#
# Example:
#
# 1) Make sure this file is loaded in an initializer
#
# 2) Add the following to your application.rb in Rails3:
@waseem
waseem / deploy.rb
Created December 1, 2012 01:14 — forked from markoa/deploy.rb
Ingredients to monitor Resque with God automatically via Capistrano (on Ubuntu)
namespace :deploy do
desc "Hot-reload God configuration for the Resque worker"
task :reload_god_config do
sudo "god stop resque"
sudo "god load #{File.join(deploy_to, 'current', 'config', 'resque-' + rails_env + '.god')}"
sudo "god start resque"
end
end
# append to the bottom:
@jessedearing
jessedearing / gist:2351836
Created April 10, 2012 14:44 — forked from twoism-dev/gist:1183437
Create self-signed SSL certificate for Nginx
#!/bin/bash
echo "Generating an SSL private key to sign your certificate..."
openssl genrsa -des3 -out myssl.key 1024
echo "Generating a Certificate Signing Request..."
openssl req -new -key myssl.key -out myssl.csr
echo "Removing passphrase from key (for nginx)..."
cp myssl.key myssl.key.org
openssl rsa -in myssl.key.org -out myssl.key
@erichurst
erichurst / database.yml.example mysql2
Created May 9, 2011 02:58
Rails 3 database.yml examples
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
@waseem
waseem / easy_tunnel.rake
Created July 11, 2010 05:12
A Rake interface to creating a tunnel using ssh.
##
# A Rake interface to creating a tunnel using ssh.
# This file originally comes with the facebooker gem for Ruby on Rails: http://github.com/mmangino/facebooker
#
# How to use:
# 1. Simply put it at a proper place. e.g. lib/tasks/ in your Rails application
# 2. Create a config file 'easy_tunnel.yml' like following:
#
# development:
# public_host_username: host_user_name