Skip to content

Instantly share code, notes, and snippets.

@ericdke
ericdke / strip-ascii-colors.rb
Last active December 27, 2015 20:49
Ruby: strip ASCII colors
your_string.gsub!(/\e\[(\d+)(;\d+)*m/, '')
@ericdke
ericdke / special-chars.rb
Last active December 27, 2015 22:19
ASCII terminal special chars
def show_single_key
c = read_char
case c
when " "
puts "SPACE"
when "\t"
puts "TAB"
when "\r"
puts "RETURN"
when "\n"
@ericdke
ericdke / removeSpecialChars.rb
Last active December 30, 2015 02:09
remove special chars from string
def removeSpecialChars(str)
str.gsub(/[.:;?!-'`&"()\/]/, '')
end
@ericdke
ericdke / to_filesize.rb
Created December 7, 2013 18:50
bytes to kb,mb, etc
class Integer
def to_filesize
{
'B' => 1024,
'KB' => 1024 * 1024,
'MB' => 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
}.each_pair { |e, s| return "#{(self.to_f / (s / 1024)).round(2)}#{e}" if self < s }
end
@ericdke
ericdke / percentage.rb
Created December 16, 2013 20:35
percentage method
class Numeric
def percent_of(n)
self.to_f / n.to_f * 100.0
end
end
@ericdke
ericdke / http_progress.rb
Created December 16, 2013 20:40
Download with HTTP + progress indicator
uri = URI.parse(THE_URL)
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request["Authorization"] = "Bearer THE_AUTH_TOKEN"
request["Content-Type"] = "THE_TYPE"
body = ''
https.request(request) do |response|
fileSize = response['Content-Length'].to_i
@ericdke
ericdke / markdown_text_from_markdown_link.rb
Created December 16, 2013 20:43
Get Markdown text from Markdown link
@ericdke
ericdke / consistent-fonts-between-browsers.css
Created January 11, 2014 20:44
consistent fonts between browsers
body {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
@ericdke
ericdke / als.bashrc
Created January 26, 2014 16:16
Save last bash command in alias
# alias last and save
# use `als c NAME` to chop off the last argument (for filenames/patterns)
als() {
local aliasfile chop x
[[ $# == 0 ]] && echo "Name your alias" && return
if [[ $1 == "c" ]]; then
chop=true
shift
fi
aliasfile=~/.bashrc
@ericdke
ericdke / pman.rc
Created January 26, 2014 16:34
Read `man` page in Preview.app
# read man page in preview app
function pman () {
man -t $@ | open -f -a /Applications/Preview.app
}