Ruby is meant to be read as sentences
Having said all that about Ruby, let’s get our hands dirty and start with the most basic of concepts. You already know about PHP variables. Variables hold information that can be used and referenced in our program. Also, PHP is a dynamically typed language, which means that the PHP “engine,” which interprets our PHP code, will automatically infer the type of content within that variable. That’s to say the following two things:
- We don’t have to define what type of content our variable has
- A variable can change types without failing
Coming from PHP, you won’t have to break your head to learn a new way of using or defining variables with Ruby, as Ruby behaves exactly the same way. However, beware that in other languages that are strongly typed, such as Java, a variable has to be defined with the type that it will contain and it can’t change types over time.
So let’s play around with some variables in PHP:
<?php $name = "bernard"; $age = 40; $height_in_cms = 177.5; $chocolate_allergy = true; $travel_bucket_list = ["Turkey", "Japan", "Canada"];
Ruby is not much different in this scenario:
name = "bernard"; age = 40; height_in_cms = 177.5; chocolate_allergy = true; travel_bucket_list = ["Turkey", "Japan", "Canada"];
For those experienced PHP developers reading this whose eyes might be bleeding from my lack of using PHP Standard Recommendation (PSR) standards on the PHP block, I apologize, but I wanted to give you a glimpse of how the code could be written in a similar manner rather than focusing on PHP’s best practices. Notice that we just wrote the variable names without the $
symbol. Another difference between PHP and Ruby is that we do not use any tag to denote PHP code, whereas, in PHP, we use the opening PHP tags (<?php
). So, the main differences (so far) between our snippets are the way we call PHP code with the PHP tags and the way we refer to variables. While this is a functioning Ruby code, I intentionally wrote the Ruby block very PHP-esque to also give you all a glimpse of Ruby’s flexibility. Ruby is extremely flexible to the point of being able to bend Ruby’s own behavior. An example of this flexibility is that while we can add a semicolon (;
) at the end of each line, it is a Ruby best practice to leave them out. Should this topic of Ruby’s flexibility interest you, you may want to check metaprogramming in Ruby. This Ruby guide is a great starting point:
https://www.rubyguides.com/2016/04/metaprogramming-in-the-wild/
But let’s not get ahead of ourselves, as this topic is really a complex one – at least for a beginner Ruby programmer.
Given the preceding code in PHP, let´s now determine whether the name is empty. In PHP, you would use the empty
internal function. We surround it with another internal function called var_dump
to show the contents of the empty function result:
$name = "Bernard"; var_dump( empty($name) );
This will output the following:
bool(false)
According to the documentation of the empty
function, this is false
because the name is not an empty string. Now, let’s try that in Ruby:
name = "bernard"; puts name.empty?;
There are a couple of things that we have to notice here. The first thing that comes to mind is that this is read almost as a sentence. This is one of the key points to how the Ruby community has come together and used Ruby to make code that is read by humans. For the most part, you should avoid writing comments on your code unless it’s for copyright and/or it does require an explanation. Ruby goes as far as having a strange way to write multiline comments. If I were to write a multiline comment on my code, I would have to look the syntax up because I’ve never used that notation. That’s not to say that you can’t or that you shouldn’t. It’s there for a reason. It simply means that the Ruby community seldom uses that notation. To write a comment in Ruby, you would simply add the hashtag symbol (#
) as the first character on a line:
# This is a comment
As you know from comments within a snippet of code, this line will be ignored by Ruby. Keep in mind that a programming language, just like a spoken language, evolves due to its use. The best of tools may be lost just because no one uses them. Part of learning a language also involves learning the usage of the tools and best practices. This includes knowing what the Ruby community has decided not to exploit and what to use. So, while the community rarely uses multiline comments, all Ruby developers will take advantage of one of its most powerful tools: objects.