khoa nguyen

coder by day, an hero by night

Read this first

Middleman and Github pages…bam!

Moving to khoan github pages

View →


ruby fun

ok, let’s get straight into irb because REPL speaks a thousand words…

irb> %w_a b c_
=> ['a', 'b', 'c']

That was cryptic…try this

irb> %i_a b c_
=> [:a, :b, :c]

That save a whole bunch of keystrokes…how about this

irb> %r_a/b/c_
=> /a\/b\/c/

That makes regular expression with forward slash more readable…in fact there’s a whole slew of them %: %q, %x, %

ok, that’s enough fun for now.

View →


tips

$ bundle
Fetching source index from http://rubygems.org/
Fetching source index from https://rubygems.org/

time for coffee break…but then I found this tip on bundler FAQ

$ bundle install --full-index

How much faster is it? Your mileage may vary.

View →


Financial System Inquiry

Time to exercise your citizenship rights. Here is my Financial System Inquiry Submission on Stability - Addressing too-big-to-fail

To David Murray

The enactment of United States of America Glass Steagall Act 1933, here in Australia, is merely a necessary first step to reorganize our economy to become physically productive. Anything other than the passage of Glass Steagall verbatim is less than useless. “Investment” bankers will have to find an alternative career in order for the rest of the economy to persist. With the passage of Glass Steagall, the unpayable debt burden will be greatly reduced; “commercial” banks will be recapitalized, via a national banking scheme, to fund projects that resolve our foreseeable shortages: water, food, electricity, education, healthcare, arts, space defense and exploration; our citizens will reclaim the dignity and pride of honest work, that are...

Continue reading →


nginx pow coexistence

I am a user of

Pow, is a zero-config Rack server for Mac OS X…serving your apps locally in under a minute

and

nginx, [engine x] is an HTTP and reverse proxy server

Now, pow is good for rack app but nothing else. But if nginx sits in front of pow, then awesome stuff happens. Here is the gist of it.

  1. configure nginx so that *.dev is passed to pow
  2. sudo launchctl unload -w /Library/LaunchDaemons/cx.pow.firewall.plist
  3. sudo launchctl load -w /Library/LaunchDaemons/nginx.plist

Now, you can have SSL, or play with php development along with rack apps. Profit!

View →


typing at the speed of thought

Every thousand miles journey begins with the first step.

My coding journey begins with learning how to type, so I
wrote this qwerty tutorial

I was going to extend it for dvorak…but found Learn typing at the speed of thought!

View →


The Sidekiq of 8 processes on Heroku Performance Dyno

So…I had the honor of doing some data migration, which happens to be CPU, and memory intensive.

I called upon Heroku PX Dyno and the Sidekiq with the wrath of one process and 100 threads.

 loading PX with one process and 100 threads
$ heroku run --size PX 'bundle exec sidekiq -e production -c 100'

But was left wanting for yet more power, and PX had some extra room left unexploited. I called upon the rustic shell scripting to load up 8 processes at 100 threads each to work around the ruby GIL.

 loading PX with 8 processes at 100 threads
$ heroku run --size PX 'for i in {1..8}; do bundle exec sidekiq -e production -c 100 & done'

But this is an intensive task, yet it relies on some flaky connection to be maintained. I called upon the one-off PX dyno to run in background.

 loading PX with 8 processes at 100 threads on one-off PX dyno in background
$ heroku run:detached --size PX 'for
...

Continue reading →


ruby defined? operator

Run this on ruby 1.9

require 'delegate'

class Bam < SimpleDelegator
  def bam
    defined? Bam
  end

  def rails
    defined? Rails
  end
end

module Rails; end

bam = Bam.new(Object.new)

 I'm expecting "constant" to be returned but it's something else in fact.
 Can defined? operator be used inside SimpleDelegator?
bam.bam
 => nil
bam.rails
 => nil

 wtf moment
defined? Bam
 => "constant"
defined? Rails
 => "constant"

Something is up inside SimpleDelegator…

class Normal
  def bam
    defined? Normal
  end
end

Normal.new.bam
 => "constant"

Mon_Ouie: Moduleconst_defined?

Mon_Ouie: You can also just explicitly start from top-level using ::SomeConstant

Thanks Mon_Ouie on IRCruby-lang. Ok, let’s try that again…

class Bam < SimpleDelegator
  def bam
    defined? ::Bam
  end

  def rails
    Module.const_defined? :Rails
  end
end

bam = Bam.new(Object.new)

bam.bam
 => "constant"
...

Continue reading →


IRC#ruby-lang

Ah good old Internet Relay Chat, haven’t used that in like ever. But found out that it’s the best source of help one could get on ruby.

Setup

I got a Mac, so I roll with Colloquy. Then connect to irc.freenode.net server and join ruby-lang channel. At first I kept getting warning about not being able to post messages. Turns out you must register with freenode server. So here it is, just type verbatim:

/msg nickserv help register

Follow instruction, check email then punch in:

/msg NickServ VERIFY REGISTER khoa **********

Select your password and all good to go.

And I learned that :: is called scope resolution operator from SteveKlanik.

View →


gotcha: ruby += operator

Looks like there’s no += operators. My guess is compiler is probably converting a += b expressions into a = a + b, or more verbosely a.send(:=, a.send(:+, b)).

Here’s some quick experimentation:

irb> 0.send("+=", 1)
 => NoMethodError: undefined method `+=' for 0:Fixnum
irb> 0 += 1
 => SyntaxError: (irb):1: syntax error, unexpected tOP_ASGN, expecting $end

View →