Skip to content

Instantly share code, notes, and snippets.

View vmoravec's full-sized avatar

Vladimir Moravec vmoravec

View GitHub Profile
# USAGE: Hash.from_libxml(YOUR_XML_STRING)
require 'xml/libxml'
# adapted from
# http://movesonrails.com/articles/2008/02/25/libxml-for-active-resource-2-0
class Hash
class << self
def from_libxml(xml, strict=true)
begin
XML.default_load_external_dtd = false

YARD CHEATSHEET http://yardoc.org

cribbed from http://pastebin.com/xgzeAmBn

Templates to remind you of the options and formatting for the different types of objects you might want to document using YARD.

Modules

Namespace for classes and modules that handle serving documentation over HTTP

@vmoravec
vmoravec / spark.rb
Created September 4, 2013 21:44 — forked from jcromartie/spark.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# prints a sparkline in the terminal using the supplied list of numbers
# examples:
# spark.rb 10 20 30 100 90 80
# spark.rb 1 2 0.4 0.1 1.3 0.7
@ticks = %w[ ]
values = ARGV.map { |x| x.to_f }

TL;DR

Unicorn was by far the best performing (5.94 trans/sec over the 1-minute test, 352 total). Puma (3.95 trans/sec, 235 total) appeared to perform no better than Webrick, despite the default behavior of using up to 16 threads. Perhaps increasing its worker count to the number of cores will improve its performance.

I've tried to run multiple Puma workers with the workers directive (per their sample documentaiton), but I receive errors about undefined method 'workers' for #<Puma::Configuration::DSL:0x007ffca4bde798>).

Webrick

Server

$ bundle exec rails server

I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").

Understanding MiniTest::Expectations

Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):

# minitest/spec.rb

module MiniTest::Expectations
class RPMSpec
attr_reader :spec
def initialize(spec)
@tags = Hash.new {|h,k| h[k] = []}
@defines = {}
@spec = spec
@sections = Hash.new {|h,k| h[k] = []}
#@rpm_build_dir = rpm_build_dir
namespace :gemfile do
desc "filters the gemfile, leaving only gems belonging to specific groups"
task :filter => :environment do
groups = (ENV['groups'] || "").split(',')
if groups.empty?
puts "! Invalid usage"
puts
puts "Please specify the groups you want to filter. Example:"
# ---role
module BookManager
def create_book(attrs)
self.books.create! attrs
end
def update_book(book_id,attrs)
book = self.books.find(book_id)
book.update_attributes attrs
# This is a possible approach to baking in side-effect free support into
# the context object for DCI. The #on_call would support a declarative way of
# knowing which roles to mixin/unmix on a given object. This could
# use something like mixology or another implementation under the covers.
class AddToCartContext < Context
attr_reader :user, :book
def self.call(user, book)
AddToCartContext.new(user, book).call
@vmoravec
vmoravec / chat.rb
Created December 4, 2012 18:50 — forked from rkh/chat.rb
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do