Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

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.

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 €18.99/month. Cancel anytime