Thursday, December 25, 2014

[Ruby] string to hex, hex to string

String to Hex

require 'digest'
Digest.hexencode(string)
string.unpack('H*').first
Hex to String

[hex].pack('H*')
Credit: http://stackoverflow.com/a/17223012 



Wednesday, December 3, 2014

Calculate sum of memory usage data with Awk

I don't know how to use awk, but I have a problem which I know it suits for.

I want to sum up memory usage data that come out from executing the command ps aux. I learned 2 usages of awk from this lesson.

Print value of a column with:
awk '{ print $COLUMN_NUMBER }'

Sum value of a column with:
awk '{ temp = temp + $COLUMN_NUMBER }END{ print temp }'

So I can get sum of memory usage of processes with certain name pattern like this:
$ ps aux | grep NAME_PATTERN | awk '{temp = temp + $6}END{ print temp }'

I still don't know awk, but at least I'm incrementally learning it.

Credits:
simple awk tutorial
Add up a column of numbers at the Unix shell

Wednesday, October 29, 2014

Get terminal to speak after finished a long processing command on OSX

Just append
 && say "Done"

For example;
 $ sleep 10 && say "Done"

This trick could be useful if you are running some tasks and want switch to work on something else while it's running but also want to get back to it immediately after it finished.

Source: How to make the hardware beep sound in Mac OS X 10.6


Wednesday, August 27, 2014

Really stop Clojure's infinite loop future with future-cancel

If you like me, executed future-cancel and the future is still running. This blog post is for you.

I suppose your code is something similar too this:
(def f (future (while true (your-function ... ))))
(future-cancel f)
All you have to do as describe in this SO comment is to replace true with (not (Thread/interrupted)). Example:
(future (while (not (Thread/interrupted)) (your-function ... )))
Because future-cancel will send interrupt signal to the future, Thread/interrupted will be changed to true. Now your future-cancel should work as expected.
 
 
 
 

 

Handy note from The Future of Ruby Performance Tooling talk

GoRuCo 2014 - The Future of Ruby Performance Tooling by Aaron Quint

It introduces a few numbers of promising Ruby performance tooling. I'll definitely refer back to this post when I do performance optimization.

Tuesday, July 22, 2014

[PostgreSQL] See SQL queries (without restarting the server!)


1. In postgresql.conf (probably under /etc/postgres/...), uncomment log_statement and set to 'all'
log_statement = 'all'
2. To reload config, sending SIGHUP to postgres server by going to psql shell and execute:
select pg_reload_conf();
    There's an another way to send SIGHUP, please refer to the link below

3. Observe the queries at postgresql.log (probably under /var/log/...)
4. To disable, comment out log_statement and reload config again

log_statementreloading postgresql config

Credit: http://stackoverflow.com/a/8208376

Sunday, July 20, 2014

Run clojure.test inside Light Table

Simply add a line
(run-tests)
and evaluate source code normally (cmd+shift+enter). See the output in the console.

Saturday, June 7, 2014

Clone a PostgreSQL database

At some point in development, you may want to make a copy of your existing development database to play with an unstable new feature. Postgres makes it super trivial to do that by allow us to create a new database by using existing database as a template.

In postgres console, run
=# CREATE DATABASE new_database_name WITH TEMPLATE original_database_name;

Boom!, you got a cloned database named new_database_name.

Credit: Creating a copy of a database in Postgres
PS: I've seen a convenient way to make a clone across remote server too. (Haven't try) How to copy postgres database to another server

Update Aug 27, 2015:
Instead of running command in psql, we can use createdb with -T flag like so
$ createdb -T original_database_name new_database_name
Credit: http://stackoverflow.com/a/6739995 

Friday, May 16, 2014

Tmux: open new pane/window base on current directory

You need to rebind your short key with argument -c "#{pane_current_path}"

E.g.
New vertical pane
Default: bind % split-window -h
To: bind % split-window -h -c "#{pane_current_path}"

New horizontal pane
Default: bind '"' split-window
To: bind '"' split-window -c "#{pane_current_path}"

New window
Default: bind c new-window
To: bind c new-window -c "#{pane_current_path}"

Credit: Tmux forgets the directory where the session was created

Monday, March 31, 2014

How to zip in Clojure

In programming, there's time when we have 2 arrays and we want values at the same index of both arrays to have operation on each other producing an array of result. In imperative style, we have to setup a temporary counter variable to store index the current operation is on, increment it until reaching the last value of an array. For example, we want to sum values of each index.

In functional style, there's a function called 'zip' (Haskell, Ruby) which allows us to pair up values of each index preparing to apply operation on each pair later.

In Haskell, there's even 'zipWith' which apply operation on the pair immediately instead of producing an intermediate array.

But when it comes to Clojure, I wasn't be able to find a function in its standard library with the same behavior. The closest I could find is 'zipmap' that returns back a map which is not exactly what I want. We definitely can work around a little bit to get a vector.

But as you can see, it doesn't preserve the order of an original vectors.

The next idea I have is to use 'interleave' and 'partition'


It's kinda work, but the fact that we have to call 2 functions is not so satisfied.

I went look up and found this Stack Overflow answer. Yes. Just use simple 'map'!


I think, I overlooked this solution because in Ruby, the language the I'm most comfortable with, 'map' can only operate on only one array.

Learning that Clojure's map can takes any number of collections is an aha moment for me. So now I can sum elements of more than 2 vectors easily.


Since the second argument can accept any function, creating Clojure records is as easy as this.

Wednesday, March 5, 2014

Speed up command line file download with Axel

Axel is a command line file downloading tool that can create multiple connection to download parts of the file and combine them later after the download finish. It's perfect replacement for cURL or Wget in downloading a big file.

To use it, as simple as;

axel -n 10 http://url/big-file
10 is number of connections

It's available through Homebrew, agt-get and other OS package managers, I suppose.

Using this tool always reminds me to FlashGet and Download Accelerator which I had used more than 10 years ago.

Thanks @kamal who introduced it to me.

Thursday, February 27, 2014

[Git] Prune when fetch/pull

When a branch is removed from your git central server, remote branch on your machine won't get removed automatically. You might already know popular command to clean those removed branches, which is
git remote prune origin
But also you could give --prune or -p argument to fetch or pull to do the pruning for you after it's done with its normal behavior. For example,
git fetch -p
git pull -p
If you are in the habit of using remote prune that's fine. These prune arguments just save you one extra step if you just put them in to help cleanup once in a while.

Tuesday, January 28, 2014

Rails4's Active Record where not nil

Since Rails 4.0 no more .where('column_name IS NOT NULL'). Instead, we now can use
.where.not(column_name: nil)
Yay!

Tuesday, January 21, 2014

start-stop-daemon: help for start, stop, create pidfile for your command that doesn't have pidfile

You have a command to start a long running process but it doesn't create a pidfile for you to write a script to start/stop it. start-stop-daemon could help that.

This is a document of the command.

Example:

start-stop-daemon -d dir_want_to_run_on -b -m -p pidfile.pid --start --startas command
start-stop-daemon -p pidfile.pid --stop

-b : background
-m : make file (need to use with -p)
-p : pid file name (need to use with -m)

Monday, January 20, 2014

Light Table: Stop the execution

Use `Eval: Cancel evaluation for the current client` or `Editor: Disconnect clients attached to editor` from the command bar

credit: How to interrupt eval?

Monday, January 6, 2014

Solved a different cause of SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

You get SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError) or similar error when trying to request to a https endpoint but no any other people solutions you have found through googling seems to be able to fix it.

What I have found is that those blog posts and stackoverflow answers led me to the wrong direction. The problem is neither Ruby's false nor you misconfigured it. Instead, missing intermediate CA certificate is the cause of the problem.

To verify that you are having the same cause of problem that I had, try running these command and compare the results
$ ruby -rnet/http -e "puts Net::HTTP.get(URI('https://github.com'))"
$ ruby -rnet/http -e "puts Net::HTTP.get(URI('https://your_webserver'))"
If you get back proper result for the first command and get the same error for the second command. That's the same problem I had. If you get errors on both commands, this post won't help solving you problem.

What's happening is that Ruby doesn't have a certificate of the intermediate CA that is used to issue certificate of the server you are requesting to

Compare Certificate chain section of the result of these 2 commands
$ openssl s_client -connect github.com:443
$ openssl s_client -connect your_webserver.com:443
In github's server case, you will get 0 and 1. On the other hand, in your web server case, you probably get only 0. SSL certificate chains section of this nginx documentation page has a good explanation of the chain.

If you own that web server or has an access to it, go fix that and your ssl issue should be resolved. For nginx web server, the fix is described in the previous link.

Good luck.

Config number of file descriptors on Linux

Check file descriptor limit for a process. Vary depending on owner of the process $ cat /proc/{process-id}/limits Check current number of...