Skip to content

Instantly share code, notes, and snippets.

Avatar
🌐
building itsy bitsy parts of the internet

Ryan McGeary rmm5t

🌐
building itsy bitsy parts of the internet
View GitHub Profile
@rmm5t
rmm5t / wheel-group-to-sudoers.sh
Last active Feb 1, 2019
Add wheel group to sudoers on macOS
View wheel-group-to-sudoers.sh
# Add current user to wheel group
sudo dseditgroup -o edit -a $USER -t user wheel
# Add wheel to sudoers
sudo su -
cd /etc
chmod 660 sudoers
vi /etc/sudoers
: <<'END_COMMENT'
@rmm5t
rmm5t / date_time_formats.rb
Last active Feb 1, 2019
Example of config/initializers/date_time_formats.rb
View date_time_formats.rb
Time::DATE_FORMATS[:long_date] = Date::DATE_FORMATS[:long_date] = "%b %e, %Y"
Time::DATE_FORMATS[:day] = Date::DATE_FORMATS[:day] = "%a, %e %b %Y"
Time::DATE_FORMATS[:local_time] = Date::DATE_FORMATS[:local_time] = "%I:%M%P %Z"
Time::DATE_FORMATS[:human] = Date::DATE_FORMATS[:human] = lambda { |t| t.strftime("%b %e, %Y %I:%M%P").sub(/0(\d):/, '\1:') } # Oct 2, 2011 5:57pm
Time::DATE_FORMATS[:rfc822] = "%e %b %Y" # Already in Date::DATE_FORMATS
@rmm5t
rmm5t / seeds.rb
Last active Feb 1, 2019
Example of idempotent seeds:
View seeds.rb
# Author: Ryan McGeary : http://github.com/rmm5t
# License: MIT : https://rmm5t.mit-license.org/
def restart_sequence(klass)
seq = [klass.table_name, "id", "seq"].join("_")
next_id = klass.maximum(:id) + 1
klass.connection.execute "ALTER SEQUENCE #{seq} RESTART #{next_id}"
end
def load_seed_data(klass, records)
View pg_searchable.rb
# Author: Ryan McGeary : http://github.com/rmm5t
# License: MIT : https://rmm5t.mit-license.org/
#
# TL;DR: A poor-man's full-text search.
#
# This module employs a mechanism by which we can easily add left outer joins
# to queries (typically for full-text-like searches that need to dig into
# specific associations).
#
# One way to do this is to just use ActiveRecord's `eager_load` as part of a
@rmm5t
rmm5t / promisify-node4.js
Created Aug 15, 2017
Node.js "promisify" implementations
View promisify-node4.js
const promisify = (fn) => {
return function () {
const args = Array.from(arguments);
return new Promise(function (resolve, reject) {
fn.apply(undefined, args.concat([function (error, data) {
if (error) return reject(error);
if (data === undefined) return resolve();
return resolve(data);
}]));
});
@rmm5t
rmm5t / OUTPUT.md
Last active Feb 19, 2021
How to properly introduce a new counter_cache to an existing Rails project.
View OUTPUT.md

Fast/efficient approach:

-- execute("UPDATE posts SET comments_count = (SELECT count(1) FROM comments WHERE comments.post_id = posts.id)")
   -> 1.3197s

Slow/naïve approach:

View iso8601_benchmark.rb
require 'benchmark'
require 'time'
n = 1_000_000
t = Time.new.utc
Benchmark.bmbm do |x|
x.report("unix epoch") { n.times { t.to_i.to_s } }
x.report("iso 8601 ") { n.times { t.iso8601 } }
end
View keybase.md

Keybase proof

I hereby claim:

  • I am rmm5t on github.
  • I am rmm5t (https://keybase.io/rmm5t) on keybase.
  • I have a public key whose fingerprint is 79DA 34B8 A2E7 3C96 E0CC 5BB2 005A C77D 7CBF 3EAF

To claim this, I am signing this object:

View bob.rb
class Bob
def respond_to(input)
@input = input.strip
return "Fine. Be that way!" if silence?
return "Whoa, chill out!" if shouting?
return "Sure." if question?
"Whatever."
end