There are tons of text editors out there, and every single developer has its own preference: Sublime Text, TextMate, Notepad++, GEdit, Emacs… same thing happens with IDEs, such as Eclipse, IntelliJ, XCode, RubyMine, Android Studio, Visual Studio… every single one has its own pros and cons; however, if there’s a text editor that is the preference of the so-called “hardcore programmers”, that is Vim.

Vim is a free and open source text editor based on Vi; you will probably have it installed on your computer, so go ahead, open your terminal and type vim test.txt; your terminal will turn into a full and rich text editor, which is awesome. There are great tutorials out there to learn how Vim works (if I have to choose one, this interactive tutorial is my favorite) that will show you how to get the best form Vim.

While I’ve used Vim in the past for minor things (squash my git commits, do a simple edition on a file…), it has been this last month when I’ve started to use Vim intensively: every single day I have to work on a remote instance of a Debian machine that contains a Rails application. In the past, when I had to work with Rails, I used Sublime Text or RubyMine, but never Vim before, and I was very happy with results.

Vim is totally customizable, and you can add as many plugins and fancy stuff as you can imagine, which converts Vim into a great development tool (as you can see in the following video):

However, Vim is not “my tool”. Maybe I’m used to “the graphical” text editors, maybe I don’t want to learn a whole new set of shortcuts (yeah, when I press Cmd + F I want to do a search, and want I type something I want to write something… yeah, I’m looking at you, “INSERT MODE”) or maybe, I’m just human and variety is just the spice of life, and I am that kind of developer who doesn’t like to use Vim for everything.

Just to illustrate how “hard” is to me to get used to Vim, I have found this little joke to be very appropiate:

How do you generate a random string? Put a first year CS student in front of Vim and ask him to save and exit.

The only thing that I can do is to learn as many Vim features as I can, but looks like I will continue using my mouse, using the arrow keys… and using any other text editor or IDE when I can ;)

Recently, I joined the “O’Reilly Reviewers” program, and the first book that fell into my hands was Learning Android, by Marko Gargenta and Masumi Nakamura.

The first part of the book is a brief introduction to the Java programming language, used to develop for Android. After this introduction, you will start the creation of a Twitter-like application called Yamba that covers the core concepts of every Android application: activities, fragments, services, content providers, async tasks, etc.

The book follows always the same structure: it will introduce you the concept that you are about to apply in your app, and right after that, you will introduce your new changes in the program; with all the new code in place, you will see a more detailed explanation of what you did. In my opinion, this is a great approach to fix the concepts in your mind, as you will understand every step that you’ve taken in a very simple way. However, you will find numerous errata in the code, and this could make you feel that you’re doing something wrong when that’s not the case.

Also, to start the development, you will need to use a library called “yambaclientlib.jar”, and the book provides a URL that, instead of pointing to the JAR file, it will take you to the Github repository of the library; what I had to do was to clone this respository, and inside the libs folder you will find the JAR file that you need in your application.

Finally, I was expecting to use Android Studio in this book, since Android Studio was released in the Google I/O 2013 and it’s the new IDE recommended by Google, but instead, you will use Eclipse and the ADT plugin.

Overall, I enjoyed reading this book, and I liked how clear and well explained were the concepts of Android development, but at the same time, it was frustrating to see all the those errors in the code; this will cause some beginner students to abandon the book.

I review for the O'Reilly Reader Review Program

A new version of Ruby (concretely, Ruby 2.1.0) was released on December 25th, 2013. It’s time to go through all the new features!

First of all, let’s make sure we’re on the same page; in your terminal, run the following command and make sure you have the version 2.1.0 installed in your computer:

$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]

New features

The most relevant changes introduced in Ruby 2.1.0 are: (release notes here)

  1. def's return value: no more void methods
  2. Rational and Complex Literals
  3. Refinements
  4. New Exception#cause
  5. New Garbage Collector

def's return value: no more void methods

In previous versions of Ruby, the return value of a method definition was always nil. Here we have an example:

$ irb
irb(main):001:0> RUBY_VERSION
=> "2.0.0"
irb(main):002:0> def test_method
irb(main):003:1> end
=> nil

However, in Ruby 2.1.0, method definitions return always a symbol that matches the name of the method:

$ irb
irb(main):001:0> RUBY_VERSION
=> "2.1.0"
irb(main):002:0> def test_method
irb(main):003:1> end
=> :test_method

This feature is very useful in those classes that are longer than usual. In old Ruby implementations, we used to have the private keyword followed by all the private methods, but in long classes this keyword was easy to miss out. Here we have an example of an old implementation:

module TestModule
  def public_test_method
  end

  private
    def private_test_method
    end
end

Knowing that private can accept a symbol, in Ruby 2.1.0 we can combine the method and its visibility:

module TestModule
  def public_test_method
  end

  private def private_test_method_1
  end

  private def private_test_method_2
  end
end

Rational and Complex Literals

In Ruby 2.1.0, the suffix i for complex numbers has been introduced:

$ irb
irb(main):001:0> (5+3i) + (2+2i)
=> (7+5i)

Working with rationals is also a better experience; the suffix r has been introduced as a helper for the Rational class.

$ irb
irb(main):001:0> (3/5r) + (2/3r)
=> (19/15)

Refinements

In Ruby, all the classes are open; this means that we can modify classes without any restriction. Refinements are an elegant solution to apply our modifications only under certain scopes:

module URLBuilder
  refine String do
    def url_builder
      prepend("http://www.jcocaramos.com/").downcase.split.join("-")
    end
  end
end

class Article
  using URLBuilder

  def initialize(title)
    @title = title
  end

  def build_article_url
    @title.url_builder
  end
end

As a result, when we execute this code, we will obtain:

irb(main):01:0> article = Article.new("My First Article")
=> #<Article:0x007f89f89d3080 @title="My First Article">
irb(main):02:0> article.build_article_url
=> "http://www.jcocaramos.com/my-first-article"

We can verify that String#url_builder only exists in the scope of the Article class so easily:

irb(main):01:0> "My First Article".url_builder
NoMethodError: undefined method `url_builder' for "My First Article":String
  from (irb):1
  from /Users/jorgecoca/.rbenv/versions/2.1.0/bin/irb:11:in `<main>'

new Exception#cause

Often when a lower-level API raises an exception, we would like to re-raise a different exception specific to our API or library. Currently in Ruby, only our new exception is ever seen by users; the original exception is lost forever, unless the user decides to dig around our library and log it. We need a way to have an exception carry a “cause” along with it.

This explanation can be found here.

In Ruby 2.1.0, now we can do something like this:

class CustomExceptionClass
  def method_with_exception
    cause = nil
    begin
      raise "2nd message" # RuntimeError
    rescue => e
      raise StandardError, "1st message"
    end
  end
end

begin 
  CustomExceptionClass.new.method_with_exception
rescue Exception => e
  puts "Caught exception: #{e.message} [#{e.class}]"
  puts "Caused by: #{e.cause.message} [#{e.cause.class}]"
end

The output of this code will be:

Caught exception: 1st message [StandardError]
Caused by: 2nd message [RuntimeError]

New Garbage Collector

Ruby 2.1.0 incorporates a new Generational Garbage Collector that replaces the old implementation of the stop-the-world mark and sweep algorithm.

The key idea and observation is that objects that are most recently created often die young. Therefore, we can split objects into young and old based on whether they survive a garbage collection run. This way, the garbage collector can concentrate on freeing up memory on the young generation.

Ruby has been always accused of putting performance in a secondary place, but with these changes we can see that the Ruby core team takes performance seriously.

Wow! 2013 is almost over, and looks like it was yesterday when we were celebrating the beginning of the 2013. It has been an amazing year, and I’d like to keep track of all the good memories; it’s gonna be hard to have a 2014 as good as 2013, but I’ll do my best ;)

Let’s start the review!

Technology

Android

Without any doubt, dominating Android has been my biggest achievement in 2013. It’s true that I had some experience from college (in 2011), and Java is not a new language for me, but so many things changed in two years that felt like learning a whole new thing (Hello Fragments! xD).

In 2013, two new versions were released: Android 4.3 (Jelly Bean) and Android 4.4 (KitKat) with great new features. As for developers, the new Android Studio has been the best gift for us: a new IDE based on IntelliJ the make our life easier.

Android

Also, in 2013, two new projects have kept my attention: Espresso and Chromecast. Espresso is a new framework for testing Android UI, and Chromecast… well, Chromecast is simply a great device to stream your media files to your TV (and it has been a perfect Christmas gift!).

Chromecast

Ruby on Rails

Rails 4 was released in summer, and it included a lot of new features. Although the first half of the year I worked on a project based on Rails 3.2, I’ve been following closely all the improvements and changes included in this new version. That’s why I prepared this small website explaining how to use all the new stuff.

Rails is, by far, my favorite web framework: simple, elegant, and it’s always one step ahead! And with the new improvements, it’s the perfect choice for your web projects. If you are new to Rails, the best tool to learn is Learn Ruby on Rails, by Michael Hartl.

Travels

St. Louis, MO | Farnsworth House, IL

During Memorial Weekend, we had the opportunity to spend a couple of days in St. Louis, MO. It was a great time in a city that we were visiting for the first time. Also, we visited the Farnsworh House, in Illinois. This house was designed by Mies Van der Rohe, the same architect that designed the Illinois Institute of Technology.

stlouis2 stlouis1 farnsworth

Park City Mountain & Salt Lake City, UT

Skiing in November? Yeah! In Park City Mountain it’s possible! We spent the Thanksgiving holidays in the incredible resort, where we met fantastic people. 100% recommendable!

skiutah DSCN0850 DSCN0867

Madrid, Spain

The last time I visited my country was in 2011… so many things have changed, but the city still maintains its beauty and charm.

Music

My Telecaster :)

My new “best” friend!

Squier Telecaster

Swedish House Mafia

I’m not a big fan of electronic music, but this group of DJ’s is the exception. It was a great concert!

Glen Hansard

Glen Hansard, Lisa Hannigan, Millenium Park… and free! Best plan ever!

glenhansard

Wavefront

Three days of party in Chicago in the beach, surrounded by the “spanish crew”… wow! What a great summer!

spanishwavefront

Riot Fest

Pixies, Blondie, Rancid, Fall Out Boy, Blink 182, Bad Religion… I love Rock and Roll!

riotfest

Spotify

This is the result of thousands of hours listening to music.

spotify

Family & Friends!

Happy 2014

Raspberry Pi and XBMC

In previous articles I explained how to install RaspBMC in your Raspberry Pi, and how to get the best performance of your RaspBMC; today I’ll explain how to connect to your RaspBMC via FTP and/or SSH.

FTP

FTP (File Transfer Protocol) is a network protocol used to transfer files between different machines. You will need to know the IP address of your Raspberry Pi and an FTP client in your computer.

Get your Raspberry Pi’s IP address

In the RaspBMC, go to Settings->System Information->Summary. You’ll see a field with the IP address.

FTP client

There are tons of FTP clients, but if I have to choose, I prefer FileZilla. It is a free option and multi-platform, so you can install it easily on Mac, Windows or Linux.

Once FileZilla is downloaded and installed in your computer, open it and go to File->Site Manager. Create a new site with the following information:

  • Site name: e.g. Raspberry Pi
  • Host: your Raspberry Pi's IP address
  • Port: leave it blank
  • Protocol: FTP - File Transfer Protocol
  • Encryption: Use plain FTP
  • Logon Type: Normal
  • User: pi
  • Password: raspberry
  • Account: leave it blank
  • Comments: write whatever you want or leave it blank

FileZilla Site Manager

Once you’ve completed all the information, click on connect and you will see the FileZilla panel: the left half is your computer, and the right half is the Raspberry Pi’s file system. Now you can drag & drop files from one computer to the other with no effort.

SSH

SSH (Secure SHell) is a secure way to control computers remotely. Accessing via SSH to your Raspberry Pi’s will allow you to run UNIX commands having to connect physically to the device. In this case, you’ll need to know again the IP address of the device.

Linux/Mac

Open the terminal and connect to your Raspberry Pi’s using the following command:

ssh pi@your-raspberry-pi-IP-address

Now you’ll need to enter the password; it is the same password that you used before to connect via FTP: raspberry.

After these easy steps, now you’ll have control of the Raspberry Pi’s command line.

Windows

The most famous SSH client for Windows is Putty. Download it and open it (it doesn’t require installation).

Putty

You’ll need this information:

  • Host: your Raspberry Pi's IP address
  • Username: pi
  • Password: raspberry

Once you’ve entered this information, click on Open and you’ll have full access to the Raspberry Pi’s command line.

In the next article I’ll explain how to manage your media library, and how to set a torrent server to manage your downloads.