Books / Ruby for Beginners / Chapter 5

Variables in Ruby

Variables in Ruby

A lot of stuff is going on. We hope you’re not feeling overwhelmed with information about the right way to run your Ruby program. Let’s jump to something fun.

Variables. Variable is a place inside of your computer’s memory where we can store a value. It’s like a cell in a honeycomb. You may be wondering, why do we need to store a value? Well, because we can read it later, or we can modify it later, it’s convenient.

But we are not obligated to modify the values of our variables. Sometimes we can introduce some variables to improve readability of our program. Often such variables named as constants, because we don’t modify them! That’s why in modern JavaScript programming language we have two keywords: let to declare variable, and const to introduce a constant, something you can’t change in the future. But it’s much easier in Ruby.

Let’s try to define (declare, create, make) a simple variable:

puts "Your age?"
age = gets
puts "Your age is"
puts age  

Program above will ask for the age. When age was provided, it will print out the age back to the screen:

Your age?
20
Your age is
20

The number answered in the program was stored in a variable with the name age. We could name it any way we want, for example a. But in this case line 4 will look like puts a. When it comes to naming variables, there are some important naming conventions out there, you can find them by typing “naming conventions for variables in Ruby” in your favorite search engine (we hope you’re using DuckDuckGo).

But Ruby and JavaScript you’ll see three common naming conventions:

  • Snake case. Underscore "_" between words. Examples:

      client_age
      user_password
      user_password_expiration_date
    

    Snake case is used in Ruby and often in databases for tables and column names.

  • Camel case. Word starts with lowercase letter, words are separated by the following uppercase letter. For example:

      clientAge
      userPassword
      userPasswordExpirationDate
    

    Camel case is often used in JavaScript and statically typed languages (Golang, Java, etc.)

  • Kebab case. Words are separated by hyphen. For example:

      client-age
      user-password
      user-password-expiration-date
    

    Kebab case is often used in HTML. For example:

      <input type="text" name="login" data-error-highlight-color="red">
    

We need to remember only the first naming convention at the moment, because it’s used in Ruby: if variable has multiple words, use underscore. It should be noted that ideally variable should be named by using one word only. Two or more words is usually indicator of too broad context, but it’s out of topic for this book. Programmers agree that sometimes naming variables isn’t something easy:

There are only two hard things in Computer Science: cache invalidation and naming things.

– Phil Karlton

If you came up with variable name which is too long, don’t try to artificially lower the character count (for example, by renaming client_password_expiration_date to cped). At this stage, leave it as it is, and get back to your code later to see if you can do any refactoring (code improvement).

In addition to naming conventions, there are some variable naming rules that come from Ruby language itself: variables should start with letter and must contain only alphanumeric characters or underscore.


Licenses and Attributions


Speak Your Mind