Skip to content

Instantly share code, notes, and snippets.

@preetpalS
preetpalS / rails_pg_backup.rb
Last active June 20, 2021 05:30
Command line app that wraps PostgreSQL commands for backup and restore of either your entire database or only its data (written in Ruby using standard library without any external gems).
#!/usr/bin/env ruby
require 'date'
require 'optparse'
require 'ostruct'
# TODO: Add quoting for file option/argument
# TODO: See if using 'pathname' stdlib could improve code.
# Rails PostgreSQL database backup helper application
@preetpalS
preetpalS / scale_win10_uwp_assets.rb
Last active November 7, 2016 00:04
Windows 10 UWP quick-and-dirty scaled image asset generation with ImageMagick
## Instructions
# 1. Place this file (modified appropriately with your paths) in folder with images to be scaled.
# 2. Run this file with Ruby (e.g. "ruby scale_win10_uwp_assets.rb").
## Results
# Creates a folder named "compiled_images" with scaled images.
## Notes
# You should create appropriate versions of your assets for different uses (and their scales and/or targets).
@preetpalS
preetpalS / prime_encoding_maximums.rb
Created May 23, 2016 00:10
Calculations of how many primes can be encoded into a product for fixed-width integer types.
# Contains utilities for working with Prime numbers.
class Prime
class << self
def prime?(n)
return false if n < 2
return true if n < 4
return false if n.even?
(3..(Math.sqrt n).ceil).each do |i|
@preetpalS
preetpalS / leftpad_numeric_filenames_with_zeros.rb
Created June 25, 2016 00:59
Script to left pad numeric filenames with leading 0's
require 'pathname'
DIRECTORIES_TO_IGNORE = ['.git']
EXTENSIONS_TO_IGNORE = [ # not implemented
/^.git*$/,
'.DS_Store'
].freeze
MINIMUM_WIDTH = 3
MOVE_COMMAND = ->(current_path, destination_path) {
@preetpalS
preetpalS / dotenv-mode.el
Last active October 30, 2019 22:11
An Emacs major mode for .env files
;;; dotenv-mode.el --- Major mode for .env files -*- lexical-binding: t; -*-
;; Author: Preetpal S. Sohal
;; URL: https://github.com/preetpalS/emacs-dotenv-mode
;; Version: 0.2.5
;; Package-Requires: ((emacs "24.3"))
;; License: GNU General Public License Version 3
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@preetpalS
preetpalS / problemB.rb
Created April 9, 2017 10:04
tidy numbers
#!/usr/bin/env ruby
require 'set'
def is_tidy(n)
narr = n.to_s.split('')
snarr = narr.sort
narr.join('') == snarr.join('')
end
@preetpalS
preetpalS / fast_directory_delete.bat
Last active August 4, 2017 06:34
Windows Fast Directory Delete (Batch File)
:: See https://superuser.com/a/289399 for more information on performance of approach.
@echo off
echo Windows Fast Directory Delete
echo Deleting directory: %1
echo Deleting files (within %1) recursively...
del /F/S/Q %1 > nul
echo Deleting directory (%1) recursively...
rmdir /S/Q %1
echo Process complete.
@preetpalS
preetpalS / handle_sigbreak_on_windows.rb
Created May 6, 2018 05:59
Handling SIGBREAK signals with existing SIGINT signal handlers on Windows in Ruby
# Sets up clean SIGBREAK signal handling on Windows by taking advantage of existing
# SIGINT signal handlers.
(-> {
Signal.trap(21) do
raise SignalException, 'INT'
end
}).call if Gem.win_platform?
@preetpalS
preetpalS / random_password_generator.rb
Last active November 16, 2018 14:23
Simple command line random password generator (Ruby)
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
class RandomPasswordGenerator
VERSION = %w(0.1.0)
CORE_CHARACTER_SET = [('a'..'z').to_a,
('A'..'Z').to_a,
@preetpalS
preetpalS / openssl-notes.txt
Created May 4, 2020 09:52 — forked from tsaarni/openssl-notes.txt
Generate self-signed certs with different key types
*** RSA
# Generate self-signed certificate with RSA 4096 key-pair
openssl req -x509 -nodes -days 3650 -newkey rsa:4096 -keyout rsakey.pem -out rsacert.pem
# print private and public key
openssl rsa -in rsakey.pem -text -noout
# print certificate
openssl x509 -in rsacert.pem -text -noout