Ruby 101: Introduction

in #technology6 years ago (edited)

Would you like to learn programming but don’t know where to start? Ruby is widely regarded as one of the best programming languages for beginners. This tutorial series is designed to initiate the complete beginner into the world of programming by way of the Ruby language. The following is a general introduction that will outline what you need to get started.

diamond-704072_1920 - Copy.jpg

Ruby Programming Tools

In order to begin learning how to program in Ruby, you'll need at least three tools: a text editor, a terminal and a Ruby runtime.

You should not only learn how to use the text editor and terminal, but also how to customize them to fit with your specific work habits. After all, you‘ll be spending a lot of time with these tools.

There are applications that integrate these features into a single program, which can be very convenient. These are software programs that combine text editing capability with the terminal, and there are also web applications that incorporate both into an online browser.

Text Editor

In order to write/edit your source code, you‘ll need to download a text editor.

An important setting that you should customize in the text editor is the indentation feature. Whenever you press the tab key, the text editor should indent two spaces.

You should also have auto-save enabled in the text editor. This will save your changes automatically and minimize your risk of losing work.

Whenever you open a new file, make sure it has been saved with a filename ending in .rb first. This will inform the editor that this is supposed to be a Ruby file. Your editor will start highlighting your code as Ruby code and will enable other Ruby-specific editor features.

Here are a few keyboard shortcuts that are very useful to know:

  • In order to indent or unindent a single line of code, select the line and then hit tab (or select multiple lines and hit shift-tab).
  • If you want to comment single or multiple lines of code, select the code and hit ctrl-/ on Windows or cmd-/ on Mac.
  • Cut out code with ctrl-x, copy code with ctrl-c, and paste it with ctrl-v on Windows (replace ctrl with cmd on Mac).

Terminal

Virtually every operating system comes with some kind of terminal application built in, which should work fine for these tutorials.

A terminal is an application that doesn’t do much more than provide a window to run another program, which is called a shell. A shell is a text-based program that does not have a window, and thus requires the terminal to run on your graphic interface.

A shell is an interactive application that waits for you to type a command and press enter (or return). It will then run the command and display any output.

In the early days of computers, typed commands were the primary mode of human-computer interaction. However, this system has largely been replaced by graphic user interfaces and hardware input devices (beyond just keyboards).

Although it may seem counterintuitive at first, the text input method of shell programming is in fact a lot more efficient than the use of a mouse and graphic user interface.

You should see the shell’s prompt and can start typing commands once you’ve started the terminal.

Two commands that you should learn right off the bat are cd (which navigates to a designated directory) and ls (which displays the content of a directory).

You may also want to learn how to use command line completion using the tab key. To use this function, type cd, then (following a space) press the first letters of the directory that you’re trying to locate, then hit the tab key. The shell should finish the rest of the directory name for you. If it doesn’t, then there are likely multiple directory names starting with the same letters. In that case, quickly double-tap the tab key, then your shell should show a list of choices. Then try typing the next letter and hitting the tab key once again.

Ruby Runtime

Another tool you’ll need is a Ruby runtime. This is a program that will execute Ruby code from your shell.

You can execute code in a file that is named hello.rb if you navigate to its directory and type the following, then press enter (return):

ruby hello.rb

This will tell the Ruby runtime to interpret the contents of the file hello.rb as Ruby code and to execute it.

Programming Workflow

Playing around with techniques that you have learned is a good way to refine your programming skills. Here is an exercise to help you get started:

  • Write some code in your text editor.
  • Save your code with a filename that ends in .rb to a designated directory.
  • Open your terminal.
  • Using the cd command, point to the directory with your .rb file.
  • Execute the file using Ruby.
  • Switch back and forth between the text editor and the terminal so you can make little changes in your code, then run it through Ruby to observe the effects.

Use the keyboard shortcut alt-tab on Windows or cmd-tab on Mac to quickly toggle between windows.

If you‘d like to repeat a previous command, just hit the up key until the desired command appears, then press enter (return) to run it.

Interactive Ruby

There‘s another tool that‘s probably worth mentioning, called IRB, which can be launched simply by typing irb in your shell and then pressing enter (return).

IRB is short for Interactive Ruby (I for interactive and RB for .rb) and is another kind of shell. Just like the shell running in the terminal, IRB is also a program that interactively waits for commands followed by the enter (return) key. However, since it‘s a Ruby shell, IRB processes Ruby code instead of system commands.

Here‘s a quick Interactive Ruby exercise:

IRB-exercise_a01 - Copy.jpg

The $ symbol indicates that you‘re in system shell mode, and the irb command launches the IRB program. The > sign then indicates that you‘re interacting with the Ruby shell.

The second line is Ruby code. When you type this line and press enter (return), Ruby will then print out Hello World!.

It will then also print out the return value for this statement, which is nil in this case. This is something that you can ignore for now.

The last line displays the Interactive Ruby prompt (>) again.

You can exit the Interactive Ruby session and get back to your system shell by typing exit and hitting enter (return). Or you can just press ctrl-d (cmd-d on Mac), which does the same thing.

Conclusion

This introduction is a preface to a series of posts on Ruby programming. The next post in this series will cover the object-oriented approach to programming (which Ruby utilizes).

This post is licensed under the Creative Commons Attribution-ShareAlike 2.0 Generic license (CC BY-SA 2.0 1920px-CC-BY-SA_icon_a02 - Copy.jpg) (source).

Sort:  

Congratulations @inf0! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made your First Vote
You received more than 10 upvotes. Your next target is to reach 50 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Thank you so much for sharing this amazing post with us!

Have you heard about Partiko? It’s a really convenient mobile app for Steem! With Partiko, you can easily see what’s going on in the Steem community, make posts and comments (no beneficiary cut forever!), and always stayed connected with your followers via push notification!

Partiko also rewards you with Partiko Points (3000 Partiko Point bonus when you first use it!), and Partiko Points can be converted into Steem tokens. You can earn Partiko Points easily by making posts and comments using Partiko.

We also noticed that your Steem Power is low. We will be very happy to delegate 15 Steem Power to you once you have made a post using Partiko! With more Steem Power, you can make more posts and comments, and earn more rewards!

If that all sounds interesting, you can:

Thank you so much for reading this message!

Congratulations @inf0! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.20
JST 0.034
BTC 90261.92
ETH 3055.42
USDT 1.00
SBD 2.96