Skip to content

Instantly share code, notes, and snippets.

View davich's full-sized avatar

David Carlin davich

  • Melbourne, Australia
View GitHub Profile
@davich
davich / instance_var_envy.rb
Created June 25, 2021 10:44
Find methods that use instance variables set in other methods.
require 'parser/current'
code = <<-RUBY
class Model
def initialize
@a = 1
@b = 2
end
def a1
@davich
davich / dependency.rb
Last active June 18, 2020 02:48
Which dependencies to inject?
class InjectMe
def initialize(
kernel:,
name_utils_class:,
text_helper_class:,
email_class:,
car:,
pet:,
payment_processor:,
random_class:,
use sha2::{Digest, Sha256};
use std::fmt::Write;
fn main() {
println!("Answer is {:?}", rust_mine("hello"));
}
pub fn rust_mine(text: &str) -> String {
let mut hasher = Sha256::new();
let mut text = text.to_string();
(0..std::usize::MAX)
.find_map(|nonce| {
use sha2::{Digest, Sha256};
fn main() {
println!("Answer is {:?}", rust_mine("hello"));
}
pub fn rust_mine(text: &str) -> String {
let mut hasher = Sha256::new();
(0..std::usize::MAX)
.find_map(|nonce| {
hasher.input(&format!("{}{}", text, nonce));
let result = hasher.clone().result();
use std::collections::HashMap;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let ints = vec![1, 3, 5, 7, 8, 1];
assert_eq!(average(&ints), 4.166666666666667);
assert_eq!(median(&ints), 6.0);
@davich
davich / handle_event.ex
Created April 28, 2018 05:04
Sad path programming using Elixir's 'with' command
def handle_event(persisted_event) do
with :not_handled <- ActionFramework.handle_event(persisted_event),
:not_handled <- Action.handle_event(persisted_event),
:not_handled <- Comment.handle_event(persisted_event) do
:error
@davich
davich / k
Created September 1, 2016 23:40
#!/usr/bin/env ruby
# Open Buildkite builds for the current repo and branch
repo = `git config --get remote.origin.url | sed -nE 's/.*\\.com[:\\/]?(.*)?/\\1/p' | sed -E 's/.git//'`.chomp
branch = ARGV.length > 0 ? ARGV[0] : `git rev-parse --abbrev-ref HEAD`.chomp
`open https://buildkite.com/#{repo}/builds?branch=#{branch}`