Skip to content

Instantly share code, notes, and snippets.

View pzol's full-sized avatar

Piotr Zolnierek pzol

View GitHub Profile
@pzol
pzol / README.md
Created June 25, 2012 07:45
Monadic vs Imperative code in ruby

Monadic vs Imperative

Here is a comparison of two pieces of code that do basically the same thing.

So what we have here is a chain of functions depending on each other, each consecutive takes the result of the previous one for further processing. If either of them fails, the rest shall not be executed.

  1. validate parameters received from the outside world in a Hash
  2. convert one of the parameters into an object
  3. do a search in the database
@pzol
pzol / README.md
Created June 5, 2012 07:23
RVM + Puma + Ubuntu

Running Puma on Nginx

I use the latest Puma v1.4.0 from rubygems.

Make sure you have nginx installed with these options:

>/opt/nginx/sbin/nginx -V
nginx version: nginx/1.0.15

built by gcc 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

@pzol
pzol / monadic.rb
Created May 30, 2012 17:56
Reliable presenter code with monadic
class MonadicHotelBookingPresenter
def initialize(booking)
@booking = Maybe(booking)
end
def hotel_name
hotel_details['name'].fetch.to_s
end
def hotel_category
@pzol
pzol / maybe.rb
Created March 28, 2012 13:06
Maybe in ruby
def Maybe(obj)
return Maybe.new(Nothing.new) if obj.nil?
return Maybe.new(obj)
end
class Nothing
def method_missing(*args, &block)
self
end
@pzol
pzol / beautiful_acceptance_spec.rb
Created March 25, 2012 16:27
Beautiful Acceptance Tests
feature 'Drinking Beer' do
scenario 'Drink a favorite beer' do
user = TestUser.new.extend(BeerDrinker)
user.pickup_favorite_beer "Belfast"
user.drinks_chosen_beer!
end
scenario "Getting wasted" do
user = TestUser.new.extend(BeerDrinker)
@pzol
pzol / rvm.sh
Created January 28, 2012 10:59
rvm reinstall from system wide to local user
sudo bash -l -c 'rvm implode && rm -rf /etc/rvmrc /usr/local/bin/rvm* /usr/local/rvm'
rm ~/.rvmrc
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
rvm reload
# check if it is loaded correctly, this should return rvm is a function
type rvm | head -n 1
@pzol
pzol / README.md
Created January 13, 2012 18:11
capybara poltergeist test

This was a test with Poltergeist I did the other day

#!ruby
def polter
   noop

end

@pzol
pzol / json.rb
Created August 5, 2011 19:10
ActiveSupport broken on UTF-8 strings
# encoding: UTF-8
# parsing with EOF throws illegal characters!?
json = <<EOF
{
"date_a": "2011-07-22T09:54:44Z",
"city": "wroc\u0142aw", # after this the next date is not parsed anymore!
"date_b": "2011-07-22T09:54:44Z"
}
EOF
@pzol
pzol / install_ruby192_freebsd.sh
Created December 18, 2010 23:24
How to install rvm 1.9.2 under FreeBSD - this is work in progress and not complete. You have to su or sudo -i to run this
# this does not run as a complete script :(
pkg_add -r bash curl sudo
pkg_add -r git vim # can you live without it?
pkg_add -r libxml2 libxslt # for nokogiri later
pw usermod pzol -G wheel
bash # start bash and run the rest inside it
curl -# -L http://bit.ly/rvm-install-system-wide > rvm-install-system-wide
bash -l < rvm-install-system-wide
@pzol
pzol / tinypm.sh
Created December 5, 2010 22:09
Installation script for tinypm on Ubuntu 10.04 and 10.10
#! /bin/sh
TINYPMVERSION=2.2.3
# from /etc/init.d/tomcat6
NAME=tomcat6
# Directory where the Tomcat 6 binary distribution resides
CATALINA_HOME=/usr/share/$NAME
# Directory for per-instance configuration files and webapps
CATALINA_BASE=/var/lib/$NAME