Getting a Coding Job: Ruby Design Principles and Code
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.
Flexibility: There are multiple ways to accomplish the same thing, and even built‐in commands can be changed. For example, when writing an if‐else 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:
if 3>4 puts “the condition is true” else puts “the condition is false” end
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:
Print a line asking for your first name.
Take user input (gets.chomp) and save it to the first_name variable.
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:
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.