Wednesday, July 31, 2013

Error: no candidate version available for postgresql-contrib-9.2

This is the error I got when I set up PostgreSQL using chef on ubuntu 13.04:

no candidate version available for postgresql-contrib-9.2

Where does the error come from

So ... it is obvious that there is no Postgresql 9.2 found in all the apt repository the system can see.

It turns out that latest postgresql packages are distributed else where:

Solve in command line

To include the repository and update your packages in command line, do this:

echo "deb precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - | sudo apt-key add -
sudo apt-get update

Solve in chef

file "/etc/apt/sources.list.d/pgdg.list" do
  owner "root"
  group "root"
  mode 00644
  content "deb precise-pgdg main"

execute "include pgdg repository key" do
  command "wget --quiet -O - | sudo apt-key add -"

execute "update apt-get list" do
  command "sudo apt-get update"

Wednesday, July 17, 2013

Ways to define a class in Javascript

Developers who come from famous OO languages such as Java/C++, like myself, may often find defining class in JavaScript confusing. This article aims to provide a very basic "class definition" in JavaScript.

Few notes on JavaScript nature
  • there is no class actually,  usual class concept in Java/C++ can hardly apply
  • everything are basically object literal, simply key-value collection
  • value of an object can be accessed by notation ".key" or "[key]"

Let's begin with a simple example below.

var eddie = { name: "Eddie", age: 25 };
var steven = { name: "Steven", age: 72 };
var benny = { name: "Benny", age: 3 };

1. Object Literal

Above objects eddie, steven, benny are all defined by object literal notation. It doesn't define any class, everything is raw but simple.

  • you can access the fields (properties) in this object like this:
  • prints Eddie
  • steven["age"] prints 72
  • you can add new property dynamically

eddie.gender = 'M';
benny.gender = 'M';

  • pros: simple, intuitive
  • cons: not reusable. Imagine when you want to define function (e.g. getName()) among all persons. >

You need a way to define a class.

2. Constructor Function

2.1 class definition
var Person = function(name, age) { = name;
      this.age = age;
      this.getName = function() {
            return this.age;

  • Person is called constructor function
  • constructor function will implicitly return this to caller 
  • use keyword new to invoke constructor function, as below

2.2 object creation

var eddie = new Person("Eddie", 25);
var steven = new Person("Steven", 72);
var benny = new Person("Benny", 3);

  • new will allocate new memory, and this in constructor function will reference to the new memory
  • "" will print Eddie
  • "benny.getName()" will print Benny

2.3 syntax
  • var Person = function(name, age) { ... } is the equivalent to 
  • function Person(name, age) { ... }

2.4 memory concern
  • note that memory of function getName() is allocated in every instance. To solve this, move the function from constructor function to prototype.
Person.prototype.getName = function() {

This way, only a single copy of getName would exist in memory.
  • "benny.getName()" will still print Benny

2.5 incorrect object creation

var john = Person("John", 11);
  • if "new" is missing (likely by mistake), you are NOT calling constructor function. 
  • "this" will reference to global context, window. Effectively, you are doing the following: = "Kalok";
window.age = 11;

  • "typeof john" will return undefined, because "implicit return this" would not apply without "new"

3. That vs This

To address the issue "this" may refer to global context, here's a possible solution.

var Person = function(name, age) {
      // define an empty object literal
      var that = {};

      // assign to that instead of this = name;
      that.age = age;

      // return that explicit
     return that;

  • "that" is just a dummy word, use "self", "_this" or whatever meaningful to you
  • the disadvantage of this solution is it loses the link to prototype, not good enough in most cases.

4. Self-invoking Constructor

Now we go back to the class definition at the beginning, and check whether this is an instance of the constructor itself.

var Person = function(name, age) {
      if (!this instanceof Person)) {
            return new Person(name, age);
      } = name;
      this.age = age;
      this.getName = function() {
            return this.age;

Thursday, June 6, 2013

Getting your real IP inside Terminal

If you want to check you current public IP for you PC / Mac, sure you know when you visit whereismyip "".

However, what to do if you are inside a Terminal, with just an internal IP?

Just try:
curl -s | sed 's/[a-zA-Z/<> :]//g'
wget -O - -q ; echo

Then you can get your real IP. 

Coding for Fun


Wednesday, May 29, 2013

Clarifying about including json data in javascript file

Just received a beginner question about passing json data to the amchart.
A friend was confused about inline json object creation and loading external json file

All the following two versions work

Version 1:

    <!-- main.html -->
    <script src="js/amcharts.js" type="text/javascript"></script>
    <script src="js/your_formatted_data.js" type="text/javascript"></script>
    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "country";
    <!-- js/your_formatted_data.js -->
    var chartData = [
                      {country: "USA", visits: 1234},
                      {country: "HK", visits: 5678},

Version 2:

    <!-- main.html -->
    <script src="js/amcharts.js" type="text/javascript"></script>
    var chartData = [
                      {country: "USA", visits: 1234},
                      {country: "HK", visits: 5678},
    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "country";

I would suggest the version one because it is easier to maintain and more readable.
You can change the json data inside your_formatted_data.js without changing the main.html

Tuesday, May 28, 2013

Power Charging your Mac for development: Finder, Sublime Text, Zsh

Here are some tips I shared during my General Assembly Ruby On Rails class, to make your Mac better for coding development

1. Finder

1.1 Setting Finder shortcut to a "projects" folder

since we travel a lot between different project, download, application folders during our coding exercises, it is better to organize all our code in a coding folder (I call it "projects" with subfolders named after the project / organization names)
and to speed up the travelling even faster, I add my projects folder shortcut to the Finder favourites.

1.2 Use Path Finder

if you are willing to pay, there is one Finder alternative called "Path Finder" which provides more file management features and more friendly to users familiar with Windows operating system.

2. Personalize Sublime Text

2.1 Sublime Text Command Line shortcut

we always navigates between Terminal (or iTerm) and Finder (or Path Finder)
so, being able to open the current Terminal project in Sublime Text is a huge speedup

2.2 Sublime Text Package Control

There are lots of wonderful plugins being developed by other talented people, setting up Package Control enables you to search and install them with few keystrokes

2.3 Sublime Text Plugin: Sidebar Enhancement

After setting up Package Control, the first plugin I recommend you to install is Sidebar Enhancement
- Open up Command Palette by pressing Apple + Shift + P
- Open up Package Control by typing Install Package and click Enter
- type SidebarEnhancement and click Enter
- after installing successfully (status message at the bottom of Sublime Text), restart Sublime Text completely (Quit and Open, not just close the current window editor)

2.4 Format your code nicely with spaces

use 2 spaces instead of tab

2.5 Remove trailing whitespace after save automatically

3. use Zsh for terminal shell

You can type less characters to execute commands. Zsh is also smart enough to correct your typo and suggest correct commands and arguments

Happy Coding !

Wednesday, April 10, 2013

Managing Rails Environment Variables is the pain in the ass

Managing Rails Environment Variables is the pain in the ass

I have quite a few heroku apps that have sandbox, staging and production environment.
and I have to manage lots of environment variables, e.g. Facebook, AWS, TWILIO, octopus database URL etc.

figaro gem really help:

Environment variables in ONE place with figaro

figaro gives you a yaml file which stores all the environment variables for all environments:


Updating Heroku environment variables in one command

the powerful bit comes when you update your heroku instances environment variables

    rake figaro:heroku

this rake task save you time to run those heroku config:set xxx=yyy --app abc-staging

Not committing this file to public !

like handling other confidential data files in git repository, this file is ignored (included in .gitignore) and never being committed to git repository.
while sharing the file structure without sharing the secrets.
I recommend using a ".sample" approach.

copy the config/application.yml to config/application.yml.sample
and then commit this .sample file to git repository for your teammates to follow

Thanks laser lemon and thanks figaro !

Sunday, March 24, 2013

Pronunciation of "route"

Occasionally, I often heard two different versions for the pronunciation of "route"("root" vs "rowt"), mostly from computer networking context. It got me to google which one is "correct".

Firstly, "root" +1 from Hong Kong Yahoo dictionary...

Then, it appears a typical pronunciation difference between British and American...

While some people suggest that there is a little historical factor... one more party French... hmm...

Interesting responses:
"I say root. Personally, I cringe when I hear someone pronounce it rowt."
"I say root. I hate when people say rowt. They all must die."