Skip to content

Instantly share code, notes, and snippets.

Avatar

Stephen Ball sdball

View GitHub Profile
@sdball
sdball / kafka-consumer.rb
Last active Jan 23, 2020
Simple generic Ruby Kafka producer/consumer for testing
View kafka-consumer.rb
require "kafka"
require "logger"
require "optparse"
script_name = File.basename($0, File.extname($0))
default_logfile = "logs/#{script_name}.log"
default_offset = "latest"
options = {}
OptionParser.new do |opts|
@sdball
sdball / sandi_metz_rules_for_developers.md
Created Jan 18, 2013
Rules for good development from Sandi Metz
View sandi_metz_rules_for_developers.md

Sandi Metz’ rules for developers

  1. Your class can be no longer than a hundred lines of code.
  2. Your methods can be no longer than five lines of code
  3. You can pass no more than four parameters and you can't just make it one big hash.
  4. In your controller, you can only instantiate one object, to do whatever it is that needs to be done.
  5. Your view can only know about one instance variable.
  6. Your Rails view should only send messages to that object i.e., no Demeter violations.[ "thunder dome principal". Translated: one model in, one model out! ]
  7. Rules are meant to be broken if by breaking them you produce better code. [ ...where "better code" is validated by explaining why you want to break the rule to someone else. ]
@sdball
sdball / nil_what_are_you_doing_stahp.rb
Last active Mar 16, 2019
Sometimes Ruby is Weird
View nil_what_are_you_doing_stahp.rb
class NilWhatAreYouDoingStahp
attr_accessor :not_nil
def initialize
@not_nil = "I'm not nil! I'm a value!"
end
def wat
if not_nil.nil?
not_nil = "Ok, now I'm not nil."
@sdball
sdball / nilbog.rb
Created Dec 14, 2018
From whence nothing but nil can return
View nilbog.rb
class Nilbog
class_eval do
Object.methods.each do |method|
class_eval("def #{method}; #{nil}; end")
end
end
def method_missing(_)
nil
end
View letters.sh
check_letter() {
echo "$1: `type $1 >/dev/null && type $1 | sed -e \"s/^$1: //\" | tr '\n' ' '`";
}
letters() {
for letter in {a..z}; do
check_letter $letter
check_letter `echo $letter | tr a-z A-Z`
done
}
View required_args.js
'use strict';
// none of this is good
class Greet {
required(arg) {
throw new Error(`${arg} is required`);
}
requireArgs(given, required) {
@sdball
sdball / ssl_cert_extraction_example.ex
Last active Mar 31, 2017
functions to extract SSL certs for Erlang SSL from ENV variables
View ssl_cert_extraction_example.ex
def ssl_config do
ssl_config(client_cert, client_cert_key)
end
def ssl_config(_client_cert=nil, _client_cert_key=nil) do
[]
end
def ssl_config(client_cert, client_cert_key) do
[
@sdball
sdball / elixir_data_bytes.exs
Created Jul 19, 2016
Looking at types of Elixir byte data
View elixir_data_bytes.exs
is_binary <<255::size(8)>> # => true
is_bitstring <<255::size(8)>> # => true
is_binary <<255::size(4)>> # => false
is_bitstring <<255::size(4)>> # => true
# So a binary is a bitstring but a bitstring isn't necessarily a binary.
# We can see that effect in pattern matching as well:
<<255::size(4)>> # => <<15::size(4)>>
<<x>> = <<255::size(4)>> # => MatchError!
View mutation_behavior.rb
original = [
{ a: { foo: 1 } },
{ b: { foo: 2 } },
{ c: { foo: 3 } },
{ d: { foo: 4 } },
{ e: { foo: 5 } },
{ f: { foo: 6 } },
{ g: { foo: 7 } },
]