Skip to content

Instantly share code, notes, and snippets.

View matisojka's full-sized avatar

Mateusz Sójka matisojka

View GitHub Profile
@matisojka
matisojka / Gemfile
Last active August 29, 2015 14:04
Proxy for separate backend + frontend apps that work together
source 'https://rubygems.org'
gem 'rack-reverse-proxy', require: 'rack/reverse_proxy'
group :development do
gem 'foreman'
end
@matisojka
matisojka / memory_cache.rb
Last active August 29, 2015 14:11
In-memory cache with key limit
class MemoryCache
DEFAULT_MAX_KEYS = 5000
attr_reader :data, :max_keys
def initialize(config)
@data = {}
@max_keys = config[:max_cache_keys] || DEFAULT_MAX_KEYS
@lock = Mutex.new
@matisojka
matisojka / env.rb
Last active August 29, 2015 14:24
Proposal for better ENV usage in Ruby
# Currently, many times we are using ENV variables in a non-optimal way.
# I'd like to explain some of the common use cases for those variables
# and how we can use them in a way that we get the most out of it.
# A common error is to assume that the variable has to be there, but it isn't
# This happens when we use the ENV hash like this:
ENV['SOME_KEY']
# In Ruby, if the key is missing, it will return `nil`, thus calling a method on it
@matisojka
matisojka / routes.rb
Last active August 29, 2015 14:24
Splitting routes into modules
YourApplication::Application.routes.draw do
root to: 'home#index'
get '/about'
get '/login' => 'application#login'
end
ADDITIONAL_ROUTES = [:messages, :orders, :api, :admin]
ADDITIONAL_ROUTES.each do |route_file|
require "#{Rails.root}/config/routes/#{route_file}"
end
@matisojka
matisojka / gist:ca41d70da79239ffbfe8
Created July 2, 2015 12:46
Github labels / states
Review finished - expecting dev input
Feedback considered - expecting review
Code accepted - Ready for QA
QA pending
QA validated
QA rejected - expecting dev input
Ship it
@matisojka
matisojka / valid_json.rb
Created March 29, 2012 11:36 — forked from carlcrott/gist:2218175
JSON validation method (without monkey patching and with proper error handling)
def valid_json?(value)
JSON.parse value
true
rescue JSON::ParserError, TypeError
false
end
@matisojka
matisojka / gist:2236364
Created March 29, 2012 11:44
Change the character encoding of an existing database
alter database DATABASE_NAME charset=utf8
@matisojka
matisojka / chef_solo_bootstrap.sh
Created May 13, 2012 19:08 — forked from ryanb/chef_solo_bootstrap.sh
Bootstrap Chef Solo
#!/usr/bin/env bash
apt-get -y update
apt-get -y install build-essential zlib1g-dev libssl-dev libreadline5-dev libyaml-dev
cd /tmp
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p125.tar.gz
tar -xvzf ruby-1.9.3-p125.tar.gz
cd ruby-1.9.3-p125/
./configure --prefix=/usr/local
make
make install
@matisojka
matisojka / measure_gem_loading_time.rb
Created July 4, 2012 07:05
Measure Gem loading time in your Rails APP
# Disclaimer: this solution has been taken from the post: http://stackoverflow.com/a/5071198/784270
# navigate to the bundler gem and in lib/bundler/runtime.rb,
# find the line that does Kernel.require and wrap it like this
puts Benchmark.measure("require #{file}") {
Kernel.require file
}.format("%n: %t %r")
# Add
@matisojka
matisojka / comparison.rb
Created November 27, 2013 08:40
First string character comparison benchmark
require 'benchmark'
name = 'my_cool_string'
Benchmark.bmbm(10) do |x|
x.report('name[0] hit') do
1_000_000.times { name[0] == 'm' }
end
x.report('name[0] miss') do
1_000_000.times { name[0] == 'y' }