Skip to content

Instantly share code, notes, and snippets.

View derigible's full-sized avatar

Marc Phillips derigible

  • CrowdStorage
  • Lehi, Utah
View GitHub Profile
@derigible
derigible / prepend_frozen_string_literals
Created March 23, 2023 00:06
Update files that don't have the frozen_string_literal comment
#! /bin/zsh
set -e
rubocop | grep FrozenStringLiteralComment | awk '{ print $1 }' | awk -F ':' '{ print $1 }' | awk '{$1=$1};1' | xargs sed -i "" '1s/^/# frozen_string_literal: true\n\n/'
@derigible
derigible / copy_to_clipboard.js
Created March 30, 2021 12:23
Copy a text field to the clipboard (used in react, but can be extended to other frameworks)
const copyToClipboard = textRef => e => {
e.target.focus();
let text = textRef.current.value;
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText(text);
} else {
// Safari support
let element = document.createElement('a');
element.setAttribute('href', text);
element.textContent = text;
@derigible
derigible / install_script.sh
Created January 21, 2021 00:22
UDOIT Install script
#!/usr/bin/bash
# install script for setting up all dependencies for running this application
yes_no () {
echo
echo "$1?"
select yn in "Yes" "No"; do
case $yn in
Yes ) return 0;;
@derigible
derigible / problem.md
Last active January 13, 2021 22:25
Relay Server

The Problem

There are two programs that cannot establish a direct connection to each other. Write a generic TCP relay server that these two programs can use to send data back and forth. This is common in peer-to-peer networks where one peer cannot talk to another peer, but both peers can talk to a third party (ie the relay server).

Requirements

  1. Implement a relay server program that listens for requests over a TCP connection on a given port (for example, port 8080) that will return a (host, port) pair to the requesting client. The relay server should then be able to forward traffic from any program that connects to that (host, port) to the requesting client.
  2. Any program should be able to take the (host, port) as parameters to talk to the requesting client (via the relay). Assume these programs are not under your control and cannot be modified. An example program might be telnet.
  3. The requesting client is a program that you can assume is under your control, so any modifications necessary to cont
@derigible
derigible / divide-without-operator
Last active December 18, 2020 16:27
Coding Challenges
# Given numbers n and k, divide n by k and return the quotient and remainder
# without using the divider (/) operator.
# Example:
# Input: 5, 2
# Output: [2, 1]
# Input: 4, 2
# Output: [2, 0]
admin page for user_report
Steps:
rails generate controller AdminPortal user_report user_lookup user_select_action
- Default generator creates a lot of files, these include the following:
create app/controllers/admin_portal_controller.rb - the controller
route get 'admin_portal/user_report' - updates the routes file to point to actions in controller
@derigible
derigible / gist:3b8bf165c41c95693ee323829011eb17
Created June 17, 2020 21:06
ActiveRecord::NoDatabaseError (FATAL: database "" does not exist ):
// you need to create a database before rails can start (omitted from the start guide as you technically don't need a database for a rails app)
$ rails db:create
// You should now be able to start your rails application
$ rails s
@derigible
derigible / gist:0667a03b2cbf1db29e0506aea5364226
Created June 17, 2020 21:03
rails PG::ConnectionBad (FATAL: role "" does not exist)
// Likely you created a rails project with --database=postgresql
// If you haven't yet, setup a password for the postgres user, you can get into psql this way
$ sudo -u postgres psql
// Create the role
$ postgres=# CREATE ROLE <role> CREATEDB LOGIN PASSWORD 'password';
CREATE ROLE
// OR, if your user has been setup you can use the createuser util that comes with postgres
@derigible
derigible / gist:189887797b5f032a1095fd15a86a6919
Created June 17, 2020 20:16
rails new throws 'Could not locate Gemfile or .bundle/ directory' error
// if using rubyenv
$ whereis rails
rails: /usr/bin/rails /home/username/.rbenv/shims/rails
// use the rails file directly
/home/username/.rbenv/shims/rails new
@derigible
derigible / installruby.sh
Last active June 28, 2019 17:40
Adding rbenv to Ubuntu
sudo apt install rbenv
# Installing ruby-build for latest ruby
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
# List possible installs
rbenv install -l
# Install ruby you want