Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
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
@awesome
awesome / android-emulator-homebrew.sh
Created May 14, 2019 03:20 — forked from spilth/android-emulator-homebrew.sh
Android Emulator with Homebrew
touch ~/.android/repositories.cfg
brew cask install caskroom/versions/java8
brew cask install android-sdk
brew cask install intel-haxm
brew install qt
export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"
sdkmanager "platform-tools" "platforms;android-27" "extras;intel;Hardware_Accelerated_Execution_Manager" "build-tools;27.0.0" "system-images;android-27;google_apis;x86" "emulator"
avdmanager create avd -n test -k "system-images;android-27;google_apis;x86"
/usr/local/share/android-sdk/tools/emulator -avd test
sql = "SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = 'awesome_db'"
x = ActiveRecord::Base.connection.execute(sql)
# remove constraints
z = x.map {|y|
"remove_foreign_key :#{y[0]}, column: '#{y[1]}', name: '#{y[2]}'\n" +
@awesome
awesome / database.rake
Created April 19, 2019 10:48 — forked from olivierlacan/database.rake
Database rake tasks that I use on Code School to ferret out huge tables with millions of rows and see how many indices they have and to see which tables have missing indices on associated tables (foreign keys). The latter was taken from this great post by Tom Ward: https://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes
namespace :database do
task fat_tables: :environment do
c = ActiveRecord::Base.connection
max_table_name_width = 0
tables = c.tables.sort_by do |t|
max_table_name_width = t.length if t.length > max_table_name_width
@awesome
awesome / dotnetlayout.md
Created March 28, 2019 16:32 — forked from davidfowl/dotnetlayout.md
.NET project structure
$/
  artifacts/
  build/
  docs/
  lib/
  packages/
  samples/
  src/
 tests/
@awesome
awesome / Simple Ruby Class Example.rb
Created March 11, 2019 04:24 — forked from dougal/Simple Ruby Class Example.rb
Simple Ruby Class Example
class Badger
attr_accessor :name, :size # Creates getter and setter methods.
def initialize(name, size)
@name = name
@size = size
end
# Instance method
@awesome
awesome / gist:56677ba7bcfab364bc826024ab371e2f
Created February 7, 2019 00:39 — forked from yurikoval/gist:3663539
Passing arguments to an around_filter
# app/controller/articles_controller.rb
around_filter { |controller, action| controller.send(:add_published_scope, Article){ action.call } }
# app/controller/application_controller.rb
def add_published_scope(klass = nil, &block)
unless klass
klass = controller_name.classify.constantize
end
klass.with_scope(:find => klass.where("published_at <= ?", Time.zone.now)) do
yield
@awesome
awesome / image-to-datauri.rb
Created November 13, 2018 11:34
image to datauri one-liner ruby @soawesomeman
require 'open-uri'
require 'base64'
filename = "lib/templates/assets/image.jpg"
datauri = "data:image/#{File.extname(filename).delete('.')};base64," + Base64.strict_encode64(open(filename).read)