Skip to content

Instantly share code, notes, and snippets.

View lazyatom's full-sized avatar
🤠
Yes

James Adam lazyatom

🤠
Yes
View GitHub Profile
require "rubygems"
require "bundler/setup"
require "sinatra"
require "base64"
get "/" do
Base64.encode64(File.binread("image"))
end
Fedifeed implementation notes
Two components
Server implements a few endpoints
/reblogs — returns accounts that you reblog the most
/core_servers — returns the instances who host most of the accounts you follow
/favourites — returns users that you favourite the most
Client hits those endpoints, and then:
@lazyatom
lazyatom / convert_image_to_bytes.rb
Created July 11, 2012 22:08
Small script to convert an image to printer commands, based on http://github.com/freerange/printer
#!/usr/bin/env ruby
# You should ensure that the `rmagick` and `a2_printer` gems are installed.
require "rubygems"
require "RMagick"
require "a2_printer"
if ARGV.length < 2
puts "Usage: convert_image_to_bytes.rb <image_file> <output_file>"
@lazyatom
lazyatom / Output
Last active October 3, 2016 12:36
Huffman encoding and decoding
$ ruby huffman.rb this is a test of compression
input length in bits 232 (29 * 8)
Binary 1111100000100011001000110100101101111101000111111001111100110111010111001110001101111010000001001110110
Encoded string length in bits: 103
Compression: 44.396551724137936%
Decoded: this is a test of compression
@lazyatom
lazyatom / jruby_refinements.rb
Created December 18, 2015 14:40
Do refinements work in JRuby?
module Refinement
refine Object do
def new_method
'new method!'
end
end
end
class Thing
using Refinement

How Should We Use Struct?

The Choice

It's common in Ruby to see some code setup a Struct like this:

class Specialized < Struct.new(:whatever)
  # ... define custom methods here...
end
@lazyatom
lazyatom / rocco.css
Created April 2, 2013 09:29
Rocco CSS
/*--------------------- Layout and Typography ----------------------------*/
body {
font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
font-size: 15px;
line-height: 22px;
color: #252519;
margin: 0; padding: 0;
}
a {
color: #261a3b;
@lazyatom
lazyatom / anonimise_selections.sql
Created March 14, 2013 13:26
Ruby Manor 4.0 voting - anonymised
select substring(md5(<first salt> || proposal_id) for 8) as proposal_hash, substring(md5(<second salt> || user_id) for 8) as user_hash from selections;
@lazyatom
lazyatom / thing.rb
Last active December 12, 2015 00:18
Is this a pattern? Does it have a name? It allow swapping out basically the whole behaviour of the `Thing`.
class Thing
def self.runner=(custom_runner)
@runner = custom_runner
end
def self.runner
@runner ||= new
end
def run(*args)
@lazyatom
lazyatom / test.rb
Created January 29, 2013 10:51
An example of action usage in Kintama
describe ThingController do
describe "creating a thing" do
action { post :create, attributes }
describe "when not logged in" do
it "should redirect to login" do
assert_redirected_to login_path
end
end