Chapter 3: Part 3

Compare these two implementations of the ubiquitous mergesort algorithm. First we have the Java code:

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class Merge{
  public static <E extends Comparable<? super E>> List<E> mergeSort(List<E> m){
    if(m.size() <= 1) return m;

    int middle = m.size() / 2;
    List<E> left = m.subList(0, middle);
    List<E> right = m.subList(middle, m.size());

    right = mergeSort(right);
    left = mergeSort(left);
    List<E> result = merge(left, right);

    return result;
    }

    public static <E extends Comparable<? super E>> List<E> merge(List<E> left, List<E> right){
      List<E> result = new ArrayList<E>();
      Iterator<E> it1 = left.iterator();
      Iterator<E> it2 = right.iterator();

      E x = it1.next();
      E y = it2.next();
      while (true){
      if(x.compareTo(y) <= 0){
        result.add(x);
      if(it1.hasNext()){
        x = it1.next();
      }else{
        result.add(y);
      while(it2.hasNext()){
        result.add(it2.next());
}
      break;
}
      }else{
        result.add(y);
      if(it2.hasNext()){
        y = it2.next();
      }else{
        result.add(x);
      while (it1.hasNext()){
        result.add(it1.next());
      }
      break;
      }
      }
      }
      return result;
    }
}


I am truly sorry for making you scroll through all of that. I'll understand if you hate me, especially after I show you the Ruby implementation.

def merge_sort(m)
  return m if m.length <= 1

  middle = m.length / 2
  left = merge_sort(m[0...middle])
  right = merge_sort(m[middle..-1])
  merge(left, right)
end

def merge(left, right)
  result = []
  until left.empty? || right.empty?
    result << (left.first <= right.first ? left.shift : right.shift)
  end
  result + left + right
end


Granted, the Java version of this method represents a fully compilable mergesort program since Ruby is an interpreted language, but the difference is still staggering. Plus, most Ruby interpreters compile to either C or Java and are capable of doing most of that heavily lifting automatically. So though there may be a lot more going on under the hood in the Ruby implementation, from the point of view there is less actual code to write. Less code means more time for debugging, refactoring, and getting creative and adventurous with your programs (and maybe a little extra time to soak in the bath).

Rails or Ruby on Rails is a model-view-controller (MVC) framework for web apps written using Ruby. Rails sets up defaults for web pages, web services, and database interaction using widely accepted web standards. Many popular websites use Ruby on Rails, chief among them Twitter. Other popular sites that use Rails are Airbnb, GitHub, Hulu, and Basecamp. Here is a snippet of the Rails sourcecode available for download on GitHub:

def create_record(attributes, raise_error = false)
  record = build_record(attributes)
  yield(record) if block_given?
  saved = record.save
  set_new_record(record)
  raise RecordInvalid.new(record) if !saved && raise_error
  record
end


As you can see, the minimal nature of Ruby code allows you to perform functions with less lines of code.

A model in Rails maps to a table in a database and to a Ruby file. As an example, a class Tweet will be defined in a file named tweet.rb(.rb being the Ruby file extension). The class is then linked to a table named 'tweets' in the database. This is another convention in Ruby that you can choose to ignore. However, if you do choose to adhere to the convention, all those connections mentioned above happen automatically!

Ruby on Rails includes tools to provide a basic development environment 'out-of-the-box'. For example, it can construct models and views needed for a basic website and includes a simple web server and a build system.

There are many, many different tutorials on the Ruby on Rails framework available through a simple erm...something search, and I encourage you to learn more wherever you can! Before your final challenge I have to tell you a secret:

That's right. By starting this tutorial you've shown that you are open minded to new ways of writing your programs. True Code Enlightenment comes from searching for best tools to write your best programs, and now that you've finished your journey with us, you've got a powerful tool in Ruby.

Your final challenge is to join with me in the video below as I explore more of the possible applications of Ruby. Thanks for taking this journey with us!