Skip to content

Instantly share code, notes, and snippets.

View szajbus's full-sized avatar
👋

Michał Szajbe szajbus

👋
View GitHub Profile
#!/usr/bin/ruby
require 'benchmark'
include Benchmark
pi = 3.14
n = 1000000
Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR) do |x|
to_i = x.report("to_i:") { n.times { pi != pi.to_i } }
gsub = x.report("gsub:") { n.times { pi.to_s.gsub(/^.*\.(.*)$/, '\1').to_i > 0 } }
# Finds equilibrium index of a sequence.
# Returns equilibrium index or -1 if no equilibrium exists.
def equi(arr)
left = sums(arr)
right = sums(arr.reverse)
for i in 1..(arr.length) do
return (i-1) if left[i-1] == right[-i]
end
-1
@szajbus
szajbus / log_filter.sh
Created June 1, 2012 11:10
Filter requests to Rails (2.3.x) application by IP address
#!/bin/bash
# Filter requests to Rails application by IP address
#
# Usage:
# tail -f log/production.log | log_filter.sh [IP]
#
# By default tries to obtain current IP address from $SSH_CLIENT or by querying http://ifconfig.me
if [[ $# -eq 0 ]]; then
if [[ -z $SSH_CLIENT ]]; then
ip=$(curl -s http://ifconfig.me)
#!/usr/bin/env bash
if ! which openssl > /dev/null; then
echo Install openssl
exit 1
fi
if ! which curl > /dev/null; then
echo Install curl
exit 1
@szajbus
szajbus / gist:5733058
Last active December 18, 2015 05:29
Strange behavior of == in Ruby 1.8.7

This gist shows buggy behavior of == in Ruby 1.8.7. This affects case equality (===) too because by default === calls == internally unless redefined in specific class (like Range or Regexp).

class A
  def ==(other)
    puts "In A: calling == against #{other}"
    super
  end
end
require 'hashie_walker' # https://github.com/monterail/hashie_walker
require 'active_support/inflector'
hash = {
"CamelCased" => {
"AnotherOne" => {
"Foo" => "Bar",
"FooFooFoo" => "Dupa"
}
}
@szajbus
szajbus / buffer.rb
Created November 10, 2015 14:50
Commitable buffer
class Buffer
def initialize(size = 1_000, &blk)
@size = size
@callback = blk
@queue = []
@lock = Mutex.new
end
def <<(object)
@lock.synchronize do
# Usage:
#
# redis = Redis.new(url: ENV["REDIS_URL"])
# keys = redis.scan_each(match: 'foo*', count: 1_000)
# keys.extend(EachWithProgress)
#
# keys.each_with_progress do |key|
# redis.del(key)
# end
#!/usr/bin/env ruby
require 'optparse'
require 'json'
require 'net/http'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: es_bench.rb [options]"
opts.on("-c COUNT", "--count=COUNT", "Number of requests") do |count|

CLI tricks

Git

Po pierwsze must-have alias w systemie (~/.bashrc lub ~/.zshrc)

alias g="git"