Talks

Summarized using AI

Ruby on Rails Hacking Guide

Akira Matsuda • April 22, 2014 • Chicago, IL

The video titled "Ruby on Rails Hacking Guide" presented by Akira Matsuda at RailsConf 2014 explores the internal workings of the Ruby on Rails framework. The talk emphasizes the importance of understanding Rails for effective programming by encouraging developers to read and comprehend the framework’s code. Matsuda identifies three key reasons for this:

  • Skill Development: To become proficient Rails programmers, developers need to read extensively, understanding all parts of their applications and not leaving any code as a 'black box' of mystery.
  • Debugging and Maintenance: Because Rails has various bugs, being familiar with its codebase is essential for resolving issues and maintaining applications effectively.
  • Open Source Accessibility: Rails is entirely written in Ruby, making it accessible for anyone to read and contribute to its code.

The talk outlines how Rails initializes and loads apps, handling HTTP requests and the interaction with databases, with a focus on understanding key components like:
- Rails Ties: Matsuda explains that Railties serves as a crucial library underpinning the Rails framework, connecting various components like ActionPack and ActiveRecord.
- Booting Rails: Details on how the Rails server boots, starting with the rails server command, demonstrate the flow from requiring essential files to initializing the application context.
- Request Handling: A breakdown of how Rails processes incoming HTTP requests, specifically how the routing mechanism interacts with controllers to match requests with actions in the application.

Matsuda also shares practical tips for following the code execution flow, such as using puts statements for debugging. The video concludes with an announcement of Matsuda's plans to write a book on this subject, aiming to translate complex Rails internals into a more accessible format for developers.

Overall, the video serves as a comprehensive guide to understanding Ruby on Rails at a deeper level, emphasizing the importance of code literacy for software developers.

Ruby on Rails Hacking Guide
Akira Matsuda • April 22, 2014 • Chicago, IL

Rails is a platform to serve your Ruby applications, and yet is a very well crafted open-source product written in the language that composes your apps. So, if you're curious to know how things work underneath your app, you can read everything!

This talk will be a good guide to step into the Rails internal. We'll cover these topics:

How Rails loads itself, libraries, and application when booting
How Rails deals with HTTP requests and databases
How to debug, patch, and extend Rails

Rails committer, Ruby committer, Haml committer, creator of widely used Rails plugins such as Kaminari, activedecorator, actionargs, html5validators, erd, databaserewinder, etc. Founder of "Asakusa.rb", the most active Ruby community in Japan.

Help us caption & translate this video!

http://amara.org/v/FG1P/

RailsConf 2014

00:00:17.790 Welcome to the Ruby on Rails internal talk. This is titled the 'Ruby on Rails Hacking Guide,' which is deeply inspired by the book 'Ruby Hacking Guide.'
00:00:23.350 Before we get started, let me introduce myself. I'm Akira Matsuda, known on GitHub and Twitter as Matsuda. I'm a committer on Ruby and Ruby on Rails projects, and I'm from Japan. As an individual on GitHub, I created several libraries such as Kaminari, ActiveDecorator, ActionArgs, and more. I've made a total of 374 commits since my first commit six years ago.
00:00:47.589 Initially, I titled this talk 'Rails Second Guide,' but I changed my mind after attending DHH's keynote. I believe we should actually call this talk the 'Ruby on Rails Reading Guide' because we are going to spend the entire 40 minutes reading the framework code. I won't be discussing technical specifications; instead, it's like reading a novel or a story.
00:01:35.770 So, why should we read Rails? I believe there are three big reasons. First, as DHH defined, we are software writers. To be good writers, we need to read a lot—and this applies to Rails programming too. If you don't understand any piece of code in your application, it indicates that you don't really understand your application.
00:01:55.210 Don't let any magical piece of code exist in your app without understanding it. Rails applications are often filled with such magic. For example, a simple one-liner of Ruby code can perform a lot of actions, but we need to be able to explain why a specific controller inherits from the application controller or how an HTTP request calls a Ruby method.
00:02:31.520 Think about Rails DSL. It may seem straightforward for new readers as documentation, but what happens when you are the one writing it? A writer should ideally only write what they understand. Consider this: if you come across a line like 'Rails.application.routes.draw'—do you know what it does? Can you explain it? If not, it means that for you, it’s just magic. That's why you're here, to learn about Rails.
00:03:24.570 Another reason to read Rails code is that Rails has its share of bugs. As I mentioned, I have patched many issues, mostly because I dislike bugs in my production applications. I'm sure many of you share this experience; Rails can be quite buggy, and as Rails programmers, we not only need to maintain our applications but also the Rails framework. Familiarity with the codebase becomes essential. Interestingly, this talk is categorized as a novice talk, but I aim to delve into Rails internals.
00:05:27.590 Let's begin by exploring the internals. Ruby on Rails is open-source software available on GitHub. Notably, there is no C extension within Rails—everything is written in Ruby, meaning that if you are a true programmer, you can read and write it as necessary. You can check it out on GitHub.
00:06:05.240 I have a small tip for finding the GitHub repository of any gem you use. There's a gem called 'gem-soros' that you can install, which will automatically clone the source from GitHub repositories of gems you use. This way, you don't need to memorize authors' names or search extensively—just install the gem and the code will be available for you.
00:07:51.360 Now, let’s return to the Rails source. To know what's included in the gem, take a look at the gem specification. Typically, it defines the files included, and you can find a bunch of dependencies listed. This means that Rails is a meta-gem, depending on several other gems, and these are all included in the Rails repository.
00:09:17.840 Rails is a full-stack MVC framework, comprising Model, View, and Controller, all in one repository. The active record, action pack, and action view gems represent these components of Rails. Therefore, Rails itself is a meta-package encompassing these seven gems.
00:10:54.200 Next, let's take a look at how the Rails server command works and how Rails boots. Starting the Rails server requires running the 'rails server' command. But what happens internally? When you execute this command, it actually runs 'bin/rails' located in your application.
00:11:27.410 This sequentially executes the configuration boot within your application and requires the Rails commands. Rails boot essentially bundles several setups, which prepares the load path correctly.
00:12:47.950 The Rails directory you are looking for can be found in the Rails 'rail ties' gem, a tiny library initially crafted by Yehuda Katz. This gem is crucial for Rails; it includes the libraries and Ruby files necessary for starting Rails.
00:14:49.900 Inside the 'rail ties' lib directory, you'll see the various components and files necessary for Rails to function. Essentially, Rails commands are invoked through this setup.
00:16:19.500 Next, we explore the concept of Rails engine. The Rails engine is important as it utilizes 'rail ties' and serves as a core component of Rails, providing methods and setups for the application.
00:17:36.530 The Rails application structure requires some essential classes—such as application, engine, and railtie. These are core components and are pivotal for Rails.
00:19:09.400 When we kick off the Rails server, it ultimately runs the Rails application, which should be seen as a Rack application. This means that Rails responds to the Rack specification, which is fundamental for its operation.
00:20:51.610 Every time a user interacts with a Rails application, a request is handled through the Rack system, where Rails must interact with this defined architecture effectively.
00:22:27.300 Think of it this way: within your Rails application, every request equates to Ruby method calls that ultimately direct operations to necessary controllers and actions based on the routing defined in your application.
00:24:28.600 For instance, if a user sends a request to 'get hello fubar,' Rails will route this request to the appropriate action and execute it. Ultimately, the goal is to convert this request into a processed response.
00:26:01.750 As we wrap up, it's evident that my time is limited, and there is a wealth of content regarding Rails internals that we couldn't explore in full depth. However, my aim was to provide a glimpse into the essentials.
00:27:17.090 Before I conclude, I’m considering writing a book titled 'Ruby on Rails Hacking Guide,' which will be focused on these internals. I plan to write in Japanese first and then translate it into English, so keep an eye out for it next year. I appreciate your attention and hope you enjoyed this talk!
00:30:07.960 Thank you very much.
Explore all talks recorded at RailsConf 2014
+133