programming

Getting started with Ruby on Rails

Ruby on Rails logo

Recently I decided to learn Ruby on Rails. When you start learning a new technology you are always looking for the best available materials (to learn as efficient as possible). I did the same (using Google and StackOverflow). Fortunately I found The Best Way to Learn Ruby on Rails and I followed the recommended steps. With small modifications (extensions).

First of all I extended first step. Instead of just going through “Try Ruby” exercises I also read first chapter of Seven Languages in Seven Weeks (which is about Ruby). It was very good move, because this book is written in the way to show the flavors of language by comparison with others. I am a .NET guy, who was coding in PHP, C++, Java and Python before. Because of that I was more interested in the differences between Ruby and these languages, than in programming from ground up. I also reviewed (not read) Humble Little Ruby Book. It is a little bit more deep, but it gives you solid Ruby basics.

I was working with Rails on Windows and on Mac. Installing on Windows is very easy when you use RubyInstaller. There is also version for Mac. However on Mac you can also install Rails using RVM. In that case I recommend you installation screncast by Michael Hartl. On Windows I used RubyInstaller, but on Mac I took advantage of Michael Hartl’s screencast. Additionally you may also need SQLite Database Browser to browse your database easily. I did not know about it at the beginning and I was using rails dbconsole. Browsing with SQLite Database Browser is much more comfortable!

When I had Rails installed I went through Jeffrey’s Introduction to Rails. During that I tried to follow him, by writing code on my machine, but many times he was too fast. I needed to pausing video very often and even scrolling back to see written command (he was changing screens to quickly). Anyway it was very nice introduction and I recommend it. But you can skip rewriting and trying code he is writing. Just watch it to get a flavor of Rails.

After that I went through Rails for Zombies tutorials. I was very lucky, because Code School had promotion in May 18-19, and they provided Rails for Zombies 2 for free in these days. These tutorials are very solid. The exercises force you to learn by typing, because you cannot proceed to next level, until you do not finish all tasks.

Agile Web Development with Rails cover

With all basics gained as described above I started a book: Agile Web Development with Rails. I really like this book. It has 3 parts:

  • Getting started (Rails installation, create first app, quick Rails architecture overview)
  • Building application (tutorial)
  • Rails in Depth

The longest part of the book is the tutorial(2nd). Through this part you are creating an complete application exploring different Rails features. Unfortunately this book is a little bit outdated. Authors use ruby version 1.8.7 and Rails 3.0.0. I installed most recent versions: ruby 1.9.3p392 and Rails 3.2.13. Sometimes you need to fix the code (e.g. Chapter 11 – Task F: Add a Dash of Ajax). During that I found Ruby on Rails documentation very useful.

The last part is going deep into Rails. I really recommend this part! It is not only about Rails, but also about Web Applications and MVC architecture in general: how browser works, how requests are handled by Rails app, session, cookies etc.

When I was in the middle of book I was a little bit angry (because it is outdated) and I switched to Ruby on Rails tutorial by Michael Hartl, which is strongly recommended on StackOverflow. Well…guys at SO are right. This is really good piece of knowledge not only about Rails, but also about using git, css, Bootstrap and Web Development in general. I really enjoyed it! If you do not want to buy videos, you can just read the free book (it is the same content as in videos and more). There are also nice videos describing advanced setup for Rails development on Mac and SublimeText configuration for Rails. Actually this tutorial covers Rails development from A to Z.

As a summary to the book and Michael Hartl’s tutorial I reviewed Rails Guides. It is a nice overview for most important rails features. Can be also used as a reference. Some of the RailsCasts are also useful.

I wanted to try a few different tutorials/books to see different approaches. E.g. Michael Hartl use rspec for unit tests, but the authors of Agile Web Development with Rails are using rails testing framework.

My adventure with Ruby (on Rails) lasts almost two months. Now I can admit that ROR is a very powerful and developer friendly framework. It contains many features, which are already grabbed by ASP.NET (e.g. migrations, bundling). What was surprising for me, you do not need IDE to develop Rails apps. I use SublimeText2 (awesome editor!) and it is really enough. Some Rails developers use VIM or Emacs. Of course there are some IDEs such as RubyMine or Aptana Studio. I tried both. RubyMine seems to be pretty cool…but I stick with SublimeText. Additionally, during Rails development you spend lot of time with console (to create/run/undo migrations, create models/controllers, run tests etc.).

If you want to start Rails development, my recommended steps are:

You might also find these tools/resources useful:

What I like in Ruby on Rails? The syntax, convention over configuration and lots of implemented features in the framework layer. Moreover: Rails are just cool.


Build 2013

June was a month of conferences for .NET developers! We had Tech Ed North America, Norwegian Developers Conference, Tech Ed Europe and bunch more, but the biggest one was Microsoft Build Developer Conference.

All videos are live and available for free on Channel 9. Both Keynotes are worth to see, but for .NET people second day’s keynote would be more interesting. I also recommend Scott Hanselman‘s session What’s New in ASP.NET and Visual Studio 2013. Scott shows lots of nice new features like:

  • One ASP.NET
  • Bootstrap as default template in ASP.NET project
  • Multiple default browsers in Visual Studio
  • Real-Time website refreshing between Visual Studio and Browser (without page reloading)
  • New ninja snippets
  • and much, much more

There is also very solid session Introduction to Node.js on Windows Azure by Tomasz Janczuk. I really enjoyed this session. Tomasz shows Node.js basics, and how to it on Windows Azure in very simple and clear way.

Third must see (if you are .NET dev) is Visual Studio 2013 for Web Developers: Deep Dive by Mads Kristensen. Mads shows hot new features in VS editors (HTML, CSS, JavaScript) and WebEssentials add-on.

Other interesting stuff:


Windows 8.1 Preview and Visual Studio 2013 Preview

At the build conference (June 26-28, 2013) Microsoft announced Windows 8.1 Preview and Visual Studio 2013 Preview. I installed them on my Virtual Machine. Just in case, to protect my system from some unexpected features 🙂

In case of Windows 8.1 there are no big changes. Only some small, useful improvements. I like ‘search all’, which enables you to search within apps, settings and files in the same time. However I am still using Search Everything, because it’s faster and more effective. It’s also cool to have the Start button, which brings you to the metro desktop, but again – no big deal (I was ok with WIN button). You can find list of improvements/changes here and here.

The new Visual Studio is more interesting. The One ASP.NET idea is applied. When you create new project, there are only one template: ‘ASP.NET Web Application’. Then in second step, you can choose which types of applications you want to include into it.

Visual Studio 2013 One ASP.NETVisual Studio 2013 One ASP.NET templates

There is MVC 5 (Preview) in it, along with various scaffolding options. You can e.g. scaffold just edit action.

Great feature for web developers: you can open page in multiple web browsers and then refresh them all from Visual Studio (e.g. after change in code).

The editors experience is improved. You can have code map in the scroll bar. HTML editor is rewritten from scratch. Short list of my favorite features:

  • new code snippets (in HTML document try: ‘div.myClass*4>lorem’ and click TAB)
  • intellisense in web.config
  • ALT + UP/DOWN – move code line up or down
  • ALT + 1/2 – extends text selection to level up or down
  • ALT+SHIFT+W – allows to surround selected text with new tag
  • ALT+V – voice commands (which shows shortcuts), yes we can speak to Visual Studio!
  • JavaScript frameworks intellisense (e.g. AngularJS)

But the greatest news is: WebEssentials2013 are now Open Source on github. Everyone can contribute. The policy is to add experimental features to WebEssentials and then move the hottest to Visual Studio (once they are tested). To see all, new, hot features watch Mads Kristensen’s talk at build 2013.

Another cool thing is possibility to ‘sign in’ in the Visual Studio. Once you sign in using your Microsoft account, you can synchronize settings across your devices. Now, it is enough to customize you Visual Studio only once.

There is much more new features. You can find them here and here.


The future of Mobile Apps

I think that in next 5 years Web Mobile apps will be more popular than classic Mobile apps we are using today.

Me, June 28, 2013

That is what happend in case of PCs. 10 years ago we were installing apps instead of just use them in the browser. Now we can edit Word documents, play games and even use IDE in Web Browser. I am not saying that it will be no classic Mobile apps at all, but e.g. apps like Calendar, gmail, Evernote, OneNote or games should be easilly accessible through Mobile Web Browser. The advantage of that would be lack of necessity to install bunch of apps.

What that means for developers? People who are currently working as Mobile Developers will need to learn Web Development. People who are currently working as Web Developers will need to learn Mobile Development. Additionally, future developers will not necessary need to know all different platforms (iOS, Android, WP), because they will be able to create apps in HTML5 and JavaScript (which should be well supported and compatible with Mobile Web Browsers in next 5 years).

This is my prediction. We’ll see what happens after 5 years.


Python jump start

In my current job (Research Assistant at SAnToS lab) I had to learn Python. I was very happy because of that. It gaves me an opportunity to get familiar with one of the most popular programming languages nowadays.

I was very lucky to find awesome Google’s Python Class by Nick Parlante. It is great! If you want to start programming with Python or just learn it for fun, start with this tutorial!

As a supplement to above course you can read some more detailed tutorial. I went through two: Learn Python The Hard Way and Tutorial from Python documentation. However if you already know some other programming language(s), your should learn during development. Python contains almost all common features of programming languages such as if/else, loops, exceptions, functions, classes etc. I said ‘almost’, because there is e.g. no switch instruction. However to check things like that there is very well written documentation. It contains a lot of examples. The main difference between other popular languages like (C, C# or Java) and Python is that there is no semicolons. We use colons and indentation instead.

if number > 0:
  print "This is natural number."
else:
  print "This is not natural number."

Python is dynamic, strongly typed programming language. It means type checking occurs during the run time, instead of compilation time. Programming in Python is a real pleasure. Sometimes you can explicitly put your mind into the code. That is because of high level of abstraction. E.g. file operations are so simple and intuitive. You do not need to remember any StreamReaders or BufferedReaders and bunch of functions for simple I/O operations. Below example reads content of file.

f = open('file.txt')
f.read()
f.close()

Cool feature is the possibility to call functions explicitly on string. Like that:

"jakub".upper()

There is a lot of implemented (widely used) functions in Python. As a comparison, let’s see how to reverse words in a sentence using C, Java and Python.

In C:

void reverse_words(char *sentence)
{
   char *start = sentence;
   char *end = sentence;

   while (*end != '\0') {
      ++end;
   }
   --end;

   reverse_chars(start, end);
   
   while (*start != '\0') {
      for (; *start != '\0' && *start == ' '; start++) ;
      for (end=start; *end != '\0' && *end != ' '; end++) ;
      --end;
      reverse_chars(start, end);
      start = ++end;
   }
}

void reverse_chars(char *left, char *right)
{
   char temp;

   while( left < right) {
      temp = *left;
      *left = *right;
      *right = temp;
      ++left;
      --right;
   }
}

In Java:

public string ReverseWords(string sentence)
{
  string[] words =  sentence.split(" ");
  string rev = "";
  for(int i = words.length - 1; i >= 0 ; i--)
  {
    rev += words[i] + " ";
  }
  return rev;
}

In Python:

def reverse_words(sentence):
  return " ".join(reversed(sentence.split(" ")))

That’s why Python is good for Rapid Development.

I am also using PyGTK (graphic library for Python) in my work. There is a great tutorial Python GTK on youtube! PyGTK requires very less code than e.g. C# to create some simple application. We do not to have tons of generated code when we start. We create application from scratch. Look at below Hello World example.

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:
    def hello(self, widget, data=None):
        print "Hello World"

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.button = gtk.Button("Hello World")
        self.button.connect("clicked", self.hello, None)
        self.window.add(self.button)
        self.button.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

The result is the window with button ‘Hello World’. When you click the button, then ‘Hello World’ will be printed on console. All of that with 22 lines of code (I do not count white lines).

Hello Python

If you don’t know python yet, I encourage you to try it. Programming in python requires a little bit different way of thinking. It also allows you to look at the programming from the different perspective.

Python installation is easy on all operating systems and you can find it in google. To install PyGTK in Windows you can use all in-one installer. There is also all-in-one installer for Mac. PyGTK is included in most Linux distributions, so you won’t need to install it if you are using Linux.