CopyAndPaste

random programming notes

Ruby Enumerator Inject

Loop over enumerator with a seed, takes output from last round as input.

eg.

array to hash

array = [ 1, 2, 3 ] 
hash = array.inject({}) do |memo, n| 
  memo["key " + n] = "value " + n
end
# hash: { "key 1" => "value 1", "key 2" => "value 2", "key 3" => "value 3" }

Ruby Auto Revivifying Hash

Hash auto revivification allows you to create content on the fly when accessing undefined hash element. Comes in handy when you need to build a nested hash

hash = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }
hash['a']['b'] = 1
hash['a']['c'] = 1
hash['b']['c'] = 1
puts hash.inspect
# "{"a"=>{"b"=>1, "c"=>1}, "b"=>{"c"=>1}}"

Welcome to Jekyll!

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve --watch, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.

Jekyll also offers powerful support for code snippets:

def print_hi(name)
  puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.

Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo. If you have questions, you can ask them on Jekyll’s dedicated Help repository.

Rails Force SSL

Staring rails 3, we shall avoid using ssl_requirement GEM, as the GEM does not work very well on heroku. Sometimes the HTTPS page is redirected and the URL params are lost. To fix this, force using HTTPS in config/routes.rb as follow:

1
2
3
4
  resources :accounts,
            :constraints => { :protocol => 'https' } do 
  # ... 
  end 
You can also use scope.

Javascript Private Variable Through Closure

Defining Javascript class private instance variable Example:

1
2
3
4
5
6
7
function MyClass(a) { // constructor
  this.getA = function() { return a; }
}

MyClass.prototype.myFunc = function() {
  return this.getA() + 1000; // do something with a 
}

Javascript Instance Method This Variable

In Javascript class instance method, you must use the this variable to access instance variables. For instance:

Javascript version
1
return this.foo;

as oppose to in C++:

C++ version
1
return foo;

One way to get around with using this for every instance variable is as follow:

1
2
3
4
5
MyClass.prototype.myFunc = function() {
  with(this) {
    return foo;
  }
}

Javascript Function Properties

Sometimes you need variable in a function to persist across invocations. You can always define global variable to do that. However, that clusters up the namespace.

A better way to do that is to define a function property accessible only to that particular function. Example:

1
2
3
4
5
6
someFunction.counter = 0;

function someFunction() {
  return someFunction.counter;
  // the counter is like a static function variable in C
}

Javascript Function Optional Arguments

Simple example:

1
2
3
4
function foo(a, /* optional */ b) {
  b = b || []; // returns b if b is defined and non-null, 
               // even if b is empty.
}

Restarting Pow

Pow is a zero configuration web server for ruby on rails development. You can download it from pow.cx.

While it’s easy to install, sometimes you will need to restart it to keep things working.

The easiest way is to do this under the rails app folder:

1
~ michael$ touch tmp restart.txt

The command restart the server. What if the pow itself does not respond at all and touching the restart.txt does not work?

You can restart the service as follow:

1
2
~ michael$ launchctl unload -Fw ~/Library/LaunchAgents/cx.pow.powd.plist
~ michael$ launchctl load -Fw ~/Library/LaunchAgents/cx.pow.powd.plist