Talks

Dissecting Rails - A Different Approach to Learning Rails

A talk from RubyConfTH, held in Bangkok, Thailand on December 9-10, 2022.

Find out more and register for updates for our 2023 conference at https://rubyconfth.com/

RubyConfTH 2022 videos are presented by Cloud 66. https://cloud66.com

RubyConf TH 2022

00:00:15.059 We just have about 200 slides to go, so let's dive in. That's me, RTDP on Twitter. As I was introduced, I'm also a digital nomad. Chiang Mai was pretty high on my list, and I never got to visit, but next week it's going to happen, so I'm pretty excited.
00:00:26.720 When I travel and work, I do things like going to 18,000 feet somewhere in the Himalayas and writing Ruby from there, claiming it to be the highest altitude Ruby commit. Right? The commits you make during a flight don't count, that's just mocking.
00:00:41.520 Some of the places I call my office while traveling provide stunning views, so it’s good. If you’re also into travel and being a digital nomad, let’s talk. Another side project of mine is WhyILoveRuby.com. It allows you to log in and share why you love Ruby. As you might guess, the most reported answer there is the community.
00:01:11.820 If you love Ruby as well, head over to I Love Ruby and add your reasons for loving it. I quickly want to discuss another project I have called Ruby Vernac, which allows you to write Ruby code in non-English languages. I feel this makes a lot of sense for people who are not native English speakers, which is about 95% of the world.
00:01:40.560 For them, coding becomes much more meaningful if it can be done in their own languages. Here’s an example I created in Thai language. Obviously, I don’t know Thai, so this depends on Google Translate, but it talks about my favorite breakfast, poha, which I can eat every day. However, there was a mix-up in translation, and it ended up saying 'upma', so something gets lost in that translation.
00:02:33.600 I worked with a couple of other developers here to create accurate translations for the Thai language, and I’m proud to say the translations are working well. You can create similar programs in other languages too. I heard there are a few developers here from Vietnam—let’s meet up! If this interests you and you think it can be useful for teaching or as an exercise, it would be a lot of fun.
00:03:01.680 For example, the keyword 'class' has been translated here, and if you simply use Google Translate or direct translations, it might not make much sense in your language. You need to think deeply about why these keywords are used as they are in English, take that meaning, convert it to your language, explore your options, and pick something meaningful.
00:03:39.360 Even if you are already familiar with Ruby but are a non-native English speaker, this is a great exercise. Create a couple of programs in your language and see how they turn out. Last week, at RubyConf Houston, someone suggested, if you can do this, why not using emojis? So, there’s also a Ruby Vernac emoji, where the code is written entirely in emojis, and this is a working code. Why not?
00:04:10.260 So first, let’s understand the problems with learning Rails. How many developers here have less than one year of experience? (Few hands are raised.) How about three years or less than three years? (A couple of more hands raised.) It’s been almost 18 years since Rails was published, back in August 2004, and since then, it has grown tremendously.
00:04:42.960 For someone who got into Rails back in 2009 and 2010, compared to 2015, 2016, and now in 2022, the experience has changed significantly because the surface area of the API and the features it provides have increased tremendously. When I started with Rails back in 2009 and 2010, it felt quite magical primarily due to the extensive meta-programming and other elements present in Rails and Ruby, which have only expanded since.
00:05:45.300 This creates problems for newcomers. One common issue is that many people don’t understand where Ruby stops and Rails starts. There are developers who, while proficient in Rails, struggle when asked to write simple Ruby scripts to perform certain tasks without the context of Rails. These struggles often arise when trying to use something like a hash with indifferent access or relying on Rails-specific methods that don’t work outside the Rails ecosystem.
00:06:27.600 Another problem that many junior Rails developers encounter during their training is grasping how Rails is modular—understanding which modules are independent from each other and the boundaries between them. If one is using a function, you should know which library from Rails is providing that function instead of viewing it as a monolithic entity.
00:07:00.300 Mid-level developers also experience challenges, especially during their progression. Transitioning from zero to one is generally easy, but moving from one to two presents difficulties. For those unfamiliar with Ruby and Rails, getting started with the basics is straightforward. But as you progress to lead a team, design better architectures, or handle scalability challenges, it becomes more complex.
00:07:39.060 Many developers find that as they scale applications or work on projects that defy Rails conventions, they need to explore Ruby and its features more deeply. For instance, developing applications without a database or simply performing scripts can often push them outside the Rails comfort zone.
00:08:15.080 With the 'why' out of the way, let's talk about 'how' we are going to achieve this. I want to start by discussing how learning should begin with either Ruby or Ruby on Rails. In my experience, I’ve noticed several companies in India that teach Rails directly without a foundational understanding of Ruby, which is not an effective approach.
00:08:53.460 Even with some knowledge of Ruby, starting directly with Rails can be a challenge due to the sheer volume of concepts to learn. Without sufficient Ruby background, it’s akin to stepping into a magical environment without a reference point, which is crucial for understanding the framework and its functionalities.
00:09:39.800 Once you begin learning Rails, I suggest starting with familiarizing yourself with the tooling in the ecosystem first. Begin breaking down Rails features from applications before diving into core concepts, ultimately summarizing everything at the end.
00:10:24.360 Let's start with Bundler. If you're starting with programming languages these days, particularly from 2015 onwards, you may take tools like Bundler and other package managers for granted. However, in 2009, 10, or 11, there were no such tools, and one had to manage dependencies manually.
00:10:51.660 It was a tedious process, often requiring days to set up everything. I recommend sometimes building a basic application without Bundler. While it can be manageable for a small project, you'll quickly realize its importance when trying to scale.
00:11:22.560 Moving on to Rake, during my experience, many developers use Rake tasks in Rails but consider them as part of Rails itself rather than separate tools. Recently, older Rake commands have been renamed and integrated into Rails commands, which may obscure their independence from the Rails framework.
00:12:00.720 I believe it's important to understand Rake by building applications or scripts outside of Rails. This is often one of the initial steps towards moving away from a purely magical perception of Rails.
00:12:37.680 Next, let’s discuss Rack. I find it to be an incredibly simple and beautiful application. If you appreciate simplicity and minimalism, Rack is a gem worth exploring. Additionally, frameworks such as Sinatra, Roda, and Hanami showcase this simplicity. A hands-on experience with these frameworks will help clarify how Rack operates and its common use across various Ruby web frameworks.
00:13:22.320 To illustrate, think of the difference between mechanical motorcycles and today's electric ones. For example, a Royal Enfield motorcycle is relatively unsophisticated, allowing you to easily perform repairs at home. In contrast, more advanced bikes like Hondas require specialized electronics to diagnose issues, which hinders your ability to repair them yourself.
00:14:02.700 This analogy showcases how understanding simpler frameworks compared to Rails enables a deeper insight into the underlying mechanisms of programming.
00:14:56.500 Moving on to Active Support—this component can be both loved and hated in the Rails community. It adds numerous handy methods, such as user.first or 10.minutes.ago, which enhance developer productivity. But developers sometimes express concerns over losing code purity, as libraries extend the core classes through meta-programming, producing a larger API surface for users.
00:15:45.660 Speaking of controversy, there was a method called array.42 in Rails. At one point, there was a big debate surrounding it, which ultimately led to its removal. However, Rails still kept this method for nostalgia's sake! A few other useful methods worth mentioning include string.camelize, pluralize, singularize, and titleize.
00:16:18.420 Another interesting feature is hash with indifferent access, which allows you to access keys using either a symbol or a string. This can be convenient in Rails, but it can cause confusion, as many newcomers might wonder why that style doesn't apply when working outside of Rails.
00:17:07.740 Next, we can discuss Active Record. Here is a simple code demonstrating how you can utilize Active Record outside of Rails. You begin by requiring Active Record and establishing a connection to the database.
00:17:58.680 With this connection, you define your models and can run typical commands such as create, count, and where. The purpose of this example is to highlight how easy it is to leverage Active Record in Ruby applications without relying on Rails.
00:18:41.400 The use cases for Active Record are numerous; for instance, you could retrieve data from a database, manipulate it, or even move data between staging and production environments while anonymizing it.
00:19:13.140 Next, let’s address Active Model. The principal difference between Active Record and Active Model lies in database connectivity. While Active Record interacts with the database, Active Model can function without needing a database.
00:19:57.540 With Active Model, you can simply include the API in your classes, allowing you to leverage validations and attribute accessors even without the database.
00:20:48.420 Several examples show how Active Model has similar callbacks like Active Record, including dirty tracking for monitoring saved changes or validates for handling input validation. Additionally, translations for localization support and the ability to serialize data into JSON or hashes are beneficial features for any Ruby application.
00:21:58.160 Next, we have Action Mailer, which allows you to set up email sending functionality in your Ruby scripts. With just a few lines of configuration, you can create a mailer class to handle email sending using both text and HTML formats directly from your Ruby code. You can utilize Action Mailer templates to structure your emails neatly.
00:22:43.920 It’s also common for developers to leverage Action Mailer in scripts deployed on platforms like AWS Lambda to send automated emails, even if their primary application doesn't use Rails.
00:23:22.680 Next, there's Active Job, which simplifies job processing with a minimal amount of code, making it easy to begin using it in your projects.
00:24:06.240 However, components like Action Cable prove more complex for outside use due to their extensive dependencies. It often becomes easier to demonstrate these components within the context of a Rails application.
00:24:59.500 Thus, focusing on core ideas and concepts within Rails development is beneficial for understanding its practices.
00:25:10.310 Let's briefly summarize the key concepts we’ve explored regarding Rails tools and libraries. We discussed Bundler, Rake, and Rack as external functionalities, while inside of Rails, we've looked at Active Support, Active Record, and Action Mailer.
00:25:40.280 We also highlighted important topics for Ruby developers to focus on, including Ruby DSL, security in web applications, routing, templating, and more.
00:26:06.450 If you want to engage further, there are some experienced people here at the conference working on similar ideas—like Nova, who is involved in rebuilding Rails; Tim and Jeremy, who are working with Roda and Hanami. Engage with them.
00:26:34.900 Lastly, I'd like to express my gratitude to all the contributors collaborating on creating these examples and resources. Thank you very much.