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

http://www.sublimetext.com/docs/2/osx_command_line.html

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

http://wbond.net/sublime_packages/package_control/installation

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
http://www.sublimetext.com/docs/2/indentation.html

2.5 Remove trailing whitespace after save automatically


http://blog.nategood.com/sublime-text-strip-whitespace-save




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

https://github.com/robbyrussell/oh-my-zsh



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:
https://github.com/laserlemon/figaro


Environment variables in ONE place with figaro


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

    config/application.yml


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...
http://hk.dictionary.yahoo.com/dictionary?p=route

Then, it appears a typical pronunciation difference between British and American...
http://www.howjsay.com/index.php?word=route&submit=Submit

While some people suggest that there is a little historical factor... one more party French... hmm...
http://ph.answers.yahoo.com/question/index?qid=20100511082156AAuZ6VL

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."

Conclusion: 

Monday, March 18, 2013

iphone Personal Hotspot suddenly disappeared and I fixed it accidentally

Suddenly Gone

I had been using the Personal Hotspot on my iPhone since day one. But just now, the Personal Hotspot was gone ! disappeared ! missing !


I suspected that it was some changes on firmware update ... but the version stayed the same. version 6.1.2

I tried the following:
- Switch it off and on again
- "Settings" > "General" > "Reset" > "Reset Network Settings"

didn't work.

Fix

then at last I found the settings in 
- "Settings" > "General" > "Cellular" > "Personal Hotspot"
after I switched it on, the "Settings" > "Personal Hotspot" appeared again.

WEIRD!

Sunday, March 3, 2013

hotcocoa/mappings/appkit/box.rb:3:in `block': uninitialized constant CGRectZero (NameError)

Where does this error coming from?

So... after playing with RubyMotion, I start picking up MacRuby !
my goal was to build up some desktop utility apps and interactive desktop application taking various of gesture capturing input devices.

First, I tried out the hello world in XCode and it works fine. Then I tried writing Macruby apps without XCode. After I install macruby on rvm and run the Macruby apps generated by hotcoca, I saw this error:

hotcocoa/mappings/appkit/box.rb:3:in `block': uninitialized constant CGRectZero (NameError)


Troubleshooting in irb ?

Spent quite a lot of time checking documentation and setup instructions. When I tested the missing dependencies in irb, I found out that only those related to Cocoa UI are missing. So I load the cocoa framework explicitly and it works:

framework 'CocoaGraphic'
CRRectZero

Solve that in hotcocoa project

inside my application.rb, I load the framework explicitly before loading the hotcocoa gem:

begin
  # for OSX 10.8
  framework 'CoreGraphics'
rescue
end
framework 'Cocoa'

require 'rubygems' # disable this for a deployed application
require 'hotcocoa'



Tuesday, January 29, 2013

Refactoring "if nil?"

Very often we have checking against nil value like this:

if products.nil?
  products.first.total_amount
else
  0.0
end


There is a cleaner way to achieve the same result:

products.first.try(:total_amount) || 0.0