Skip to content

Instantly share code, notes, and snippets.

View mloughran's full-sized avatar

Martyn Loughran mloughran

View GitHub Profile
@mloughran
mloughran / gist:721430
Created November 30, 2010 09:52
Appending to a string vs stringIO
require 'rubygems'
require 'benchmark'
require 'stringio'
Benchmark.bm do |x|
n = 1000000
x.report('string') do
string = ''
appended = 'b' * 100
@mloughran
mloughran / pwned.rb
Created December 4, 2020 17:37
Interactive tool to check passwords against the HIBP API
require 'digest/sha1'
require 'net/http'
API_ROOT = "https://api.pwnedpasswords.com/range/"
def split_sha(password)
sha = Digest::SHA1.hexdigest(password).upcase
[sha[0...5], sha[5..]]
end
@mloughran
mloughran / unmasking_benchmark.rb
Created April 4, 2011 18:56
Making WebSocket unmasking fast in ruby
module EventMachine
module WebSocket
class MaskedString < String
def read_mask
raise "Too short" if bytesize < 4 # TODO - change
@masking_key = String.new(self[0..3])
end
def slice_mask
slice!(0, 4)
# encoding: UTF-8
require 'digest/md5'
puts Digest::MD5.hexdigest("asdf") # => "912ec803b2ce49e4a541068d495ab570"
puts Digest::MD5.hexdigest("åß∂ƒ") # => "0d65ab8f1db3344230f46420e56e465f"
@mloughran
mloughran / getting-started.md
Last active May 24, 2019 12:40
Suggestions for getting started with Elm

Learning Elm

Start by following the guide from start to finish. It's really clear and written by Evan, Elm's author. Do the exercises!
https://guide.elm-lang.org

I found this example helpful as an example of piecing everything together after reading the guide
http://elm-bootstrap.info/getting-started

Don't worry about modules or splitting stuff into separate files. This advice is oft repeated in the Elm community and it's absolutely correct. The patterns that work in Elm aren't the patterns that work in other languages, and if you split modules out too early you'll make it harder to get a sense of the right module abstractions. Refacting is a breeze so you can modularise later. (See /The life of a file/ below).

Getting started

First add your twitter username and password. Then server.rb and once it's started open websocket.html in your browser. You should see some tweets appear. If not take a look at the javascript console.

Keybase proof

I hereby claim:

  • I am mloughran on github.
  • I am mloughran (https://keybase.io/mloughran) on keybase.
  • I have a public key ASCoOglpsX51BeVmxe0-db0fDe6iBgqtYlN2KKCtMcg_sgo

To claim this, I am signing this object:

@mloughran
mloughran / page.html
Created March 16, 2012 19:27
Mobile Safari crash when returning to WebSocket page
<html>
<head>
<script>
function debug(string) {
var element = document.getElementById("debug");
var p = document.createElement("p");
p.appendChild(document.createTextNode(string));
element.appendChild(p);
}
@mloughran
mloughran / .gitignore
Last active January 1, 2016 05:49
Comparison of using run time & memory usage to run a ruby script that requires a single gem (which does nothing) then exits.
.bundle
bundle
vendor
@mloughran
mloughran / Gemfile
Created August 15, 2012 19:33
em-http-request not closing connections (v 1.0.2)
source :rubygems
gem 'eventmachine', '1.0.0.rc.4'
gem 'em-http-request', '1.0.2'
gem 'sinatra'
gem 'thin'