Skip to content

Instantly share code, notes, and snippets.

View rafbm's full-sized avatar

Rafael Masson rafbm

View GitHub Profile
@rafbm
rafbm / gist:9068f377f54b6b7a0915
Created January 19, 2015 15:52
Ruby String#tr VS String#gsub benchmark
require 'benchmark'
STRING = 'Haha hah hahaa ahah!!'
FIND = 'a'
REPLACE = 'o'
STRING.tr(FIND, REPLACE) == STRING.gsub(FIND, REPLACE) or abort
Benchmark.bmbm do |bm|
bm.report '.tr' do
@rafbm
rafbm / clean-markup-line-numbers.html
Created March 27, 2011 22:23
<table>-free way to display <pre> line numbers that don’t mess up the clipboard.
<html>
<head>
<meta charset="utf-8">
<title>Clean-Markup Line Numbers™</title>
<style>
body {
width: 800px;
margin: 0 auto;
}
@rafbm
rafbm / pusher-v1_3_2-v1_3_3.patch
Created October 11, 2019 19:26
Diff between versions 1.3.2 and 1.3.3 of `pusher` gem
diff --git a/README.md b/README.md
index 97c0bfb..365acbd 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ or install via gem
gem install pusher
```
-After registering at <https://dashboard.pusher.com/>, configure your Pusher Channels app with the security credentials.
+After registering at <https://dashboard.pusher.com/>, configure your Channels app with the security credentials.
@rafbm
rafbm / config.ru
Created January 28, 2012 23:29
Example Sinatra app using the Namespace extension from Sinatra::Contrib
require 'sinatra/base'
require 'sinatra/namespace'
class MyApp < Sinatra::Base
register Sinatra::Namespace
get '/' do
'Home'
end
def call
scope = base_relation.joins(:care_periods)
if params.care_provider_id.present?
scope = scope
.where(care_periods: { care_provider_id: params.care_provider_id })
end
if params.hospital_id.present?
scope = scope
@rafbm
rafbm / jquery.in-groups-of.js
Created December 15, 2010 14:21
Returns an array of jQuery objects, grouped by specified number of elements.
/*
* Returns an array of jQuery objects, grouped by specified number of elements.
*
* Say you have 17 <frameset> tags on your page...
*
* $('frameset').inGroupsOf(7); // => [ jQuery[0..6], jQuery[7..13], jQuery[14..16] ]
*
*/
$.fn.inGroupsOf = function( countPerGroup ) {
@rafbm
rafbm / json_benchmark.rb
Created May 22, 2013 20:12
Benchmarking Ruby’s JSON.load and JSON.parse.
require 'benchmark'
require 'json'
str = '{"foo": "bar"}'
Benchmark.benchmark Benchmark::CAPTION, 11 do |bm|
bm.report 'JSON.load' do
500_000.times { JSON.load(str) }
end
@rafbm
rafbm / human_name.rb
Last active December 17, 2015 04:29
Fix common downcase/whitespace issues in people names
# encoding: utf-8
require 'minitest/autorun'
# `gem install unicode_utils`
require 'unicode_utils/downcase'
require 'unicode_utils/upcase'
module HumanName
def self.all_lower_case?(name)
@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'