Skip to content

Instantly share code, notes, and snippets.

@srawlins
srawlins / jqAddAndRemoveClass.js
Created March 8, 2013 01:02
Even if you don't have jQuery available, you can use the add and removeClass() functions!
function jQaddClass( elem, value ) {
var newClass = value;
if ( elem.nodeType === 1 ) {
if ( !elem.className ) {
elem.className = value;
} else {
var className = " " + elem.className + " ", setClass = elem.className;
if ( className.indexOf( " " + newClass + " " ) < 0 ) {
require "mechanize"
a = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }
a.get("https://www.google.com/") do |page|
puts page.at("#gbqfq").inspect
end
require "mechanize"
@srawlins
srawlins / obscure-gz-writing.rb
Last active December 19, 2015 12:09
Example of rack #571
require 'stringio'
require 'zlib'
GZ_FILE = "rack-issue-571.gz"
class VerboseStringIO < StringIO
attr_accessor :log
def initialize(*args)
@log = []
@srawlins
srawlins / curries.rb
Created February 22, 2014 00:20
flexible curry for Procs and methods in Ruby
def curry(prc, *args)
Proc.new { |*remaining| prc.call(*args.map { |arg| arg || remaining.shift }) }
end
def curry_m(m, n, *args)
define_method(m, curry(method(n), *args))
end
power = ->(a,b){ "#{a}**#{b} is #{a**b}" }
square = curry power, nil, 2
@srawlins
srawlins / gist:9244013
Created February 27, 2014 03:47
Reduce allocations during mail gem's specs
Before:
Top 10 allocation sites:
String allocations at <PWD>/lib/mail/utilities.rb:180
4639 allocations during ./spec/mail/attachments_list_spec.rb:312
...
Array allocations at <PWD>/lib/mail/parsers/received_parser.rb:13
2326 allocations during ./spec/mail/message_spec.rb:1627
...
String allocations at <PWD>/lib/mail/fields/common/common_field.rb:46
2269 allocations during ./spec/mail/fields/content_type_field_spec.rb:198
@srawlins
srawlins / gist:5b0fe367cd3a412e6925
Created May 22, 2014 00:14
Slowdown due to proposed Coverage changes ruby bug #9508

Slowdown of Mail specs due to use of Coverage

$ git clone [email protected]:mikel/mail.git $ cd mail $ bundle $ time bundle exec rake

  • w/o Coverage: 10.5 sec.
  • w/ current Coverage library: 12 sec (14% increase).
  • w/ proposed Coverage library: 16.5 sec (57% increase over "w/o Coverage", 38% increase over "current Coverage").
@srawlins
srawlins / gist:d03d7309b8b9ef22c980
Created September 2, 2014 00:38
Broken links on dartlang.org as of 2014-09-01
$ wget --spider -o ./broken-links.txt -e robots=off -w 1 -r -p https://www.dartlang.org/
$ cat broken-links.txt |grep -B 2 '404 Not'
--2014-09-01 17:08:11-- https://www.dartlang.org/articles/writing-unit-tests-for-pub-packages/
Reusing existing connection to www.dartlang.org:443.
HTTP request sent, awaiting response... 404 Not Found
--
--2014-09-01 17:13:48-- https://www.dartlang.org/articles/benchmarking/
Reusing existing connection to www.dartlang.org:443.
HTTP request sent, awaiting response... 404 Not Found
@srawlins
srawlins / args-changes.markdown
Last active August 29, 2015 14:06
Dart args SDK changes example

args/args

New class 'OptionType'

ArgParser

The 'addOption' method has a new parameter: { dart-core.String valueHelp: false }

# Through vertical-arrows (first 10 ifs)
%self total self wait child calls name
7.60 1.284 1.284 0.000 0.000 25396 String#split
5.60 0.946 0.946 0.000 0.000 117606 Array#flatten
4.08 0.690 0.690 0.000 0.000 487582 Regexp#match
4.03 1.053 0.680 0.000 0.372 393708 SCSSLint::ControlCommentProcessor#retrieve_comment_text
3.53 0.597 0.597 0.000 0.000 522139 Kernel#is_a?
3.42 0.860 0.578 0.000 0.282 396943 Enumerable#any?
3.21 0.542 0.542 0.000 0.000 492675 Kernel#class
@srawlins
srawlins / tables-spec.markdown
Last active September 30, 2025 18:32
CommonMark-like spec text for tables