Skip to content

Instantly share code, notes, and snippets.

View sdball's full-sized avatar

Stephen Ball sdball

View GitHub Profile
@sdball
sdball / sandi_metz_rules_for_developers.md
Created January 18, 2013 18:47
Rules for good development from Sandi Metz

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 / recommended.md
Last active August 6, 2021 03:40
Recommended code reading from Ruby Parley
@sdball
sdball / kafka-consumer.rb
Last active January 23, 2020 05:14
Simple generic Ruby Kafka producer/consumer for testing
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 / nil_what_are_you_doing_stahp.rb
Last active March 16, 2019 01:35
Sometimes Ruby is Weird
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 December 14, 2018 16:58
From whence nothing but nil can return
class Nilbog
class_eval do
Object.methods.each do |method|
class_eval("def #{method}; #{nil}; end")
end
end
def method_missing(_)
nil
end
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
}
'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 March 31, 2017 13:59
functions to extract SSL certs for Erlang SSL from ENV variables
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 July 19, 2016 13:37
Looking at types of Elixir byte data
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!
original = [
{ a: { foo: 1 } },
{ b: { foo: 2 } },
{ c: { foo: 3 } },
{ d: { foo: 4 } },
{ e: { foo: 5 } },
{ f: { foo: 6 } },
{ g: { foo: 7 } },
]