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

Libraries and Class Syntax

So far, we’ve only worked with Ruby and its core components without the advantage of other libraries that are available in Ruby. Additionally, we’ve only taken a partial peek at Ruby objects and classes. In this chapter, we will take a test drive with you to learn about Ruby libraries (gems) and how we can take advantage of the Gemfile to do so. Lastly, we’ll also learn the basics of class syntax to help us move toward more advanced tools, such as Ruby on Rails.

With that in mind, in this chapter, we will cover the following topics:

  • Let’s get ready to bundle!!!
  • Gemfile versus composer.json
  • Integrating libraries into your code in Ruby
  • Declaring classes in Ruby
  • Objects in Ruby
  • Inheritance in Ruby

Technical requirements

To follow along 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 Xcode Command Line Tools installed
  • Ruby version 2.6 or later installed and ready to use

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

Let’s get ready to bundle!!!

Programming languages by themselves, while useful, can’t take into account every single use case that a programmer might encounter. The core of the language includes many useful libraries, so out of the box, the language is quite useful. This is true for most programming languages. However, there comes a time when we need to go beyond the core library and use other libraries to solve our issues at hand. In Ruby, the community has created a number of libraries, fondly named gems. To keep track of these gems, the Ruby community has come up with a tool called bundler. To season PHP developers, the PHP counterpart for the bundler is Composer (https://getcomposer.org/). The two work for the same purpose (managing libraries), but bundler works slightly differently in that it installs the libraries in the computer, while Composer merely makes them available for your project. But wait – we haven’t even installed a library yet. Let’...

Gemfile versus composer.json

As I mentioned before, bundler helps us handle all our program’s dependencies – that is, everything we need to install in order for our program to run correctly. To accomplish this, bundler uses a text file, which we will call a Gemfile. Composer works in a very similar way by having us create a file called composer.json, but while Composer downloads the required libraries into a folder, bundler installs them on our system. If bundler determines that a dependency is missing, it will automatically try to install it. Ruby’s way is a bit more magical (or automatic). Let’s take bundler for a test drive to understand the process a little further. We will start by uninstalling our previously installed oj gem with the following command in the shell:

gem uninstall oj

The preceding command will confirm when the gem is removed from our system:

Successfully uninstalled oj-3.14.2

Now, if we try to run our reading_json.rb again...

Integrating libraries into your code in Ruby

One of the most useful skills you should acquire in your path to becoming a seasoned Ruby developer is integrating other gems into your code. As we’ve seen before, this is accomplished by using the Gemfile, but we’ll look at some additional options we can add to it and integrate them into our own scripts. Let’s write a script that takes the GitHub public API and lists all of the public repos for the user @PacktPublishing. There are several ways we could do this, but for this example, I’ve chosen a gem called Faraday. You can take a look at the source code here: https://github.com/lostisland/faraday.

Faraday is a client library that can help us make Representational State Transfer (REST) calls that are much easier to read than using the native Net::HTTP library that comes with Ruby. Let’s create a folder called integrating_gems and navigate to that folder:

mkdir integrating_gems
cd integrating_gems...

Declaring classes in Ruby

Both PHP and Ruby are languages that use the Object-Oriented Programming (OOP) paradigm, Ruby by design and PHP by its own evolution. By now, serious developers should be very familiar with the paradigm. In PHP, all frameworks use OOP. While we are not going to go in depth into how this paradigm is implemented in Ruby, we will go through the basics of class syntax.

A class is basically an abstraction of a real-world entity. It is the blueprint of this abstraction. Let’s start by creating a simple class representing a person, some attributes for this person, and an action (or method) for them. Let’s create a file called class_syntax.rb with the following content:

class Person
end

This is as simple as it gets, but this by itself is not very useful. For this to be useful, we need to add attributes that represent the characteristics of a person. So, let’s add some attributes such as their first name and their last name. Our code will...

Objects in Ruby

In the previous section, we defined what our abstraction of a person should look like. It is a person that will have a first name and a last name, and we will be able to print out their full name. In parallel to the construction business, since we now have a blueprint, we are now ready to erect our building with these specifications. This creation is what we call an instance or an object of a class. The class definition is generic and the instance is specific. Without going too deep into this relationship between a class definition and an object, we’ll take a look at what this relationship looks like in code and how this will help us make better and more readable code. Let’s take our previous code and create our first object:

# class_syntax.rb
class Person
  def initialize
   @first_name = 'James'
   @last_name = 'Raynor'
  end
  def full_name
    puts...

Inheritance in Ruby

So far, we’ve looked at a few features that come with Ruby’s implementation of the OOP paradigm, but we have neglected to look at one of the core features that help us recycle code. Inheritance can be simplified as the practice of passing the features of a class to create a brand-new child class. With this new class, we can use any of the features from the parent class, create new features, or customize the features that come from the parent class. The syntax for inheritance can be quite different than in PHP, but the behavior is quite similar. With that in mind, let’s take a look at a few use cases and see it in action.

Let’s say we wanted a class that would let us connect to a database. Instead of having to write all the functionality to connect to a database, we could get an already created database class, create a new one that inherited all the database functionality, and then focus on creating just the features that we need. This...

Summary

In this chapter, we learned how Ruby libraries are installed, used, and named (gems). We also learned to use the bundler tool to install gems, creating environments for our scripts and programs to function correctly. Lastly, we learned the most basic OOP syntax to both create, instantiate, and inherit classes. Now, we are ready to start debugging.

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}