Back to Basics: Ruby 101

The Basics of Language

This article is for people newly introduced to programming and are looking for a primer course to intro them to the wonderful world of coding. If you’re looking for an article on RESTful API’s, stick around awhile – that’s coming soon.

Let’s dive right into this. Ruby is a programming language. Ruby, along with being very intuitive and beginner friendly, is also not unlike normal languages like English. In the English language we have nouns, and verbs among other parts of speech. Ruby has the same thing, let’s take a look.

Syntax

The arrangement of words and phrases to create well-formed sentences in a language.

You have, no doubt, heard the word syntax by now in your search of coding resources. Much like normal languages, computer languages (i.e Ruby, Python, Javascript) have different syntax. We need to formulate our nouns and verbs in such a way that the computer understands our commands and properly executes those commands.

Nouns

A noun in the world of Ruby is going to be the data object. This data object could be a string, integer, array or hash – those are all nouns. They define what the data object is exactly. This is very important, as you will learn later on, because data objects are only able to perform a limited amount of operations. You would never expect a string to operate as a hash and vice versa. These are built in rules that allow us to make reasonable assumptions about the properties and actions of given terms, words and variables.

Let’s test it out. Go into your terminal and open up a pry session by typing in pry. Type in the following: "String".class. You should get a return of, String.  What is happening here? .class is a built in method in Ruby that will return the type of data object you are calling it against. In layman’s terms we are asking the computer to tell us the noun of that data object and the computer is returning a response that says, this value is a String.  It is important to note that Ruby will consider anything in quotations as a string, even something like "23".class will return String – Go ahead, try it out for yourself!

Another noun that is available to use is fixnum. This is the class for any integer (positive or negative whole numbers) that are written without quotation marks – this is an important distinction. They can be used in any kind of mathematical operations like you would normally expect.

You can verify that this works by going back to your terminal and typing in the following: 23.class should return a value of Fixnum. You can try this with any combination of numbers, something like (15 * 3).class.

Verbs

A language without verbs is pretty boring. Without verbs, the boy and the ball could never do anything cool. However, with verbs, we can give action to this scenario – the boy throws the ball. Much better.

Ruby and all Programming languages are the same way, they use verbs to get things done and give action to their programs. Another word for verbs in Ruby is methods. These are written lines of code that tell the computer what actions to take with certain types of objects. They are what give action to our programs. Of course, you can create your own methods, and you will when you get a little more knowledgeable but you can also utilize some of the built in methods Ruby provides us with.

Type the following into your terminal, "I Love Ruby!".reverse, you should get a return value of !ybuR evoL I. This is a built in method that comes with ruby that will reverse a given string input. Try another, "I LOVE RUBY!".downcase. This should output the previous string in all undercase, i love ruby!.

Conclusion

This may seem like trivial information, but for something just starting out it is critical. It will become second nature in no time and you will have no problem remembering exactly what does what here. Afterall, Ruby is a language designed to be both user and developer friendly – everything is very intuitive and does just what you would think they would do based on their names.

One comment

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s