Skip to content

Instantly share code, notes, and snippets.

require 'pathname'
directories = Pathname.name('.').children.select(&:directory?)
file_paths = {}
directories.each do |directory|
directory.children.each do |file_path|
basename = file_path.basename
file_paths[basename] ||= []
@ollie
ollie / .zsh_custom
Last active August 29, 2015 14:23
Oh My Zsh Setup
# Set language environment
export LC_CTYPE=cs_CZ.UTF-8
export LC_ALL=cs_CZ.UTF-8
export LANG=cs_CZ.UTF-8
# Setup preferred editor
export EDITOR='vim'
# Some handy aliases
alias l="ls -la"
@ollie
ollie / math_stats.rb
Last active August 29, 2015 14:23
Percentile, mean and deviations.
# A bunch of statistics-related methods.
#
# @example
# p Math::Stats.percentile(40, [15, 20, 35, 40, 50]) == 29.0
# p Math::Stats.percentile(75, [1, 2, 3, 4]) == 3.25
# p Math::Stats.median([15, 20, 35, 40, 50]) == 35.0
# p Math::Stats.median([1, 2, 3, 4]) == 2.5
# p Math::Stats.sum([15, 20, 35, 40, 50]) == 160
# p Math::Stats.mean([15, 20, 35, 40, 50]) == 32.0
# p Math::Stats.sample_variance([15, 20, 35, 40, 50]) == 207.5
@ollie
ollie / telnet.rb
Last active August 29, 2015 14:22
Telnet test
require 'net/telnet'
class Client
# Fetch weather forecast for NYC.
#
# @return [String]
def response
fetch_all_in_one_response
# fetch_multiple_responses
ensure
@ollie
ollie / indexes.rake
Last active August 29, 2015 14:16
Find missing table indexes and foreign keys
namespace :db do
desc 'Find missing table indexes, foreign keys and null false columns'
task indexes: :environment do
require 'rainbow'
require 'tables_health'
connection = ActiveRecord::Base.connection
tables_health = TablesHealth.new(connection)
tables_health.indexes_report
@ollie
ollie / pull.rb
Created October 30, 2014 10:50
Multithreaded Ruby "git pull".
#!/usr/bin/env ruby
require 'pathname'
require 'open3'
# Mass-pull git repositories.
#
# GitPull.mass_pull(Dir.pwd)
class GitPull
# Check for this string in directory names.
@ollie
ollie / class_level_instance_variables.rb
Created June 26, 2014 15:21
Class-level instance variables
class Person
class << self
# Make it available from the outside
attr_reader :traits
# This removes duplication in this class and subclasses
def init_variables
@traits = {}
end
@ollie
ollie / fixnum.rb
Last active August 29, 2015 14:02
# Some time-related helpers.
class Fixnum
# Go back in time from right now.
#
# 5.days.ago => 2014-06-12 13:21:38 +0200
#
# @return [Time]
def ago
before(Time.now)
end
@ollie
ollie / hash.rb
Last active December 11, 2015 05:09
Ruby 1.9 Hash syntaxes
# Ruby 1.8 Hash syntax:
some_hash = { :person => 'John Doe', :age => 30 } # Hash-Rocket-style
# Ruby 1.9 Hash syntaxes:
some_hash = { :person => 'John Doe', :age => 30 } # Hash-Rocket-style syntax still there.
some_hash = { person: 'John Doe', age: 30 } # JSON-style syntax, keys are symbols behind the scenes.
# Watch out!
some_hash = { 'person' => 'John Doe', 'age' => 30 } # This will work.
some_hash = { 'person': 'John Doe', 'age': 30 } # This will not work!
@ollie
ollie / gist:915391
Created April 12, 2011 12:06
Multibyte string operations
'asdščěřžýasd'.mb_chars.upcase
# or
require 'unicode'
Unicode.upcase 'asdščěřžýasd'.upcase