Skip to content

Instantly share code, notes, and snippets.

View nguyendangminh's full-sized avatar

Nguyen Dang Minh nguyendangminh

View GitHub Profile
@nguyendangminh
nguyendangminh / rubyist.rb
Last active August 29, 2015 14:09
Rubyist
# default value
PHRASE_FIRST = ARGV[0] == "english"
open(aFile) {|f|
f.each_line {|word| words.push(word.chomp)}
}
def craft_names(rand_words, snippet, pattern, caps=false)
@nguyendangminh
nguyendangminh / split_join_file
Created November 22, 2014 18:01
Split and Join file(s) in Linux
# Split a file into parts
$ split --bytes=SIZE input_file
# Join files together
$ cat file_part.* > output_file
@nguyendangminh
nguyendangminh / config.rb
Created December 9, 2014 02:40
Reading YAML config file in Ruby
# config.rb
require 'yaml'
config = YAML.load_file('config/config.yml')
puts config['user_data']
# config.yml
user_data: 'hello world'
@nguyendangminh
nguyendangminh / file.rb
Last active August 29, 2015 14:11
Working with file in Ruby
# Reading whole file
bootstrap = File.read('config/bootstrap')
# Relative path.
# web_app/
# app.rb
# db/sample.db
# In the app.rb
"#{File.expand_path(File.dirname(__FILE__))}/db/sample.db"
@nguyendangminh
nguyendangminh / string.rb
Created December 9, 2014 03:02
Working with string in Ruby
# Replace a pattern
bootstrap = "ID = USER_ID"
id = 12345.to_s
new_bootstrap = bootstrap.gsub('USER_ID', id) # return a new string
bootstrap.gsub!('USER_ID', id) # replace in place
@nguyendangminh
nguyendangminh / time.rb
Created December 9, 2014 04:14
Working with time in Ruby
# Timestamp
puts Time.now
puts Time.now.to_s
puts Time.now.to_i
puts Time.now.to_i.to_s
@nguyendangminh
nguyendangminh / sinatra.rb
Last active August 29, 2015 14:11
Snippets for Ruby Sinatra
### Spawn a long run process in Sinatra ###
require 'sinatra'
get '/process' do
child_pid = Process.fork do # spawn a long run process
tryCitus.setup(config)
Process.exit
end
Process.detach child_pid
##############################
@nguyendangminh
nguyendangminh / rvm.sh
Last active August 29, 2015 14:11
RVM - Ruby Version Manager
# Note:
# RVM does not control the system Ruby or its gems. Only Rubies and gems installed by RVM are under it's control!
# Install RVM
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
$ \curl -sSL https://get.rvm.io | bash -s stable --ruby
$ source /home/ubuntu/.rvm/scripts/rvm
# Install a Ruby version
$ rvm install ruby-1.9.3
@nguyendangminh
nguyendangminh / bash.sh
Created December 22, 2014 07:53
Gist for Bash
# Get path of directory in which the script is stored
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )
echo $DIR
# Use content of a file as argument
kill -9 $(<./thin.pid)
@nguyendangminh
nguyendangminh / gist.go
Created December 28, 2014 18:07
Gist for Golang
// Logging
import (
"log"
"time"
)
func response(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Hello world!"))
log.Printf(time.Now().String(), r.Method, r.URL.String())
}