Skip to content

Instantly share code, notes, and snippets.

module Mixin
def foo
puts "foo"
end
end
class A; end
a = A.new
@britishtea
britishtea / inject.rb
Created October 1, 2013 18:56
Benchmarking Symbol vs Symbol#to_proc for Enumerable#inject
require "benchmark"
require "benchmark/ips"
array = Range.new(1,1_000_000).to_a
Benchmark.bmbm do |x|
x.report("#inject &:*") { array.inject &:* }
x.report("#inject :*") { array.inject :* }
end
@britishtea
britishtea / example_case.rb
Last active December 25, 2015 12:49
Simple and possibly broken pattern matching in Ruby. Proof of Concept.
require "pattern_matching"
# Let's abuse case statements and the case equality method (#===) to implement
# pattern matching in Ruby.
#
# Note that this'll only work in Ruby 2.1.0-dev (2.0.0-p247 and lower does not
# honour refined #=== in case statements).
using PatternMatching
@britishtea
britishtea / example.rb
Last active December 31, 2015 16:09
Simple pattern matching in Ruby that won't win any beauty contests.
require 'function'
# A fibonacci function.
fib = Function.new
fib[0] = 0
fib[1] = 1
fib[Integer] = ->(i) { fib[i - 2] + fib[i - 1] }
p fib[0] # => 0
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 / benchmark.rb
Last active January 27, 2021 18:37
Array#join vs Regexp.union (ran on Ruby 2.0.0)
require "benchmark/ips"
input = %w[one two three four five six seven]
Benchmark.ips do |x|
x.report "Array#join" do
Regexp.new input.map { |e| Regexp.escape e }.join "|"
end
x.report "Regexp.union" do
@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