Building an App Series Part 3: Programming in Ruby
Suppose you and your friends have this amazing idea for a company that is guaranteed to make millions. At its core, the project requires the ability to store information in a database and retrieve it when necessary. Equipped with user stories, acceptance criteria, and wireframes you learned to produce from a prior lesson, you’re now ready to start programming. You are exploring a number of languages, trying to figure out which one would be best suited for the project. In this article, we’ll discuss why we love Ruby here at Launch Academy.
Code Ruby. Be Happy.
Ruby’s ease of use is very beneficial to the entry-level programmer. It was designed to optimize the developer experience, rather than a machine’s functionality. Of Ruby’s creation and intent, creator Yukihiro Matsumoto says, “We need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves.” With this in mind, he created a programming language that has lowered the barrier to becoming a software developer. Let’s explore some aspects that have made Ruby such a highly regarded language.
Ruby Syntax is Easy to Understand
When you first look at Ruby code, you will notice how readable it is. Syntactically, Ruby is far more aligned with the english language than other programming languages. Without having any knowledge of it, readers can make inferences about what a block of code is doing.
Ruby excels as a platform for aspiring developers to achieve proficiency and/or mastery of advanced computer science topics. It is particularly effective for learning framework oriented concepts that can be easily translated to other languages.
As a beginner, you will in all likelihood have bugs and errors in your code. Ruby’s approachability and syntactic efficiency come in handy when debugging code. JavaScript, for example, incorporates characters likes semicolons and braces to partition code segments. Ruby is great to debug in because you are less likely to spend time on missing or erroneous punctuation.
Let’s take a look at a few of the differences between Ruby and JavaScript with these bits of code.
# Ruby example
def hello_world
puts “Hello World.”
end
hello_world
// JavaScript example
function hello_world() {
console.log(“Hello World.”);
};
hello_world();
The above methods output the sentence “Hello World.” Notice the parentheses, brackets, and semicolons that break up elements of the JavaScript version. Functionally, these two methods perform the exact same operation (outputting a sequence of characters). On the second line, Ruby utilizes the puts
method, while JavaScript uses the console.log
method, to output the sentences. To finish each of these methods, Ruby uses end
while JavaScript uses };
. Without any context, a reader may not infer that a };
intends to end the defined function.
Ruby Documentation and Tutorials
Ruby’s rich ecosystem allows developers, both new and seasoned, to master the language’s fundamentals and more advanced concepts. Ruby is one of the best documented and sourced languages in the software development realm. For beginners, documentation is critical for understanding the breadth and ability of a given language. Within Ruby’s documentation, not only are there lists of methods and instructions on how to achieve programming goals, but there are examples that support the various definitions you will find. The official Ruby-Docs are managed by long-time developers in the Ruby community, aiming “to provide complete and accurate documentation for the Ruby programming language.”
Since its release in the mid-1990s, millions of users have embraced and contributed to the Ruby community through open-source directories. Open-source refers to reference materials that are open to the public. There are millions of applications in dozens of languages that are available for public consumption available on GitHub.
Ruby’s online documentation is structured to make it easy for users to find methods and definitions in a very efficient manner. But, oftentimes the core documentation does not answer a question the way a user wants it to. Where do developers with more complex issues turn to?
Stack OverFlow is an incredible online resource where users ask specific questions pertaining to code implementation. In order to obtain the most effective answers, users submit their code in markdown format to support their question. When users include lots of details in their questions, other users are more apt to give feedback that not only answers the question, but clarifies any confusion insinuated within the question. The submitted answers are up-voted and down-voted by interested users based on a given answer’s efficacy. Also, the user who asks a given question can identify the best answer to their question, which provides clarity for developers with similar questions.
Another component of Ruby’s rich open-source ecosystem are Rubygems. Rubygems give developers the ability to write and release code for the public to freely use. In turn, Rubygems can be downloaded and used by developers all over the world on millions of projects. For beginning programmers, Rubygems are great tools for accomplishing more complex programming objectives. For instance, there is a gem called “Rubies” that allows aspiring developers to practice working with complex data structures.
These third-party rubygems are well documented and curated. Check out Ruby Doc info for a comprehensive list of the available rubygems and their supporting documents. As we continue our exploration, we’ll start to make use of specific ruby gems. For now, just know that this resource exists, and it is a fantastic place for you to learn more about the gems we’ll make use of as we develop our skills together.
Ruby is Object Oriented
Developers often apply abstract concepts to concrete real-world instances by thinking of them in terms of metaphor. Describing object oriented programming in terms of phonics is a great way to understand its concepts. Object oriented programming languages allow us to write code in the same way we do sentences. We have nouns (objects) and verbs (instance methods). These nouns and verbs can interact with one another as a way to communicate our intent as to how components of an application should function.
Objects == Nouns
In Ruby, groups of alphanumeric characters are known as strings. To better understand strings, let’s take a look at a few examples.
‘Hello World’
“I have 3 bicycles in the garage.”
“The quick brown fox jumped over the lazy dog.”
“”
In the above examples, you’ll see that strings are surrounded by either single or double quotation marks. They can include any combination of the numbers and letters in any particular order. On line 4, you’ll notice that they can also be empty. Let’s explore one of the examples in further detail.
“Hello World”.class
=> String
When you call the class
method on a variable, it returns the object’s type. In our example, ”Hello World”.class
returnsString
. In terms of metaphor, think of the String
object as a proper noun. It exists to perform tasks and have tasks acted upon it.
There are other core objects that you’ll find in most programming languages. Integers are numbers without decimal points andfloats are numbers with decimal points. Just like a calculator, you can call all of the typical arithmetic methods on integers and floats. Let’s take a look at a few examples.
123
123 + 123
=> 246
246.class
=> Fixnum
#Note: Fixnum is another name for Integer.
1.0
3.14159265359
5.125.class
=> Float
These are just a few examples of objects you will encounter in the Ruby language. How do we translate this concept of objects to our code reference aggregator, CodeBuddy?
In Ruby, we can create custom objects. For CodeBuddy, the primary object we want to focus on is the Reference
object. Every component of the application will revolve around adding, manipulating, and deleting coding references. We will also need a User
object. Object oriented programs are designed with users in mind. It only makes sense to define how they should interact with a given application. A Comment
is another object that somehow has to relate to both a Reference
and a User
. In future articles, we’ll discuss how Ruby gets these objects to interact with one another.
Instance Methods == Verbs
In Ruby, there are a number of core methods you can call on the various object types. For example, if we called the reverse
method on an instance of the string class, it will reverse the order of the string.
“Hello World”.reverse
=> "dlroW olleH"
Sticking with our phonics metaphor, think of instance methods as verbs. We utilize instance methods to make things happen to objects. Another great component of Ruby is the ability to chain methods together. Let’s make all of the letters lower-cased by calling the downcase
method, in addition to reversing them (the downcase
method takes all of the letters in given string and makes them lowercase).
“Hello World”.reverse.downcase
=> "dlrow olleh"
Applying Instance Methods to Objects
Let’s take a look at a few examples of adding objects together.
“5” + “5”
=> “55”
5 + 5
=> 25
5.55 + 5
=> 10.55
“5” + 5
TypeError: no implicit conversion of Fixnum into String
Notice how in the first example adding the new strings
together just combined them without performing the addition function. When objects are in different states, certain methods—like the addition method—behave differently.
In the 4th example, we tried to add the string
”5”
to the integer
5
. When running the program, we receive an error:TypeError: no implicit conversion of Fixnum into String
. There are methods to convert an object’s state. Let’s take a look at a few of them.
#Converts object to an integer
“3”.to_i
=> 3
#Converts object to a string
4.to_s
=> “4”
#Converts object to a float
5.to_f
=> 5.0
There are hundreds of instance methods in Ruby that can be applied to a number of different core objects. However, we can also create our own methods that can perform custom functionality. When we begin building CodeBuddy in earnest, we will show you how to create your own methods within the scope of custom objects.
Ruby is Test Driven Development Friendly
Another unique feature of the Ruby community is the prevalence of Test Driven Development (TDD). TDD is a development methodology that prescribes writing tests to maintain code validity and stability. In the software development realm, it serves as a quality assurance tool, saving hours of potential debugging time.
Other languages have testing capabilities, but the Ruby community has really embraced TDD methodologies.
Testing allows you to isolate the problem you are trying to solve. As a beginner, it can often be overwhelming trying to overcome a complex problem. By utilizing TDD, developers decompose large problems into smaller problems. Solving all of the smaller problems ultimately leads to solving the large problem.
Developers often debate the merits of TDD. Ruby developers embrace it is an efficient way to build verbose applications and maintain code viability. Also, the Ruby community has created some amazing rubygems that have transformed and standardized TDD practices. In later articles, we explore materials that dive deeper in the TDD methodologies.
In Summary
Ruby is in the midst of an incredible transition from a young programming language with high potential to a mainstay in the software development landscape. It’s ease of use, extensive 3rd-party rubygem gem community, and lower barrier to entry have contributed to its popularity and adoption by software developers the world over. These factors make programming in Ruby a joyful experience.
Next time, we’ll explore boolean values, flow control, and data structures in Ruby.
Challenges
Combining Strings
“It was all a dream.”
“I used to read Word Up magazine.”
Given these two strings, figure out how to combine them into one string (with a space at the end of “It was all a dream.”)
Separating Strings
“50 inch screen, money green leather sofa”
Find a method that separates this string on every word.