Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
@awesome
awesome / ruby-regexp-uuid-and-dash-optional.rb
Last active September 5, 2022 14:14
UUID pure regexp and dash/hyphen is optional; return only full uuid matched
# https://ihateregex.io/expr/uuid/
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
# https://rubular.com/r/sXJ27X08h7tKZ2 refactor by soawesomeman
/([0-9a-fA-F]{8}-?(?:[0-9a-fA-F]{4}-?){2}[0-9a-fA-F]{4}-?[0-9a-fA-F]{12})/
# https://rubular.com/r/lIO6HeHPlHcTl3 further refactor by soawesomeman
# see: https://stackoverflow.com/questions/15205896/matching-any-character-except-an-underscore-using-regex#15206584
/((?:(?!_)[\w\d]){8}-?(?:(?:(?!_)[\w\d]){4}-?){3}-?(?:(?!_)[\w\d]){12})/
@awesome
awesome / ruby-hash-with-or-without-rockets-to-javascript-object-notation-aka-json-style-howto-by-soawesomeman.rb
Created June 28, 2022 22:58
ruby-hash-with-or-without-rockets-to-javascript-object-notation-aka-json-style.rb
# ruby-hash-with-or-without-rockets-to-javascript-object-notation-aka-json-style.rb
#
# - context: https://alwayscoding.ca/momentos/2012/06/15/ruby-hash-syntax-hashrocket-vs-json-style
# - regex by SoAwesomeMane: https://rubular.com/r/9wlDeHLHejZvuB
# - regex research: https://sadique.io/blog/2013/09/29/named-capture-groups-in-regular-expressions
# - more regex research: https://www.regular-expressions.info/refquick.html "\k Named backreference"
# - json schema from https://www.liquid-technologies.com/online-json-to-schema-converter
# - 🍻🍻🍻
hash = {"$schema"=>"http://json-schema.org/draft-04/schema#",
@awesome
awesome / format-ticket-title-to-git-branch-name.rb
Created June 21, 2022 22:17
format ticket title to git branch name
str = "ZZZ://ZZZZZZ (ZZZZ allocation - slider does not automatically reflect the entered Stop value and requires a click outside o
f the Stop input field)"
str.downcase.gsub(/\W/, '-').squeeze('-')[0..45].chomp('-')
# => "zzz-zzzzzz-zzzz-allocation-slider-does-not-aut"
@awesome
awesome / pad-an-array-to-be-a-certain-size.rb
Created December 17, 2021 23:55
Pad An Array To Be A Certain Size in Ruby
# https://stackoverflow.com/questions/5608918/pad-an-array-to-be-a-certain-size#16180695
def rpad(item, padding, num)
Array(item).fill padding, Array(item).size, num
end
@awesome
awesome / ruby-url-to-hash-and-query-to-hash.rb
Created December 9, 2021 19:49
Ruby: URL-to-Hash & Query-to-Hash
RX_URL = Regexp.new('^((?<protocol>https?):\/\/)?(?<host>[^\/]+)(\/(?<path>[^\?#]+))?(\?(?<query>[^#]+))?(#(?<fragment>.*))?', 'i')
# @see http://stackoverflow.com/questions/18825669/how-to-do-named-capture-in-ruby#18825787
# @param url_str [String] URL string with or without `https://`, `http://`
# @return [Hash] hash with keys: `:protocol, :host, :path, :query, :fragment`
def url_to_hash(url_str)
m = url_str.match(RX_URL)
m.names.map(&:to_sym).zip(m.captures).to_h
end
@awesome
awesome / ruby-securerandom-fake-string-of-alphanumeric-characters-always-lead-with-alphabetic-characters.rb
Created September 14, 2021 19:49
Ruby SecureRandom fake string of alphanumeric characters; always lead with alphabetic characters.
#
# "generate a string with 0-9a-z (36 characters) that is ALWAYS 12 characters long. Change 12 to whatever length you want."
# -- Gerry Shaw via https://stackoverflow.com/questions/88311/how-to-generate-a-random-string-in-ruby#comment35392122_1619602
#
# min length is 6
def fake_code(length = 12)
# always lead with four chars
str = SecureRandom.hex.gsub(/\d/, "")[0..3]
# followed by chars or int
@awesome
awesome / ERB-h_html--j_json--t_textile--u_url.rb
Created December 4, 2020 20:08
2020 ERB escape and text-transformation
##
# Using Text Transformation Methods
#
# ERB provides optional methods for transforming text.
# To use these features your code must include the module ERB::Util.
# via "An Introduction to ERB templating" (July 01, 2016) https://www.stuartellis.name/articles/erb/#using-text-transformation-methods-
#
# ERB transformation has changed since 2016: "j" not defined as alias but still executes without error, albiet weird rendering
@awesome
awesome / archive_desktop.sh
Created March 25, 2020 16:56
macOS archive_desktop bash script
#!/bin/bash
ts=$(date "+%Y%m%d%H%M%S")
da=~/Documents/Desktop_Archives/$ts
mkdir -p $da
mv ~/Desktop/* $da
echo "created $da"
@awesome
awesome / ruby-sanitize-string-remove-nonword-chars-preserve-dashes-and-underscores.rb
Created January 30, 2020 00:17
Ruby sanitize string: remove nonword-chars, but preserve dashes and underscores.
str1 = "11423!@#$!@_asdf+_1234__asfsdf"
str1.gsub(/\W/,'')
# => "11423_asdf_1234__asfsdf" (seems legit…)
str2 = "11423!@#$!@_asdf+_1234__asf-sdf"
str2.gsub(/\W/,'')
# => "11423_asdf_1234__asfsdf" (doesn't preserve dash)
str2.gsub(/[^-\w]/,'')
# => "11423_asdf_1234__asf-sdf" (profit!)
[7] pry(#<#<Class:0x007fbefaceac00>>)> rows_per_page, items_per_row = PdfBuilder.rows_per_page_items_per_row("image_large")
=> [3, 2]
[8] pry(#<#<Class:0x007fbefaceac00>>)> array = 8.times.map(&:to_s)
=> ["0", "1", "2", "3", "4", "5", "6", "7"]
[9] pry(#<#<Class:0x007fbefaceac00>>)> array.in_groups_of(items_per_row).in_groups_of(rows_per_page)
=> [[["0", "1"], ["2", "3"], ["4", "5"]], [["6", "7"], nil, nil]]
[10] pry(#<#<Class:0x007fbefaceac00>>)> array.in_groups_of(items_per_row, []).in_groups_of(rows_per_page, [])
=> [[["0", "1"], ["2", "3"], ["4", "5"]], [["6", "7"], [], []]]
[11] pry(#<#<Class:0x007fbefaceac00>>)> array.in_groups_of(items_per_row).in_groups_of(rows_per_page, [])
=> [[["0", "1"], ["2", "3"], ["4", "5"]], [["6", "7"], [], []]]