Skip to content

Instantly share code, notes, and snippets.

Avatar

Kelly Stannard kwstannard

  • Andros
  • New York City
View GitHub Profile
@kwstannard
kwstannard / gist:613cebba33eaf0bf767d473d9e5eaa57
Last active Oct 24, 2020
a logger with no if statements
View gist:613cebba33eaf0bf767d473d9e5eaa57
require 'active_support/core_ext/string'
module Logger
class Null
def initialize
@blob = {}
end
def unknown(*); end
def fatal(*); end
View metaprogramming but inspectable
@class_generator = ->(name, attrs) {
file = "tmp/meta_classes/#{name.underscore}.rb"
FileUtils.mkdir_p(File.dirname(file))
template = File.write(file, ERB.new(<<TEMP, nil, "%").result(binding))
class <%= name %>
attr_accessor <%= attrs.map(&:inspect).join(', ') %>
def initialize(<%= attrs.join(", ") %>)
% attrs.each do |attr|
@<%= attr %> = <%= attr %>
@kwstannard
kwstannard / line lengths
Created Sep 8, 2020
Every line in makery ordered by line length
View line lengths
[[3, "end"],
[3, "end"],
[3, "end"],
[3, "end"],
[5, " end"],
[5, " end"],
[5, " end"],
[5, " end"],
[5, " end"],
[5, " end"],
@kwstannard
kwstannard / foo.sh
Last active Sep 24, 2019
dotenv, but its a one line bash function
View foo.sh
$ be() { if [ -f .env ]; then env $(<.env grep -v "^#") bundle exec "$@"; else bundle exec "$@"; fi; }
$ echo HELLO=WORLD > .env
$ echo "# a comment" >> .env
$ be ruby -e 'puts ENV["HELLO"]'
WORLD
View foo.rb
def set_env
around do |ex|
@old_env = ENV.to_h
yield ENV
ex.call
ENV.replace(ENV)
end
end
View herp.sh
ag ENV.*?[A-Z_]+ -o --no-filename | ag -o [A-Z_]+$ | sort | uniq
View foo.sql
mysql> create temporary table tmp (d INT(11));
Query OK, 0 rows affected (0.02 sec)
/* there are 2 rows being inserted with value of 1 */
mysql> insert into tmp (d) values (1), (2), (1), (3);
Query OK, 4 rows affected (0.01 sec)
/* this shows that distinct works */
mysql> select distinct d from tmp;
+------+
View gist:d0fa4e71ac8a584199115c123a0a5f5f
>> RUST_BACKTRACE=1 cargo run > log.txt
Compiling serde v1.0.43
Compiling core-foundation-sys v0.5.1
Compiling regex-syntax v0.5.5
Compiling walkdir v2.1.4
Compiling base64 v0.9.1
Compiling proc-macro2 v0.3.8
Compiling num-traits v0.1.43
Compiling rand v0.4.2
Compiling net2 v0.2.32
@kwstannard
kwstannard / insert_args.rb
Last active Oct 21, 2016
equivalent to elixer pipe in ruby
View insert_args.rb
class Proc
def <<(*args)
args = args.flatten if args.count == 1
-> (*items) { self.call(*items, *args) }
end
end
adder = -> (*args) { args.inject(&:+) }
puts [1,2,3,4,5]
View has_one_through_problem.rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'