Skip to content

Instantly share code, notes, and snippets.

require "benchmark"
require "benchmark/ips"
require "dish"
hash = {
title: "My Title",
authors: [
{ id: 1, name: "Mike Anderson" },
{ id: 2, name: "Well D." }
],
@britishtea
britishtea / for.rb
Last active August 29, 2015 14:05
Benchmark: #each vs for
require "benchmark"
require "benchmark/ips"
range = Array(1..100_000)
Benchmark.ips do |x|
x.report("each") { range.each { |x| } }
x.report("for") { for x in range; end }
end
@britishtea
britishtea / symbol.rb
Created October 11, 2014 17:17
Two small extensions to `Symbol`.
class Symbol
def [](*args)
proc do |object|
object.send self, *args
end
end
def ~
proc do |object, *args|
object.send self, *args
@britishtea
britishtea / benchmark.rb
Created October 13, 2014 13:43
String#=~ vs String#include? vs String#[]
Benchmark.ips do |x|
x.report { x="str"; x =~ /foo/ || x =~ /bar/ }
x.report { x="str"; x.include?("foo") || x.include?("bar") }
x.report { x="str"; x["foo"] || x["bar"] }
end
@britishtea
britishtea / result.txt
Created January 26, 2015 18:22
Building a long chain of words (PPCG code golf challenge)
["Del", "Delmar", "mar", "mariachi", "chickpea", "pea", "peahen", "henpecking", "ingraining", "ingratiated", "tedious", "ousters", "ersatzes", "zest", "estranging", "ingenuously", "sly", "slyness", "essayist", "isthmus", "mushing", "ingredient", "entertainment", "entryway", "waywardness", "essences", "cession", "ion", "ionization", "ionizer", "zeros", "roses", "sesame", "amelioration", "ionosphere", "erection", "ionospheres", "resale", "alerted", "tediously", "slyest", "establishment", "entrenchment", "entombed", "bedevilment", "entailing", "inglorious", "ousting", "ingested", "tediousness", "essay", "saying", "ingrown", "ownership", "hippie", "piercingly", "glycerin", "ringing", "ingrains", "insurgent", "entertainingly", "glycerol", "rolling", "ingenuousness", "essaying", "ingenuous", "ouster", "terracing", "ingesting", "ingratiating", "ingestion", "ionizing", "ingot", "got", "gotten", "tensing", "ingrates", "testable", "blest", "estimations", "onshore", "ore", "ores", "restructure", "urea", "rears", "arseni
@britishtea
britishtea / benchmark.rb
Created February 20, 2015 20:56
Is _ as an argument name optimized or just a convention?
def does_care(*a)
"hi"
end
def cares_a_little(*_a)
"hi"
end
def does_not_care(*_)
"hi"
@britishtea
britishtea / trades.rb
Created June 27, 2011 22:01
Bitcoin trade data
# I turned the script into a Ruby gem. To install it, first install Rubygems if you haven't already. Instructions are here: https://rubygems.org/pages/download. Next type: sudo gem install trades
#
# To run the script type:
# trades
#
# Or if you only want to see trades from specific exchanges:
# trades mtgoxUSD thUSD britcoinGBP
#
# You can specify as many exchanges as you want. Just make sure you use the same name Bitcoincharts uses.
#
@britishtea
britishtea / np.scpt
Last active September 28, 2015 06:27
Modified /np for Textual
on textualcmd(ignore, destination)
if destination is equal to "" then
return "/debug Invalid destination channel."
error number -128
end if
-- iTunes
if isRunning("iTunes") then
tell application "iTunes"
if player state is playing then
@britishtea
britishtea / scopes.rb
Created February 21, 2012 15:12
Scopes in Ruby
module Namespace
class String
def a
String.inspect
end
def b
::String.inspect
end
end
@britishtea
britishtea / string.rb
Created April 14, 2012 15:54
String#sample
class String
def sample
slice rand(length)
end
end
puts "hahaha".sample # => "h"