Skip to content

Instantly share code, notes, and snippets.

View ramonrails's full-sized avatar
💭
Available to build SaaS, PaaS, MVP, consult and mentor

Ram ramonrails

💭
Available to build SaaS, PaaS, MVP, consult and mentor
View GitHub Profile
@ramonrails
ramonrails / fetch-website.sh
Created February 29, 2024 18:08
wget wrapper function to fetch a website
#!/bin/bash
# fetch entire website
#
# Example:
# fetch-website https://www.bharat.in
#
function fetch-website {
# Check if wget is available
if ! command -v wget &>/dev/null; then
@ramonrails
ramonrails / format_string_with_regex.rb
Last active February 7, 2024 18:31
reformat string with regex
# Usage:
# tested with 8, 9, 10, 11, 12 digit phone numbers
#
# ar = ["(+1) 888 33x19", "95606 3354", "555 123 1234", "0 95606 33544", "(+91) 95606 33544"]
# ar.map {|e| format_string(e) }
# => ["188-833-19", "956-063-354", "555-123-12-34", "095-606-335-44", "919-560-633-544"]
#
def format_string(s)
# step #1 => remove all non-digits
# step #2(a) => pick the digits in groups of 333[2][2]
@ramonrails
ramonrails / pdf-compress.sh
Last active February 24, 2024 12:25
Compressing a PDF using Ghostscript on command line
# utility functions
#
# compress PDF using ghostscript
#
# https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input
#
# -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
# -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
# -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
@ramonrails
ramonrails / factories__users.rb
Last active January 16, 2024 09:37
Resilient, faster and maintainable seeding in ruby on rails
# frozen_string_literal: true
# a simple user factory
# factories/users.rb
#
FactoryBot.define do
factory :user do
name { Faker::Name.unique.name }
email { Faker::Internet.email(domain: "bharat.in") }
# some users will randomly be "admin"
@ramonrails
ramonrails / matching_brackets.js
Created January 3, 2024 19:27
Matching brackets (simple version)
// matching the brackets
// * one type of brackets only
// * opening must appear before closing bracket
// * pre or post padding characters should not affect the result
// * any other characters in0between anywhere should not affect the result
//
const balanced = (arg) => {
//
// `for char of result` should also work here
//
@ramonrails
ramonrails / matching_multi_brackets.js
Last active January 8, 2024 19:08
Matching brackets problem - with multiple bracket types
// Goal: find matching enclosures
// * opening enclosure must appear before closing
// * multiple opening enclosures can appear, but must be closed in proper reverse order
// * any other text, character ot symbol should not affect the result
// * pre or post padding space or characters should not affect the result
const balanced = (string) => {
// example: { "{}": 0, "[]": 0, ...}
let enclosures = { "{}": 0, "[]": 0, "()": 0, "<>": 0 }
// stack of keys must match closing braces in the reverse order
@ramonrails
ramonrails / leetcode_3.rb
Last active November 12, 2023 09:11
Leetcode #3 (flawed?)
# [Leetcode #3](https://leetcode.com/problems/longest-substring-without-repeating-characters/) says
# Given a string s, find the length of the longest substring without repeating characters.
#
# Example 1:
#
# Input: s = "abcabcbb"
# Output: 3
# Explanation: The answer is "abc", with the length of 3.
#
# Example 2:
@ramonrails
ramonrails / patterns_in_array.rb
Last active January 8, 2024 19:10
Algorithm - Patterns in array
# Faker: A ruby gem (MIT) to generate test data
# https://github.com/faker-ruby/faker
require "faker"
# generate a random set of words
#
# => ["temporibus", "laborum", "et", "facilis", "assumenda", "autem", "voluptatem"]
#
# words = rand(5..10).times.collect { Faker::Lorem.word }
words = Array.new(rand(5..10)) { Faker::Lorem.word }
@ramonrails
ramonrails / range_multiples.rb
Created December 19, 2022 16:02
ruby range with output based on multiples of 3, 5, 15
# frozen_string_literal: true
require 'benchmark'
# synopsis
#
# rubocop suggested syntax is
# * costlier on execution
# * not really that super easy to read and maintain than case statement
# * adds more number of lines
#
# Bucket is a wrapper around Apartment::Tenant for utility methods
#
class Bucket
PUBLIC = "public".freeze
SYSTEM_NAMES_REGEX_PATTERN = 'information_schema|pg_\w+'.freeze
#
# Class methods
#