Skip to content

Instantly share code, notes, and snippets.

View mikz's full-sized avatar

Michal Cichra mikz

  • Prague, Czech Republic
View GitHub Profile
@mikz
mikz / unicorn.rb
Created April 2, 2012 13:59
unicorn config
preload_app true
worker_processes 2
listen 3000
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
class Example
attr_reader :value
def initialize(value)
@value = value
end
def truthy?
!!@value
end
@mikz
mikz / gist:2813751
Created May 27, 2012 12:27
Make Rails 2.3.x play nice with Ruby 1.9.x. Forces encoding to UTF8 for every string in parameters.
module UTF8
module Params
private
def normalize_parameters_with_encoding(value)
normalize_parameters_without_encoding(value).tap do |value|
value.force_encoding(Encoding.default_external) if value.respond_to?(:force_encoding)
end
end
end
end
@mikz
mikz / october_daemon.rb
Created June 29, 2012 16:43
October Daemon
module October
class Daemon
attr_reader :child, :root, :env
def initialize(root, env = nil)
@root = root
@env = env
end
def spawn!
@mikz
mikz / script_pry.rb
Created July 4, 2012 09:48
script/pry for Rails 2.3
#!/usr/bin/env ruby
console = File.expand_path(File.dirname(__FILE__), '.') + "/console"
exec console, *ARGV, '--irb=pry'
array = 1_000_000.times.map{ rand(0..3) }
Benchmark.bm do |x|
x.report('check') { array.each_slice(2){|a,b| b == 0 ? 0 : a / b } }
x.report('rescue') { array.each_slice(2){|a,b| a / b rescue 0 } }
end
@mikz
mikz / calendar.rb
Created October 12, 2012 10:09
GeekTool MacRuby calendar (10.8 only)
#!/Users/mikz/.rvm/rubies/macruby-0.12/bin/ruby
# coding: utf-8
def colorize(text, color_code)
"\e[0;#{color_code}m#{text}"
end
def black(text=""); colorize(text, 30); end
def red(text=""); colorize(text, 31); end
def green(text=""); colorize(text, 32); end
@mikz
mikz / alias.sh
Created November 15, 2012 16:21
Tiny script to run ruby files or run all files in directory (usefull for tests)
alias rt="ruby -Itest -I. -e \"alias rt="ruby -Itest -I. -e \"require'pathname';P=Pathname;ARGV.map{|a|p=P.new(a);next unless p.exist?;p.directory?? P.glob(p.join('**/*.rb')):p}.flatten.compact.uniq.each{|p|require p.expand_path}\""\""
# flat api params like: name=John&surnname=Doe
POST '/api/user', name: 'John', surname: 'Doe'
# nested api params like: user[name]=Jack&user[surname]=Xzbit
POST '/api/user', user: { name: 'Jack', surname: 'Xzbit' }
# how to get only params that are related to the model?
# imagine case:
@mikz
mikz / docker-namesgenerator.rb
Created March 17, 2014 19:58
Fetches awesome namesgenerator from docker & runs it in ruby.
require 'open-uri'
class NamesGenerator
def self.load
go = open('https://raw.github.com/dotcloud/docker/master/pkg/namesgenerator/names-generator.go')
left, right = go.read.scan(/\[\.\.\.\]string{(?<strings>.+)}/)
left = left.first.scan(/"(.+?)"/)