Skip to content

Instantly share code, notes, and snippets.

View kwstannard's full-sized avatar

Wolf kwstannard

  • Andros
  • New York City
View GitHub Profile
@kwstannard
kwstannard / gist:613cebba33eaf0bf767d473d9e5eaa57
Last active October 24, 2020 19:05
a logger with no if statements
require 'active_support/core_ext/string'
module Logger
class Null
def initialize
@blob = {}
end
def unknown(*); end
def fatal(*); end
@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 September 8, 2020 23:52
Every line in makery ordered by line length
[[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 September 24, 2019 20:42
dotenv, but its a one line bash function
$ 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
def set_env
around do |ex|
@old_env = ENV.to_h
yield ENV
ex.call
ENV.replace(ENV)
end
end
ag ENV.*?[A-Z_]+ -o --no-filename | ag -o [A-Z_]+$ | sort | uniq
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;
+------+
>> 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 October 21, 2016 19:06
equivalent to elixer pipe in ruby
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]
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'