Skip to content

Instantly share code, notes, and snippets.

@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"
@britishtea
britishtea / .bash_prompt.sh
Last active October 11, 2015 17:57
My bash prompt
# This bash prompt is loosely based on https://gist.github.com/31967
red=$(tput setaf 1)
yellow=$(tput setaf 3)
green=$(tput setaf 2)
blue=$(tput setaf 6)
bold=$(tput bold)
reset=$(tput sgr0)
# Detects whether the current directory is a git repository
@britishtea
britishtea / flat_map.rb
Created December 6, 2012 23:58
#map { ... }.flatten vs #flat_map
require 'benchmark'
arr = 1_000.times.map { (1..100).to_a }
Benchmark.bm 9 do |x|
x.report('flatten') { arr.map { |x| x << 1001 }.flatten(1) }
x.report('flat_map') { arr.flat_map { |x| x << 1001 } }
end
# test: $ ruby flat_map.rb
@britishtea
britishtea / dependency.rb
Last active March 30, 2017 19:43
A null object in Ruby with refinements
module Dependency
def self.give_me_the_nil
nil
end
end
@britishtea
britishtea / twitter.rb
Last active December 18, 2015 01:39
A Twitter plugin for Cinch
require "cinch"
require "tweetstream"
module Cinch
module Plugins
class Twitter
include Cinch::Plugin
def initialize(*args)
super
object = []
object.object_id # => 70185911630120
hash = Hash.new([])
hash[:one].object_id # => 70185911630120
hash[:two].object_id # => 70185911630120
@britishtea
britishtea / fib.rb
Last active December 20, 2015 01:09
Memoized Fibonacci numbers with Hash default values
Fib = Hash.new { |hash, key| hash[key] = hash[key - 2] + hash[key - 1] }
Fib.merge! 0 => 0, 1 => 1
require 'minitest'
require 'minitest/autorun'
describe Fib do
it 'fibs' do
assert_equal Fib[0], 0
assert_equal Fib[1], 1