Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
From PHP to Ruby on Rails

You're reading from   From PHP to Ruby on Rails Transition from PHP to Ruby by leveraging your existing backend programming knowledge

Arrow left icon
Product type Paperback
Published in Dec 2023
Publisher Packt
ISBN-13 9781804610091
Length 244 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Bernard Pineda Bernard Pineda
Author Profile Icon Bernard Pineda
Bernard Pineda
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Part 1:From PHP to Ruby Basics
2. Chapter 1: Understanding the Ruby Mindset and Culture FREE CHAPTER 3. Chapter 2: Setting Up Our Local Environment 4. Chapter 3: Comparing Basic Ruby Syntax to PHP 5. Chapter 4: Ruby Scripting versus PHP Scripting 6. Chapter 5: Libraries and Class Syntax 7. Chapter 6: Debugging Ruby 8. Part 2:Ruby and the Web
9. Chapter 7: Understanding Convention over Configuration 10. Chapter 8: Models, DBs, and Active Record 11. Chapter 9: Bringing It All Together 12. Chapter 10: Considerations for Hosting Rails Applications versus PHP Applications 13. Index 14. Other Books You May Enjoy

Everything is an object

The second thing that came to my mind while reading the previous code is that we are calling a method on a string. Now, let’s step back a bit, and this is where we start looking at code through our newbie set of Ruby eyes. Our variable name contains a string. Does this mean that our name is an object? Well, the short answer is Yes. Almost everything in Ruby is an object. I know this might seem as if we’re skipping a few chapters, but bear with me. We will see Ruby’s object-oriented syntax in Chapter 5. For now, let’s take this a step further within our code by getting what type of object our variable has with the following line:

puts name.class();

This will return the type of class of our object (in this specific case, String). We are able to do the same with the rest of our variables and we would get similar values (Integer, Float, TrueClass, or Array). And to take this even further to prove my point that almost everything in Ruby is an object, let’s read the following example:

puts "benjamin".class();

This will also return a String type. So, bear that in mind when you’re writing Ruby code. Now, let’s go back to the initial example with the empty function:

name = "bernard";
puts name.empty?();

The third thing we also notice is that we are actually asking a question. This baffled me the first time I saw it. How do you know when to ask? Is Ruby so intuitive that you can actually ask questions? What type of sorcery is this? Unfortunately, the truth is far less ominous than the code itself. In Ruby, we can name a function or a method with the question mark symbol as part of the name, solely to improve readability. It does not have any special execution or meaning to the Ruby interpreter. We are just able to name a method/function like that. Having said that, by convention, Ruby developers use the question mark to hint that we will return a Boolean value. In this case, it merely answers the question about the emptiness of the variable name. Simply put, if the name is empty, the question will return a true value, and vice versa. This naming technique is part of the Ruby philosophy to make our whole code readable. Additionally, this type of code style is permeated throughout many of Ruby’s internal classes. Some methods that are attached to number objects and array objects are an example of this. Here are a few examples:

  • .odd?
  • .even?
  • .include?

All these examples were named like that for the sole purpose of readability and nothing more. Some of them are even shared between different classes but have their own implementation for each type. While we are currently looking at the question mark symbol, let’s take a peek at a similar symbol: the exclamation point (!). Also known as a bang, it has a slightly different connotation within Ruby developers. Let’s look at it with an example.

Let’s show the name in uppercase letters. In PHP, we would write the following:

$name = "bernard";
echo strtoupper($name);

In Ruby, the same can be accomplished with the following code:

name = "bernard";
puts(name.upcase());

In both cases, this will return the name in uppercase (BERNARD). However, if we make any additional references to the name variable, the variable will remain unchanged:

name = "bernard";
puts(name.upcase());
puts(name);

This would return the following:

BERNARD
bernard

But what happens if we add the bang symbol (!)?

name = "bernard";
puts(name.upcase!());
puts(name);

This will return the name in uppercase twice:

BERNARD
BERNARD

The bang symbol in fact modifies the variable contents permanently. Functions that are named with the bang symbol are referred to as destructive methods because they modify or mutate the original object rather than just return the modified value. Examples of this are these methods from the String and Array classes:

  • .downcase!
  • .reverse!
  • .strip!
  • .flatten!

We can infer what they do just from reading them, but we now know what the bang symbol means in this context. Be careful when using these, but also don’t be shy of using them when the use case requires it. Now, when you read Ruby code, you will be aware of the question mark (?) and the bang (!) symbol.

You have been reading a chapter from
From PHP to Ruby on Rails
Published in: Dec 2023
Publisher: Packt
ISBN-13: 9781804610091
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image