It's About Time

The date and time is important for an application which is used in different timezones. For example, consider what happens if a user from India enters some data with its own local timestamp at 5:30 a.m and a user from U.S.A wants to see the data. If the timezone is not changed then time which is entered is in the future for the user in U.S.A.

Before getting into actual coding to handle date and time, let us understand the time terminology:

  • UTC - The reference time.
  • Standard Time - The local time without a daylight saving time offset. For example, in India, standard time(IST) is UTC+5:00.
  • Daylight Saving Time(DST) - The local time with a daylight saving time offset. This offset is typically one hour, but not always. It is typically used in most countries away from the equator. In India, daylight saving time is UTC+05:30

Ruby provides two special Libraries to handle time and date:

  1. Time: It is usually sufficient for everyday programming needs because the class uses the time provided by the operating system. It may not be able to display dates before the year 1970 and after the year 2038 and is based on library time.
  2. Date: It consists of two classes Date and DateTime

There are many solutions to managing the timezones and one of them is to store the time in a standard format like UTC and converting it to the user's time-zone.

The code given below can display all possible date-time formats:

Praveen Shukla is an engineer at C42 Engineering. If you liked this post, please consider...


Crystal for rubyists

For last few weeks I have been exploring a relatively new language (about 2 years old) Crystal (hasn’t hit 1.0 as of today). Crystal is inspired by Ruby and it has Ruby like syntax that we all love. It is a statically typed language, so you won’t have runtime bugs since the compiler will catch type related bugs during compilation. 

Crystal is very efficient in compiling code to native code which can be distributed as binary.

Crystal has a modern standard library which has implementations of markdown and websockets baked into it. 

And while we are in introduction territory, it’s important to know that Crystal is self-hosted. That means 

The Crystal is 100% written in Crystal. 

Crystal Basics

  • Everything is a object just like Ruby.
  • There are two types of objects, Values and References.
  • Value objects includes Number, Bool, Nil , Char, Struct, Tuple and Pointer.
  • Rest all objects are References including String, Array and Hash.
  • Value objects are passed by value and Reference objects are passed by reference.

Now that we’re over with the introductions, let’s dig a little deep into language core constructs and concepts.

Global Type Inference

Crystal determines the type of objects and return types of functions using mechanism called global type inference. The process includes traversing the whole AST and assigning type to each node based on type expression it represents. 

Let’s catch up on curious case of Union Types. 

In above code snippet the type of variable name is determined by type inference. The AST node representing first conditional expression will have type of String associated with it while the AST node representing second expression will have Boolean type associated with it. The variable name hence will have a union type String | Boolean associated with it.

Let’s take another example of Union Type.

foo = ["foo", false, 1, "bar"] typeof(foo) #=> Array(String | Int32 | Bool)

In above code variable foo will have type Array(String | Int32 | Bool).

Moving on.

Method Overloading

Method overloading has three different criteria.

  • Number of arguments.
  • Types of arguments.
  • If methods accepts a block or not.

Let’s take an example of addition.

The last line in above code snippet will throw below error.

no overload matches ‘String#+’ with types Int32
Overloads are:
 — String#+(other : self)
 — String#+(char : Char)

During the compile time Crystal determines all the possible overloads and throws errors if type of arguments supplied doesn’t match the possible argument types available in overloads. 

You can also pass type of arguments in methods like this

Let’s try to fix the String and Number overloads situation.

This works perfectly fine since now we have a overload that can handle argument of type String and Number.

Moving right along!


Structs are 

  • stack allocated
  • passed by value since Struct inherits from Value.

while class is heap allocated and passed by reference since it inherits from Reference. These features makes struct more suitable for defining a type in. For example if you want to build a vehicle, the implementation would look something like this.

This is just a very brief overview of crystal. This is missing many concepts like macros, generics, pointers, slice and much more, which I’ll try to cover in next blog.

You can help

  • by contributing to crystal.
  • by funding crystal's development and help it become production ready at bountysource.

Vijay Dhama is an engineer at C42 Engineering. If you liked this post, please consider...



Setting up certain tasks to be run as a daemon can be done using Rake. The actual logic to be executed can be invoked by a Rake task. That Rake task can then be daemonized using the start-stop-daemon command. If required, the PID of that Rake task can be stored in a PID file which can be used by a monitoring service like Monit.

Sample Rake task that executes some business logic in a Rails app

If the command to execute the rake task and any environment variables are stored in a startup script, then the startup script can be executed to create the daemon. The command to start the daemon will be something like :
"/sbin/start-stop-daemon -S --quiet --pidfile #{path to file to store the PID in} --exec #{path to startup script} -b"
The command to stop the daemon will be :
"/sbin/start-stop-daemon -K --pidfile #{path to file to store the PID in}"

Shishir is an engineer at C42 Engineering. If you liked this post, please consider...


When to use the command pattern

Given the problem statement

Design a system to emulate cricket. A captain should be giving some commands and depending on the command players are supposed to act on it.

It might at first look like the Command Pattern to novice programmers who just discovered the Command Pattern. Today I will recount an episode of where I had done the same. The impatience combined with carelessness made me think that this problem could be easily solved using the Command Pattern. The classes I built were as follows.

Now using the Executer class we can make the Bowler or Batsman execute according to his/her position in the team. This is called a Command Pattern and is mainly used at situations like this where you have to execute the commands in a particular order. But I can implement the same using the Strategy Pattern as well. For example, the left orthodox medium pace bowler can be the base class from which Glen Mcgrath is inherited from. Using different such types, we can implement the Strategy Pattern.

So then why use a Command Pattern?

This article about Command Pattern says -

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Here in the Cricket class we need not have logging or undoing operation. Imagine a batsman going to the boundary line and bringing the ball back and giving it back to the bowler!

So where exactly are command patterns not an overkill?

Examples of Command Pattern:

  1. Computer games where each command object is sent over the network to be executed on the server machine.
  2. Any application which uses undo/redo operations.
  3. A simple application using a command pattern would be a calculator.

It is also necessary to understand that it is always better to evolve a pattern out of code instead of imposing a known pattern on it. Design is always an art and in art it is always good to be a Picasso rather than a copycat Picasso.

Anugrah is an engineer at C42 Engineering. If you liked this post, please consider...

  • following @Anunymouze on twitter
  • subscribing to this blog

SETTING UP STAGING ON A Vagrant virtual machine

To setup staging you require.

  • Vagrant
  • Virtual Box

Vagrant allows you to spin up as many Linux virtual boxes as you want, whenever you need them. It isolates dependencies and their configuration in a single file called “Vagrantfile”. So instead of installing your dependencies one at a time you can just create a Vagrantfile and Vagrant handles the rest of the setup.

Seting up vagrant

Note: make sure that you have 1-2 GB of free RAM as Vagrant runs a full OS in the virtual machine on your system.

  1. Install Virtual Box in your machine.
  2. Install Vagrant in your machine.
  3. Pick the box that you want to spin up at
  4. Execute vagrant box add “name_of_the_box”.
  5. Go to the project folder.
  6. Execute vagrant init.
  7. Execute vagrant up.
  8. Execute vagrant ssh.

So just like that you have a virtual Linux box running on your machine.

Configuring The Vagrantfile

This file is the heart and soul of your vagrant setup and you can use it to normalise and automate the process of setting up your virtual machine. A lot of stuff like provisioning, networking and syncing application folder can be automated directly from this file.

Setting Up The Networking: "private_network", ip: ""

This creates a private network. You also get options for port forwarding or connecting to public network.

Setting The Synced folders:

It enables Vagrant to sync folders on the host machine to the virtual machine.

config.vm.synced_folder "./", "/home/vagrant/app"

The first parameter is a path to a directory on the host machine. If the path is relative, it is relative to the project root. The second parameter must be an absolute path of where to share the folder within the virtual machine.

Provisioning The Virtual Machine:

Provisioner in Vagrant allows you to automatically install software, alter configurations, and more on the machines as part of the vagrant up process instead of using ssh and installing all software manually, each time.

config.vm.provision "shell", privileged: false, inline: <<-SHELL

The script will be run in the shell of your virtual machine when you execute vagrant up.

Prashant is an engineer at C42 Engineering. If you liked this post, please consider...

  • following @TheData_Ninja on twitter
  • subscribing to this blog