For those of you who are intimidated by coding, look no further than the hopeful red glimmer in your career-switching horizon. Ruby is your best friend, grasshopper. It's a programming language that is easy to read because it looks a lot like plain English.
Readability catered to beginners isn't typical for coding languages. This ease of understanding is because its creator, Yukihiro Matsumoto, built Ruby with humans' happiness in mind. Isn't it nice to be put first, before a computer?
A sandbox for coders is a development environment where you can play and experiment with code without crashing a real-world app. Imagine making a mistake and messing up someone's cloud-hosted calendar or interfering with someone's food delivery app. Just like kids at a playground, they can build a sloppy sandcastle that falls apart when other kids run past it. No one gets inconvenienced in the process except for the crying kid with a crumbled castle.
Before we dive into the concepts of Ruby, please go to Try Ruby so that you can code along with this introduction. An essential key to learning how to code is that you cannot learn to code from passively reading articles or watching tutorials. You must code along. Repetition helps the material sink in. Have you open up one of your tabs to Try Ruby? Great, let us begin!
Ruby is object-oriented, a phrase you'll hear more as you dive into your development journey. Everything in Ruby is an object. This paragraph that you're currently reading is an object. The featured picture above of a red light bulb is an object. Also, the title of this post is, you guessed it, an object.
When we think of coding as manipulating objects, then it's a little easier to decipher what exactly is going on when you're reading confusing lines of code. Objects are data structures that programmers can control to execute programs. Think of programs like scripts that tell the software what to do. For example, when you click on a button, you are telling the program to grab certain objects and display them in the web browser. Those objects can be an article, a podcast, and a video.
Data structures consist of different data types. In Ruby, these types are numbers, strings, booleans, symbols, arrays, and hashes.
Strings are words and sentences such as, "I love Ruby!" You always wrap strings in quotations marks.
Booleans are either true or false. Unlike strings, you do not wrap them in quotes. Ruby is case-sensitive, so you never capitalize false or true.
Variables are like nicknames that hold the memory of specific data. For example:
- my_favorite_number = 11
- my_fav_num_is_10_boolean = false
- my_string = "When the time is 11:11 make a wish."
When creating variables, make sure the name matches the data it's holding. Separate each word in your variable's name with an underscore. Try to keep the names short. The boolean variable above isn't a great example, because it's a bit on the long side.
Number variables can hold more than just a single number. You can program variables to hold math problems. There are six arithmetic operators you can call upon when developing with Ruby:
- Addition +
- Subtraction -
- Multiplication *
- Division \
- Modulo %
- Exponentiation **
While you don't need to be an expert in mathematics, having a strong mathematical background serves you as you advance into your coding career.
The Puts and Print Commands
The print command takes strings and prints them onto your web browser. The only difference between the puts and the print command is that puts will add a line break after the string. See puts and print in action:
puts "I am a string that's printed out with puts. There is a line break after this sentence."
print "I am a string that's printed out with print."
Manipulating Ruby Objects with Methods
Say it with me, "Everything in Ruby is an object." If a string is a noun, then a method is a verb. There are many built-in methods in Ruby, and they always start with a dot. Let's put this concept to work:
Guess what number this method outputs? Yep, you guessed it, 6. The ".length" method works on sentences as well, and it counts every single character, even the symbols and spaces.
"I love learning Ruby, because it's so easy.".length
This method returns the number 43.
But, Milcah, who cares about the length of a string? What's the point of all of this?
Sometimes, when a website prompts you to enter a password, there is a maximum amount of characters you must have for the password to be valid. This instance is where the ".length" method is of utmost importance. For example, you can check whether the password is at least six characters before the password gets saved into the database.
Another use case for this method is its ability to monitor a maximum character count for social media postings such Twitter's character limit for tweets. If the post exceeds a certain amount of characters, then Twitter will not let you post your tweet, differentiating the tweets from blog posts. It's great for marketing and directing your followers to external links.
Want to Learn More?
Here is a free Ruby course by Codecademy.
Read Ruby documentation.