Getting a Coding Job: Ruby Design Principles and Code

By Nikhil Abraham

It’s a good idea to know how to work with Ruby to get a coding job. Ruby was created by Yukhiro “Matz” Matsumoto, a developer with experience programming in Perl and Python, Unsatisfied with both and wanting an easy‐to‐use scripting language, he created Ruby.

When designing Ruby, Matsumoto’s explicit goal was to “make programmers happy.” He created the language so that programmers could learn and use it easily. Today, Ruby is a popular way for startups and companies to quickly create prototypes and launch websites on the Internet.

A few design principles make programming in Ruby less stressful and more fun than other programming languages. These design principles follow:

  • Conciseness: In general, short and concise code helps create faster and easier‐to‐read programs. The initial set of steps written in English to run a program is referred to as pseudocode. Ruby is designed so that little additional effort is needed to translate pseudocode into actual code. Even existing Ruby commands can be made more concise. For example, Ruby’s if statement can be written in three lines or just one.

  • Consistency: A small set of rules governs the entire language. Sometimes this principle in referred to as the principle of least astonishment or the principle of least surprise. In general, if you’re familiar with another programming language, the way Ruby behaves should feel intuitive. For example, when working with string methods in JavaScript, you can chain them together as follows:

    “alphabet”.toUpperCase().concat(“Soup”)

    This JavaScript statement returns ALPHABETSoup by first making the string alphabet uppercase by using the .toUpperCase() method, and then concatenating soup to ALPHABET. Similarly, the following Ruby statement chains methods just as you would expect, also returning ALPHABETSoup:

    “alphabet”.upcase.concat(“Soup”)
  • Flexibility: There are multiple ways to accomplish the same thing, and even built‐in commands can be changed. For example, when writing an ifelse statement, you can use the words if and else, or you can use a single ?. The following versions of code both perform the same task:

    Version 1:

    if 3>4
     puts “the condition is true”
    else
     puts “the condition is false”
    end

    Version 2:

puts 3>4 ? “the condition is false” : “the condition is true”

Ruby generally uses less punctuation than other programming languages you may have previously tried. Some sample code follows:

print “What’s your first name?”
first_name = gets.chomp
first_name.upcase!
if first_name==“NIK”
 print “You may enter!”
else
 print “Nothing to see here.”
end

If you ran this code, it would do the following:

  1. Print a line asking for your first name.

  2. Take user input (gets.chomp) and save it to the first_name variable.

  3. Transform any inputted text into uppercase.

    Test the user input. If the input equals “NIK” then the code would print “You may enter!”; otherwise, it prints “Nothing to see here.”

Don’t worry if these statements and commands seem foreign to you. For now, as you look at the code, notice some of its styling characteristics:

  • Less punctuation: Ruby, unlike JavaScript, does not have curly braces, and unlike HTML, does not have angle brackets.

  • Space, tab, and indentation are ignored: These whitespace characters do not matter unless they are in a text string.

  • A newline indicates the end of a statement: Although you can use a semicolon to put more than one statement on a line, the preferred and more common method is to put each statement on its own line.

  • Dot notation is frequently used: The period (for example, .chomp or .upcase) signals the use of a method, which is a set of instructions that carry out a particular task. In this code example, .chomp removes carriage returns from user input, and .upcase transforms the user input into uppercase.

  • An exclamation point signals danger: A method applied to a variable, such as first_name.upcase, by default transforms a copy of the variable’s value, not the value itself. An exclamation point signals a permanent change, so first_name.upcase! permanently changes the value of the variable first_name.

To learn more Ruby, you might want to check out Code Academy and tryruby.org for some free, short, self‐taught lessons.