A Brief Guide to Moving from PHP to Ruby
- 5231 views
- 17 min
- Feb 21, 2019
PHP has long been among the top programming languages in popularity rankings. It has lots of advantages, from a low barrier to entry to a huge and well-developed ecosystem. For this reason, lots of newcomers to the world of programming opt for PHP, and you may be one of them.
Yet if you’re reading this article, you’re likely considering switching to a different software development technology and Ruby is on your shortlist. Just like PHP, Ruby is a high-level object-oriented programming language that allows developers to implement web applications of any complexity. But is the shift from PHP to Ruby worth it? Definitely yes. Ruby stands out from other programming languages due to its unique philosophy. Unlike other technologies, it’s created to help developers feel happy about their work.
If you aren’t familiar with Ruby and its developer happiness first philosophy seems far-fetched, this article is for you. We consider the main advantages of this programming language and provide a road map to help PHP developers quickly learn Ruby and related software development technologies.
Key strengths of Ruby
As a PHP developer, you know well the advantages and disadvantages of PHP, so there’s no point mentioning them in this article. Besides, what may seem like a significant drawback to you may actually be a plus in the eyes of another programmer. So let’s move to Ruby right away.
Experienced Ruby developers can praise their favorite language for ages. To cut a long story short, we’re going to draw your attention to the aces Ruby has up its sleeve. Let’s delve in!
#1 Developer happiness first
When Yukihiro “Matz” Matsumoto was designing Ruby, he set a goal to make software developers happy. In Matz’s opinion, a person is happy if they can do their job quickly and while having some fun. So he decided to create a programming language that would be both comfortable and intuitive for developers while allowing them to be productive:
Ruby features an elegant syntax that highly resembles natural human speech. This makes it much easier for developers (and even for non-technical people) to understand code in Ruby compared to code in less intuitive programming languages. To prove this point, take a look at these examples:
This short code snippet prints Hello, world! five times, and even a person with no programming background is likely to understand what it does. Now check out code that does the same thing in PHP:
Compare the two examples above. The majority of developers (regardless of the programming language they work with) will agree that the Ruby syntax is more readable and intuitive.
Simple and concise code is not only easier to write but also to check, meaning developers can be more productive. The syntax significantly speeds up development and maintenance of applications written with Ruby.
Another awesome aspect of the Ruby programming language is its pure object-oriented nature: everything is an object in Ruby. All data structures are objects and, therefore, you can manipulate them accordingly. To show you how this works, imagine you need to count the number of characters in a phrase. Here’s how to do it in PHP:
Now the same code in Ruby:
Take a look at these examples and perhaps you’ll agree that the Ruby code is easier to understand.
#2 Ruby’s synergy
Now let’s focus on frameworks.
There are a bunch of PHP frameworks that follow the Model–View–Controller (MVC) architectural pattern. Here are the most popular:
A variety of frameworks seems to be an advantage for a programming language, but let’s look at the issue from a different angle. All MVC frameworks for PHP listed above serve the same purpose − they help create modern web and mobile applications. The PHP community thus needs to support a variety of frameworks that are quite similar in terms of the tasks they solve.
The situation is different in Ruby − instead of scattering efforts on supporting several identical tools, the community focuses on supporting frameworks that solve different problems. For example, when the community decided to merge Merb and Ruby on Rails, the two MVC frameworks served the same purpose. As a result of this merger, the Ruby on Rails 3 framework was created and the community focused solely on it.
In fact, whenever you hear the word “Ruby,” it’s the Ruby on Rails framework that probably comes to mind. Yet there are other Ruby frameworks that serve different purposes:
Ruby on Rails for startups
Created in 2005 by David Heinemeier Hansson (known as DHH in the community), Ruby on Rails has become a cornerstone of the whole Ruby ecosystem. It’s the dominant Ruby framework, and knowing it is a must for any Ruby developer.
Ruby on Rails is a great tool for technology startups as it enables programmers to speed up development. Ruby on Rails provides a lot of solutions to common problems developers face when implementing a web application. Instead of writing excess code, Ruby programmers can focus on proper implementation of an application’s business logic.
Ruby on Rails relies on a large number of skilled developers that support it and keep it up to date. There are over 3,600 Ruby on Rails contributors on GitHub, which is more than twice as many as the PHP Symfony framework has. The numbers speak for themselves!
Sinatra for implementing a service-oriented architecture (SOA)
Sinatra (together with its offspring Padrino) is a framework and a domain-specific language in Ruby for building web services. While Rails is tailored for developing monolithic applications, Sinatra allows programmers to create web servers and build SOA applications.
When a monolithic application built with Ruby on Rails becomes too big, it becomes reasonable to scale it by splitting the application into several web services. Ruby developers can easily do this with the help of Sinatra.
Hanami for building enterprise applications
Hanami (formerly known as Lotus) is a fully featured architecturally sound Ruby framework that follows the Clean Architecture approach. Thanks to this approach, developers can easily scale a Ruby application built with Hanami regardless of its functional complexity and the number of developers on the project.
The modular structure of Hanami allows Ruby developers to add new functional modules to applications quickly and safely. This makes Hanami an excellent choice for building enterprise applications in which stable performance and high development speed are crucial.
#3 Ruby gems
For many developers, the best component of the Ruby ecosystem is the gems or, in other words, open-source libraries that allow developers to easily expand the functionality of any application written in Ruby. Programmers don’t need to write code from scratch; instead, they can simply select a relevant gem and use it. Gems make programmers’ work easier and significantly speed up development.
Now you might be asking, So what? There are scores of similar libraries in PHP and other programming languages. What makes Ruby special? It’s all about flexibility. For example, lots of PHP libraries are compatible only with a specific framework, which considerably restricts their usability. In Ruby, gems are compatible with any framework, be it Ruby on Rails, Sinatra, or Hanami.
At an early stage of development of the Ruby ecosystem, developers understood the necessity to ensure the compatibility of gems. To make sure gems were compatible, the community created RubyGems − a package manager for the Ruby programming language that’s included in the standard Ruby on Rails package.
RubyGems provides a standard format for managing and distributing open-source libraries written in Ruby. It allows developers to install gems with relevant dependencies. The tool integrates with the Ruby runtime loader and automatically installs required gems. Moreover, RubyGems manages dependencies and downloads the version of a gem that’s compatible with a specific Ruby application.
RubyGems works as a command-line tool called gem. To install a gem, a Ruby developer simply types its name after gem install in the command line:
So far, the Ruby community has developed almost 150,000 gems. There are gems for anything: authentication, payments, content management systems, you name it. Think what functionality you need to implement and there’s certainly a gem that will help you.
Take a look at a brief list of functionalities that you can easily implement with the help of Ruby gems:
#4 Focus on code quality
The Ruby community puts a strong emphasis on code quality. You could even claim that Ruby developers are obsessed with delivering high-quality code. In our opinion, the very philosophy of developer happiness means that developers should be able to do their job well and, naturally, write good code.
Before moving on, it’s important to get a clear understanding of what code quality means.
- Reliability − The software needs to be reliable and predictable to be capable of solving the problems of end users.
- Readability − Quality code is easy to read and easy for other programmers to understand.
- Maintainability − A good codebase is easy to maintain and update.
High code quality is not only a prerequisite for building fully functional applications; it also facilitates their maintenance and reduces technical debt. In Ruby, writing quality code doesn’t take a rocket scientist and doesn’t require years of experience. There are plenty of tools in the Ruby ecosystem that help programmers ensure code quality.
Here are the most important tools for:
General code analysis
Ruby developers use multiple software development practices that help them write A-level code. For example, programmers remove business logic and finders out of the MVC framework. Yet making sure such practices are followed can be difficult. To mitigate this issue, the Ruby community uses a variety of code metric tools, such as rails_best_practices.
Detecting code smells
Code smells (such as bloaters or couplers) are indicators of deep-rooted problems in a codebase that can result in serious problems. Developers need to detect and remove them before the codebase is deployed to production. Ruby programmers can easily do this with the help of such gems as Reek.
Sometimes developers may neglect to write automated tests, but it’s an integral part of the software development process in the Ruby community. There’s no need to emphasize the importance of automated tests for code quality, and the Ruby ecosystem boasts a rich tool set for test automation:
- Testing frameworks, such as RSpec and Cucumber, allow Ruby developers to write automated tests, while Capybara lets them create acceptance tests.
- Test data generators, such as faker and factory-bot, enable Ruby developers to easily create test data to run automated tests against.
Test coverage is the major metric for assessing the code quality of software applications. In the Ruby ecosystem, there are lots of tools (like SimpleCov) that help you find out the test coverage in your application and add more automated tests if needed.
Static code analysis
In order to write code that’s easy to read and easy for other developers to understand, programmers should follow certain rules, formats, and styles. All of them are included in the Ruby style guide, and to help developers write better code, the community has created multiple gems for static code analysis (such as RuboCop).
Apart from checking that code follows the guidelines, Ruby developers can easily check a codebase for structural similarities with the help of Flay.
No matter how proficient and experienced you are, bugs are inevitable. So it’s essential to be able to track them down and fix them. The Ruby ecosystem has lots of debugging tools (such as Pry) as well as utilities that display detailed error pages (such as Better Errors) and, consequently, facilitate bug fixing.
#5 Focus on application security
Application security is the priority for software developers. A vulnerable application is likely to fall victim to hacking or theft of sensitive information, which means reputational and financial losses for business owners.
The Ruby community has developed plenty of tools to detect and root out application vulnerabilities. Such tools as Brakeman help developers scan their applications for vulnerabilities, while the bundler-audit library allows them to detect vulnerable versions of gems.
When it comes to security, it’s better safe than sorry. There are also third-party security assessment services such as CodeClimate that Ruby developers can use.
#6 Mature community
Two eyes see better than one.
You can be an exceptionally gifted developer, but it’s much better to rely on a proficient and skilled community. Let’s find out how different the PHP and Ruby communities are.
As mentioned above, PHP is one of the most popular programming languages in the world, so it’s no wonder there’s a large community of PHP developers. Yet the relatively low barrier to entry of PHP means there are lots of novices in the community, so the size doesn’t match the quality: there are many immature coders.
In Ruby, the situation is different. The community is smaller in size but comprises mostly professionals who know Ruby up and down. Keep in mind that Ruby is typically the second programming language for developers. It’s no wonder the community is extremely active and productive. According to the The State of the Octoverse 2017 report by GitHub, Ruby outpaces PHP in terms of pull requests per year (870,000 versus 559,000, respectively).
There’s another positive aspect about a higher level of experience in the Ruby community − salaries. Needless to say, professionalism should be paid accordingly, and the average Ruby developer is paid more than the average PHP developer:
That’s exactly what David Heinemeier Hansson (the creator or the Ruby on Rails framework) meant in this tweet:
#7 Powerful metaprogramming capabilities
Metaprogramming is a technique of writing code-generating computer programs. In other words, metaprogramming allows developers to create code that can on its own read, generate, analyze, and transform other code or even itself during runtime.
This programming technique helps developers write testable, reusable, and flexible code that corresponds to the DRY (don’t repeat yourself) design principle. Also, metaprogramming substantially speeds up development, allowing Ruby developers to spend several minutes accomplishing tasks that usually take hours if implemented in other programming languages.
A roadmap to moving from PHP to Ruby
Already eager to transition from PHP to Ruby? If your answer is yes, you might be asking how to do it. There are scores of video tutorials, courses, books, and forums about Ruby, but choosing the most efficient can be a problem. We decided to help out by drawing up a shortlist of the best resources that will help you become a high-level Ruby programmer.
Step #1 Learn the basics of Ruby
When learning any new subject you should start from the basics. So if you’re interested in studying (and, hopefully, mastering) Ruby, you should start from the very beginning. There are plenty of guides and books about coding in Ruby, and here are some of the most popular:
- Learn Ruby the Hard Way, a step-by-step guide to learning Ruby that’s comprised of 52 practical exercises. This book provides you with the necessary skills and tools so that you can move to more complex programming topics.
- Ruby Essentials, a free brief guide to the basics of Ruby.
- Why’s (Poignant) Guide to Ruby, an offbeat approach to learning Ruby based on stories and comics.
If you’re already familiar with the basics of Ruby but still have questions, make sure to check the official Ruby FAQ.
This document has been edited with the instant web content converting tool which can be found at htmleditor.tools - give it a try.
Step #2 Study Ruby frameworks
After learning the basics of Ruby, you can move to frameworks. To create top-notch applications in Ruby, you need to know:
Web application frameworks
First of all, you need to master web application frameworks in Ruby. Most developers study only Ruby on Rails, but if you want to become an expert, you should also be able to use other web application frameworks:
Ruby on Rails
- Getting Started with Rails − an official guide to Ruby on Rails that contains clear and detailed instructions on how to get a new Rails application up and running
- Ruby on Rails Tutorial (Rails 5) − an in-depth tutorial on Ruby on Rails
- Agile Web Development with Rails 5.1 − an end-to-end guide to Rails development
- Rails for PHP Developers − a book written specifically for PHP developers that want to switch to Ruby on Rails development
- Sinatra: Getting Started − the official guide to the Sinatra framework
- Sinatra: Up and Running − a book on web development with Sinatra
- Hanami: Getting Started − a comprehensive guide to the Hanami framework
Writing quality code involves a lot of testing, so if you decide to transition from PHP to Ruby, you need to learn testing frameworks, meaning RSpec and Cucumber:
- The RSpec Book: Behaviour Driven Development with RSpec, Cucumber, and Friends − a detailed guide to BDD with RSpec, Cucumber, and other frameworks
- Everyday Rails Testing with RSpec − a manual on test-driven development using RSpec
- Effective Testing with RSpec 3 − a comprehensive guide to RSpec
Business logic framework
Writing functional and bug-free code is crucial, but it’s equally important to structure it properly. Trailblazer, a powerful business logic framework, helps programmers efficiently structure the business logic in Ruby applications. Apart from Ruby on Rails, Trailblazer can be used with other web application frameworks (Sinatra and Hanami).
Trailblazer is growing popular in the Ruby community, so if you want to become a skilled developer, you should know how to apply this business logic framework. Here are some handy resources you can opt for:
- Trailblazer in 12 minutes − a brief introduction to Trailblazer
- Trailblazer − a book that provides a detailed explanation of how Trailblazer works
Step #3 Enhance your skills in Ruby development
As you gain experience in Ruby development, you should enhance your coding skills to build more maintainable, scalable, and reliable applications. Here are some useful books that will provide you with the necessary knowledge:
- Practical Object-Oriented Design in Ruby: An Agile Primer − a complete guide to writing better and more maintainable applications in Ruby
- Ruby Performance Optimization: Why Ruby is Slow, and How to Fix It − this book teaches you to write better and more high-performance code
- Metaprogramming Ruby 2: Program Like the Ruby Pros − a comprehensive guide to the use of metaprogramming in Ruby
Step #4 Interact with the Ruby community
Finally, don’t forget that a large and skilled community is one of Ruby’s most notable advantages. You can always interact with the community to get help or to learn new things about Ruby and its ecosystem.
Here’s a brief list of resources that will help you interact with the community:
- Stackoverflow − a popular website where you can ask questions and get answers from Ruby developers
- GitHub − the world’s leading software development platform where you can not only download gems but also interact with fellow Ruby programmers
- Reddit − one more popular platform where you can keep track of the latest news in the world of Ruby
- Go Rails − informative paid screencasts about Ruby on Rails
- Ruby Tapas − short screencasts that help you pump up your Ruby development skills
- Drifting Ruby − paid tutorials about Ruby development
- The Bike Shed − listen to opinions and recommendations from renowned Ruby developers
- The Ruby Testing Podcast − learn more about testing in Ruby
Now you realize what’s so special about Ruby: it’s a language created to make developers happy. Moving from PHP to Ruby can be a great solution if you want to be productive and participate in interesting and challenging projects. If that’s what you want, then Ruby is the perfect choice.
If you want to stay updated on the latest news about Ruby, subscribe to our blog.
Subscribe via email and know it all first!