Ruby Concurrent Programming

This is a talk that I presented in Case Common learning lunch:

Concurrent programming in Ruby is hard.
Before we start implement things with ‘Thread.new’, we should know the difference between Thread and Process, how to write thread safe code and what framework to use.

Read More...

Bash like a marshall

Bash is a programming language design for interact with user. Which have a very flexible syntax to take command from user.
But in the bottom, it’s just a programming language like any others, it provides variable, function and flow control.

Read More...

Build dropdown list with RxJS

Dropdown list is one of the most common web UI component, but yet one of the most difficult to implement. Recently I was working on a navigation dropdown list with animation. But this time, I implemented it with RxJS, wh

Read More...

Let's build react.js

Recently React is growing to become the de facto standard for web components. And it is also pretty straight forward to learn and use React.

I recommand to start with the official website for documentations, also some examples on github and Todomvc

However, for people already know how to write React, they might still wondering why React is built in a certain way, like React DOM, state, how React update and manipulate the DOM elements.

For those who are curious, this article is going to rebuild React from scratch and create a workable basic React-like framework, by going through the logics in React source code. I hope you can understand how React works after reading this post.

Read More...

Rails abstraction showcase

Abstraction showcase

Maintaining large application is always a pain for rails developer.
Because the MVC scructure encourage developer to write business logics in controller and model.
Therefore when application become bigger, usually it will result in Fat controller or All mighty model that have lots of business logics crumble all over the places.
Extract those logic to another place is one of the solution, but how do we extract and where to extract is another problem.

Read More...

Rails circular dependency

Circular dependency

Recently, I encountered a circular dependency problem that happened in rails,
When the parent model is dependent on child model, it returns Runtime Error for Circular dependency.
However, there is 2 child model that have circular dependency on parent model, but only one will fail on loading:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ./app/models/alpha_product
class AlphaProduct < BaseProduct
end

# ./app/models/base_product.rb
class BaseProduct
PRODUCTS = [AlphaProduct, Product]
# this works
# PRODUCTS = [Product]
end

# ./app/models/product.rb
class Product < BaseProduct
end

# test file:
require 'spec_helper'

it 'does something' do
AlphaProduct.do_things # RuntimeError: Circular dependency detected while autoloading constant AlphaProduct
end

But when we remove the dependency on AlphaProduct, the application works fine. Why is that?

Read More...

Dig into the rails errors

Errors

Rails errors is handling by ActiveModel::Errors, which generate error messages with attribute name and error type.
Recently I am working on some feature related to rails error messages, so it is a good time to go over how the rails errors works.

It’s just a hash

ActiveModel::Errors actually is a wrapper for error messages hash, which include the attribute names and error messages for attributes.

Read More...

Source code odyssey: angular.js injector

Angular.js

Angular.js is a fasnating framework that including a lots of interesting features.
One of the unique feature in Angular.js is dependency injection,
instead of requiring and injecting the dependencies, Angular.js creates a special component to find the dependencies according to parameter names and pass it through the function:

1
2
3
4
5
6
var injector = angular.injector();

injector.invoke(function($http) {
//get http service from service providers
$http.ping('http://angularjs.org');
});

Read More...

Command-t.vim crash on yosemite

The bug

Today I updraged my mac osx to 10.10 yosemite,
However, the command-t.vim plugin does not work correctly with new os.

I saw the command-t.vim could not load the C extension message from vim.

Read More...

Learn jQuery from jqLite

JQuery

Jquery is a great library, it makes manipulating DOM element and browser really simple and easy.
As a web developer, jQuery is our day to day tool. However, sometimes it just too convenience that
we forget how to make web page without it. It is important to go back to see how jQuery handle and wrap
the DOM api and provide the simple interface for us.

JQLite

JQLite

Angular.js comes with a simple compatible implementation of jQuery, called JQLite.
JQLite is used internally for angular.element if user doesn’t include jQuery, as a
replacement for jQuery. It only have 1000 lines of code with lots comments,
So it’s a good starting point to understand how jQuery works.

Read More...