Running Shell Commands with Rake

We all owe to Jim Weirich (RIP) - the father of the Rake, developer of many great projects such as builder, RubyKoans (I also contributed to this project lately with some PRs) and Git Immersion. I learned a lot from his talks on YouTube and really admire him.

In this post, I'm going to share a small tip about Rake for his memory and his awesome work.

Running shell commands with Rake is actually pretty simple and straightforward. sh followed by some bash commands is what we are going to be using.

Here is a basic example:

task :gs do
  sh "git status"

After defining the task like shown above, just run the task with:

$ rake gs

and you will see exactly the same output as git status does.

Continue reading
Date: | Tags: rake, ruby and rake, rake examples, shell commands with rake | Categories: tech, ruby

Max Common Array Slice with Ruby

Question: We need a program which takes a comma delimited array of numbers from STDIN and will output the maximum slice of the array which contains no more than two different numbers. Your result should be written to STDOUT.

Example 1:

[1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8] = 10 because the array slice of (0, 9) is the largest slice of the array with no more than two different numbers.

Example 2:

[53, 800, 0, 0, 0, 356, 8988, 1, 1] = 4 because the slice of (1, 4) is the largest slice of the array with no more than two different numbers. The slice (2, 5) would also be valid and would still give a result of 4.

'Max common array slice' (also known as Maximum sub-array problem) is a classical programming test question requiring some knowledge of O(n2) or O(n3). It is often asked in technical interviews in two different ways; a) maximum sum of array elements with no more than two different numbers, b) maximum length of array elements with no more than two different numbers. You can see more about Max Common Array Slice on Codility and Wikipedia.

Continue reading
Date: | Tags: max common array slice, longest array slice ruby, array slice ruby | Categories: tech, ruby

Comparing Ruby Objects (Class Instances) and Equity in Ruby

Recently I needed to compare two different Ruby objects (or Class instances) in a project. I just Googled around a little bit, but found no clue. Then I just continued looking around and came across with a great article written by Alan Skorkin (Skorks).

Here is my sample Money::Cash class that I'm going to be using during this article:

module Money
  class Cash
    attr_accessor :amount, :currency

    def initialize(amount, currency)
      @amount = amount.round(2)
      @currency = currency.upcase

    def convert_to(rate)
      rate = rate.upcase
      current_rate = Money::Rate.all.find { |r| r.code == @currency }

      if current_rate.nil?
        raise TypeError, "#{rate} has not defined for Money::Cash object yet."
      elsif rate == @currency
        return self
        rate_value = current_rate.rates[rate.to_sym]
        return Money::Cash.new(amount * rate_value, rate)

As you might guess, there are two different classes in this project, Cash and Rate. Rate is holding exchange rates for the Cash object, and Cash is holding the amount and the currency of the money. I just followed some basic OOP principles. We already have a convert_to method available for money conversation which makes money conversations based on defined Rates. Let's create some new Cash instances first:

dollar = Money::Cash.new(50, 'USD')
euro = Money::Cash.new(10, 'EUR')
Continue reading

Import huge databases faster in mysql

Recently I needed to import very huge SQL dumps to my local machine. There were 10 different .sql files dumped from Moodle and each of them were sized more than 10GB with approximately 5 million rows. First of all, I tried to import them in a traditional way by using:

mysql -u username -p some_db < database1.sql

However each import request took a very long time, approximately 6-7 hours for each .SQL file. Please also you consider that I conducted this operation in a brand new laptop with Intel i7 CPU, 16GB RAM and 512GB SSD disk. Since I didn't have 60 hours for importing all these dump files, I took a look for potential solutions and found a very effective one.

Continue reading
Date: | Tags: mysql, import big database, import huge database, innodb settings | Categories: tech, database

Markdown preview extension for sublime text 3

If you want to add markdown support to your Sublime Text, you can simply install some plug-ins. To do this, you must install the Package Control first. Then we will install Markdown Preview and Markdown Editing packages.

Continue reading