Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
From PHP to Ruby on Rails

You're reading from  From PHP to Ruby on Rails

Product type Book
Published in Dec 2023
Publisher Packt
ISBN-13 9781804610091
Pages 244 pages
Edition 1st Edition
Languages
Author (1):
Bernard Pineda Bernard Pineda
Profile icon Bernard Pineda

Table of Contents (15) Chapters

Preface 1. Part 1:From PHP to Ruby Basics
2. Chapter 1: Understanding the Ruby Mindset and Culture 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

Setting Up Our Local Environment

As a developer, you probably already know that one of the key skillsets that you need to start programming is to install the programming language itself onto your computer. We need a way to start testing code besides our head, as our brains are not the best language compilers. But what does installing the language really mean?

Coming from PHP, this could mean installing the binary interpreter of PHP on our computer so that we can run PHP, open a browser, and then away we go. Or it could mean downloading the PHP source code, compiling it, and generating our own binary with the compilation options we choose. In Ruby, we not only have very similar options to these, but also many more ways to install the Ruby interpreter on our local machine.

In this chapter, we will explore different ways of setting up our development environments. We will analyze the pros and cons of each to give you different options to choose from so that any errors will be consistent...

Technical requirements

To follow along with this chapter, we will need the following:

  • Any IDE to view/edit code (e.g. SublimeText, Visual Studio Code, Notepad++ Vim, Emacs, etc.)
  • For macOS users, you will also need to have the Xcode Command Line Tools installed

The code presented in this chapter is available at https://github.com/PacktPublishing/From-PHP-to-Ruby-on-Rails.

Installing Ruby locally

We are ready to set up our Ruby environment. Probably the most convenient way to install Ruby on our machines is with a package manager or an installer, depending on your operating system.

macOS users

For macOS users, the brew package manager is the way to go.

To install brew, using the Finder window, navigate to the Applications folder, then to the Utilities folder, and then scroll until you find Terminal:

Figure 2.1: The Application Utilities

Figure 2.1: The Application Utilities

Double click on the Terminal icon and you should see a Terminal with a shell prompt waiting for commands:

Figure 2.2: The Terminal

Figure 2.2: The Terminal

Then copy the command from the brew homepage (https://brew.sh/) that you can see here:

Figure 2.3: Homebrew installing instructions

Figure 2.3: Homebrew installing instructions

Now paste the command into the Terminal to install brew.

Once brew is installed, installing Ruby (or its variations) is easy. If brew was already installed, you...

Using a virtual machine

So far, we have seen how to install Ruby on our own local machine, that is, the machine we use daily for work. This is the best way to install Ruby when starting out, but once you start working on more complex applications and with other developers, you will want to have consistency in the behavior of everyone’s local environments.

Why, you may ask? The simple answer is that we want to avoid the dreaded phrase that all developers have either heard or even used at some point in our careers: “It works on my machine.” What is important to remember is that every local environment is different, from the processor to the OS version and Ruby version, and this can get in the way of more important work.

As an example that I personally experienced, we once wasted almost a week when we deployed a PHP application to a Linux server that was developed on Windows local machines. The problem was that one of the developers forgot that Windows (at the...

Using Docker

Docker (https://www.docker.com/) is yet another virtualization technology that has become the go-to option for a lot of medium and large enterprises. While it’s still a virtualized environment and comes with its own set of disadvantages, the advantages overshadow them. Let’s take a look at a few advantages:

  • Docker is not fully virtual – it creates a container that shares resources with the host in which it is running. Because of this, it runs significantly faster than a virtualized environment. I’m oversimplifying the Docker technology, but in essence it’s an improved (in my opinion) virtualized environment.
  • You can package your Docker environment into a Docker image (similar to a virtual machine), which can be deployed and used by others with ease.
  • With the advent of cloud computing, Docker has become more and more popular. Amazon (AWS), Microsoft (Azure), and Google (GCP) all support Docker and little by little have...

Using rbenv

While it is highly recommended to use some sort of homogeneous environment for all members of the team (such as the virtual ones previously mentioned), a simpler and quicker way to create equivalent environments is to use some sort of Ruby version management tool. These types of tools allow us to install different versions of Ruby and for the most part will behave similarly, even if they are installed on different operating systems. We have a couple of options, but for simplicity, we will use rbenv: https://github.com/rbenv/rbenv.

rbenv allows us to install multiple versions of Ruby and manage these versions. By managing them, I mean we can define what version of Ruby the whole system uses (global), or we can define a specific version to be used on each project (local). For macOS and Linux users, you should follow the instructions on the previously mentioned GitHub repository, which also serves as the official website. If you run into any issues while trying to install...

Summary

So far, we’ve learned how to install Ruby on macOS, Windows, and Linux systems. While some operating systems may already come with Ruby installed, it is almost always outdated. We should always install the latest version of Ruby, as installing Ruby is part of our own development process as Ruby developers.

We also learned about virtualization with VMware, VirtualBox, and Docker. I want to point out that the theory regarding virtualization technologies might be a little overwhelming for a beginner developer – I personally did not work with any of these virtual technologies when I started with Ruby, mainly because it was not a practical option, but also because they had not become so popular at that time – but I can now confidently say that I wish I had had those resources when I started, as it would have saved me some major headaches. Having said that, I can guarantee you that they will come in handy (at least Docker), but to start programming in Ruby...

lock icon The rest of the chapter is locked
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 €14.99/month. Cancel anytime}