Skip to content

Instantly share code, notes, and snippets.

View rafbm's full-sized avatar

Rafael Masson rafbm

View GitHub Profile
@rafbm
rafbm / 20130401200058_increase_delayed_jobs_handler_length.rb
Created April 2, 2013 00:41
ActiveRecord migration for increasing MySQL column length from TEXT to MEDIUMTEXT
class IncreaseDelayedJobsHandlerLength < ActiveRecord::Migration
def up
# Increase to MEDIUMTEXT (16,777,215 bytes)
change_column(:delayed_jobs, :handler, :text, limit: 16.megabytes - 1)
end
def down
# Back to default TEXT (65,535 bytes)
change_column(:delayed_jobs, :handler, :text)
end
@rafbm
rafbm / gist:5209832
Last active December 15, 2015 05:29
Character escaping in Ruby
irb(main):051:0> puts 'foo\bar'
foo\bar
nil
irb(main):052:0> puts 'foo\\bar'
foo\bar
nil
irb(main):053:0> puts 'foo\\\bar'
foo\\bar
nil
irb(main):054:0> puts 'foo\\\\bar'
@rafbm
rafbm / input.html
Created March 1, 2013 23:03
A reliable HTML minifier isn’t a thing.
<ul>
<li>Foo</li>
<li>These <b>two</b> <b>words</b> should stay separate.</li>
<li>Bar</li>
</ul>
@rafbm
rafbm / application_helper.rb
Last active December 14, 2015 09:50
Rails view helper for stripping whitespace between elements. Very useful for using `display: inline-block`.
module ApplicationHelper
def inline_tag(*args, &block)
body = Nokogiri::HTML(capture(&block)).at_css('body')
# First-level text nodes
body.xpath('text()').each do |node|
node.content = node.content.strip
end
content_tag(*args) { body.inner_html.strip.html_safe }
describe ReportsController do
describe :create do
before do
@payload = { :version => "1.0.0" }
end
it "succeeds with present serial" do
@payload[:serial] = "123abc"
post reports_path, @payload.to_json
@rafbm
rafbm / gist:4944651
Created February 13, 2013 13:37
Benchmarking `respond_to?` and `rescue NoMethodError` for method delegation in Ruby.
require 'benchmark'
# Say you have a Foo class and you want instances of this class to delegate unknown
# methods to two objects, first to @object_1, then to @object_2.
class Foo
def initialize
@object_1 = nil
@object_2 = ''
end
@rafbm
rafbm / code.rb
Created February 1, 2013 12:31
Markdown rendering with syntax highlighting
# gem install redcarpet
# gem install pygments.rb
require 'redcarpet'
require 'pygments'
class MarkdownWithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, lexer: language)
end
@rafbm
rafbm / gist:4644755
Last active December 11, 2015 18:49
Benchmarking the creation of an array of digits, lowercase and uppercase letters
require 'benchmark'
# Which is faster for creating an array of numbers, lowercase and uppercase letters?
Benchmark.benchmark Benchmark::CAPTION, 12 do |bm|
bm.report 'String#split' do
100_000.times { '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('') }
end
bm.report 'Range#to_a' do
100_000.times { ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a }
@rafbm
rafbm / challenge.md
Created January 3, 2013 18:36
CSS Challenge #2

CSS Challenge #2

You start with this:

And must end up with this:

@rafbm
rafbm / challenge.md
Last active December 10, 2015 01:08
CSS Challenge

CSS Challenge

You start with this:

<style>
  div {
    display: inline-block;
    width: 250px;
 padding: 20px;