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-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 / opposite_of_array_intersection.rb
Created January 21, 2010 17:44
Ruby opposite of array intersection
# Ruby opposite of array intersection... or maybe the method is missing from my brain bc not enough coffee
# http://twitter.com/soawesomeman/status/8035087261
def awesome(ar_1, ar_2)
(ar_1 + ar_2) - (ar_1 & ar_2)
end
awesome([1,2,3,4], [3,4,5,6]) # => [1, 2, 5, 6]
@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"], [], []]]
@awesome
awesome / bash_completion
Created December 6, 2012 09:28 — forked from milligramme/bash_completion
brew install bash-completion
$ brew install bash-completion
==> Downloading http://bash-completion.alioth.debian.org/files/bash-completion-1.3.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3
==> make install
==> Caveats
Add the following lines to your ~/.bash_profile file:
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
Dpkg: check that unix package is installed. Bash script: install package if not present
By neokrates, written on May 21, 2010
Was the package already installed or it still needs to be?
Software:
✔ Ubuntu *
dpkg -s can be used.
@awesome
awesome / format_join.rb
Created January 7, 2020 19:51
Replace non-word-characters with underscore or dash; without preceding or trailing underscores or dashes.
def format_join(string, joiner=' ')
string.split(/\W/).reject(&:empty?).join(joiner.to_s)
end
# $ ruby -v
# ruby 2.1.9p490 (2016-03-30 revision 54437) [x86_64-darwin18.0]
#
# $ irb
# irb(main):001:0> def format_join(string, joiner=' ')
# irb(main):002:1> string.split(/\W/).reject(&:empty?).join(joiner.to_s)
@awesome
awesome / how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
Last active June 6, 2019 20:36
How to get the name of the current file with directory filepath using ruby. @awesome #soawesomeman
# $ ruby -v
# ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
# $ pwd
# /Users/dev/Documents/Github/Gists/awesome/
# $ echo "puts File.expand_path(__FILE__)" > how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# $ echo "puts File.absolute_path(__FILE__)" >> how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# $ cat how-to-get-the-name-of-the-current-file-with-directory-filepath.rb
# puts File.expand_path(__FILE__)
# puts File.absolute_path(__FILE__)
# $ ruby how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb