Skip to content

Instantly share code, notes, and snippets.

Avatar

Man Vuong kidlab

  • Toptal (@toptal)
  • Ho Chi Minh City, Vietnam
View GitHub Profile
View 1_graphql_ruby_n_plus_one.md

How to run examples:

  1. Run $ createdb nplusonedb to create DB
  2. Run specs $ rspec demo.rb
View log-extraction.go
func main() {
s := time.Now()
args := os.Args[1:]
if len(args) != 6 { // for format LogExtractor.exe -f "From Time" -t "To Time" -i "Log file directory location"
fmt.Println("Please give proper command line arguments")
return
}
startTimeArg := args[1]
finishTimeArg := args[3]
@kidlab
kidlab / gist:e961bc606801505a9e544d1e1ac2bf4a
Last active Jul 31, 2019 — forked from khakimov/gist:3558086
Matrix Effect in you terminal
View gist:e961bc606801505a9e544d1e1ac2bf4a
echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
# Binary version
echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="00100001100100011101011111101110110010110110011000011000011110010101000"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
@kidlab
kidlab / System Design.md
Created Dec 2, 2018 — forked from vasanthk/System Design.md
System Design Cheatsheet
View System Design.md

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@kidlab
kidlab / rspec-best-practices.md
Last active Nov 23, 2018
RSpec Best Practices
View rspec-best-practices.md

Standardize your code first

gem install fasterer
fasterer
  • Rubocop Performance is another good document. We can consider enabling Performance in .rubocop.yml as well.

Then, standardize your specs

@kidlab
kidlab / manual_preloading.rb
Created May 2, 2018 — forked from sobstel/manual_preloading.rb
Rails manual association preloading
View manual_preloading.rb
# taken from https://mrbrdo.wordpress.com/2013/09/25/manually-preloading-associations-in-rails-using-custom-scopessql/
# collection association e.g. has_many
owners = People.all
association_name = :photos
owners.each do |owner|
records = Array(whatever_you_want)
@kidlab
kidlab / README.md
Last active Nov 3, 2017
Ruby memory conscious
View README.md

MEASURING MEMORY USAGE

There are several gems that help with determining where a program allocates memory. The two that I most often use are allocation_tracer and memory_profiler.

Both tools can measure a whole program or they can be turned on and off to only measure certain parts of a program. Either method allows you to determine hotspots in your program and then act on the information. For example, while developing kramdown several years ago I found that the HTML converter class allocated huge amounts of throw-away strings. By changing this hotspot to a better alternative kramdown got faster and used less memory.

To get you started on using these two gems, here are two files that are intended to get pre-loaded using the -r switch of the ruby binary (i.e. ruby -I. -ralloc_tracer myscript.rb).

Credit: https://gettalong.org/blog/2017/memory-conscious-programming-in-ruby.html

@kidlab
kidlab / .rvmrc
Created Nov 3, 2017 — forked from eirc/.rvmrc
Ruby-based Benchmark of MessagePack vs. JSON vs. Yajl vs. Protobuffers vs. MultiJson vs. Marshal vs. YAML vs. BSON
View .rvmrc
rvm --create ree@benchmarks
@kidlab
kidlab / GIF-Screencast-OSX.md
Created May 12, 2017 — forked from dergachev/GIF-Screencast-OSX.md
OS X Screencast to animated GIF
View GIF-Screencast-OSX.md

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF

Instructions

To capture the video (filesize: 19MB), using the free "QuickTime Player" application:

@kidlab
kidlab / Rakefile.rb
Created Apr 18, 2017 — forked from drogus/Rakefile.rb
This is the example contents of the Rakefile, which you would use to run active record tasks without using Rails. It assumes using the same directories as rails uses: `db/migrate`, `config/database.yml`.
View Rakefile.rb
require 'bundler/setup'
require 'active_record'
include ActiveRecord::Tasks
db_dir = File.expand_path('../db', __FILE__)
config_dir = File.expand_path('../config', __FILE__)
DatabaseTasks.env = ENV['ENV'] || 'development'