Ruby Basics

Introduction

Our back end focus will be on Ruby, the language designed for programmer happiness. What takes dozens of lines of code in Java or a hundred in C could take just a couple in Ruby because it prepackages lots of sneaky functions into easy-to-use convenience methods.

Ruby is pretty darn close to Python. In some ways, they sort of resemble romance languages – once you’ve learned one, it’s not terribly hard to pick up another because they tend to follow many of the same conventions, just using different “words”. Python tends to be taught more in colleges and is used a fair bit for more data-intensive and processor-heavy applications.

But Ruby has a secret weapon that makes it the love of fast-iterating website producers – the framework Ruby on Rails (which we’ll cover in the next section on Frameworks). It has been optimized for being able to write code faster and with less headache, which allows you to iterate more frequently when building a website. By so doing, the end product is more likely to suit the client or the user’s needs, making your first mission as an engineer a success.

With either of the languages, there are a couple of things that aren’t immediately intuitive but become very useful when you understand them. These are the quirks and nifty tricks that you didn’t see in Javascript.

In this lesson we’ll do a healthy introduction to Ruby and then, later on in the full Ruby course, you’ll get to understand it like the back of your hand.

A final note – you’ll be learning a bunch of new terminology and concepts here but don’t think they’re only applicable to Ruby. Most of it (like methods, classes, objects etc.) will pop up again in pretty much any other language you ever pick up.

Learning Outcomes

Look through these now and then use them to test yourself after doing the assignment.

What is an “interpreted” language?

What is IRB?

What are Objects?

What are Methods?

What are Classes?

What are Blocks?

What is an Array?

What is an Iterator?

What are hashes?

What is a library?

What is a gem?

Assignment

Dive in a little deeper by reading chapters 1-10 of Chris Pine’s Learn to Program. Try to do the exercises at the end of each chapter. Take a crack at chapter 10, but don’t feel disheartened if it still doesn’t click for you. Answers to the exercises are available at learntoprogramanswers.blogspot.com

Have a go at the next two problems too; feel free to move on to the next lesson though if you find it too challenging. The aim here is to increase your resilience to difficult problems and get you more exposed to how loops and methods are structured: