Skip to content

Instantly share code, notes, and snippets.

View nguyendangminh's full-sized avatar

Nguyen Dang Minh nguyendangminh

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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)