Skip to content

Instantly share code, notes, and snippets.

View wjlroe's full-sized avatar
📷

William Roe wjlroe

📷
View GitHub Profile
@wjlroe
wjlroe / README.md
Last active March 25, 2024 12:29
Can you detect memory allocated via malloc in C (via Ruby's FFI) when calling GC.stat?

Results of running this:

❯ ruby ffi.rb
Allocating 1048576 via malloc in FFI
	before malloc: 		maxrss: 	37044224
	before scribble: 	maxrss: 	37093376
	after scribble: 	maxrss: 	38141952
heap_live_slots: 48069 -> 48073 (diff: 4)
heap_free_slots: 4098 -> 4094 (diff: -4)
@wjlroe
wjlroe / test_have_attributes_and.rb
Created March 20, 2023 13:59
Show RSpec's unhelpful error output when combining matchers
require 'rspec'
Person = Struct.new(:name, :age)
RSpec.describe do
subject { Person.new("Bob", 32) }
it { is_expected.to have_attributes(name: "Bob", age: 33) }
# ^ This failure prints the attributes diff as follows:
@wjlroe
wjlroe / two_filters_elasticsearch_query.json
Last active August 14, 2022 21:09
An (contrived) example of how you can combine a query (a prefix query in this case) with more than one filter
{
"query": {
"filtered": {
"query": {
"prefix": {"name": "Ms."}
},
"filter": {
"and": [
{"exists": { "field": "favourite_opera" }},
{"range": {"last_opera_attended": {"gte": 1357020000}}}
@wjlroe
wjlroe / room.rb
Created May 21, 2011 15:47
to_xml - is there a better way?
class Room < ActiveRecord::Base
def full
false
end
def to_xml(options = {})
options[:methods] = [:full]
super(options)
end
end
const std = @import("std");
pub fn main() !void {
const allocator = std.heap.c_allocator;
const cwd = try std.process.getCwdAlloc(allocator);
defer allocator.free(cwd);
var paths = std.ArrayList([]const u8).init(allocator);
{
@wjlroe
wjlroe / README.md
Created November 20, 2012 17:16
JSON API -> CSV

Howto download a JSON API array and spit out a CSV

Tools needed

  • httpie -- easy_install httpie (probably you want to sudo easy_install httpie
  • this script -- curl -o /usr/local/bin/json-to-csv.py https://raw.github.com/gist/4119347/24adb5dcd9c025c272042653a70d25b73b485958/json-to-csv.py
  • make that executable -- chmod +x /usr/local/bin/json-to-csv.py

Here we go

@wjlroe
wjlroe / playing_with_threads.c
Last active April 30, 2019 14:35
Playing with C11's stdatomics like atomic_compare_exchange and atomic_fetch_add for thread-safe global counters
#include <stdatomic.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 1000
pthread_t threads[NUM_THREADS];
@wjlroe
wjlroe / composable_sort.rb
Created April 25, 2018 16:10
Riddle me this: How can you compose sort_by in Ruby?
require 'minitest/autorun'
class ComposableSort
def initialize(stuff)
@stuff = stuff
end
def sorted
@stuff.sort_by { |item|
criteria.flat_map { |criterium| criterium.call(item) }
@wjlroe
wjlroe / keybase.md
Created January 3, 2018 17:58
keybase.md

Keybase proof

I hereby claim:

  • I am wjlroe on github.
  • I am willroe (https://keybase.io/willroe) on keybase.
  • I have a public key ASCQHZg1eVUWgaxWCRTjheQvY_6LhcuTDpJu8pdi-a3AnAo

To claim this, I am signing this object:

@wjlroe
wjlroe / README.md
Created August 13, 2012 10:20
How to serve static files with POW

Problem

You wanna serve up static files, but are bored by the usual trick of:

python -m SimpleHTTPServer

This is pretty simple but why type this every time? Let's use Pow for some zero-effort web serving. If you have Pow already set up, then just drop the provided config.ru file into the root of your static files. Then link your directory into ~/.pow so Pow can see it and that's it!

Let's say you have a directory with mockups you wanna show people, they are in ~/Dropbox/Mockups/ then you might do this: