RubyConf Taiwan 2023

Deep dive into Ruby require

#rubyconftw 2023

Deep dive into Ruby require

Since Ruby's bundled and default gems change every year with each release, some versions may suddenly happen LoadError at require when running bundle exec or bin/rails, for example matrix or net-smtp.

In this presentation, I will introduce the details of the functionality that extends Ruby's require to provide guidance to users on what they can do to load them. And I will also show how $LOAD_PATH is build behind Ruby and Rails by Bundler.

RubyConf Taiwan 2023

00:00:28.960 Welcome to the topic of 'Deep Dive into Ruby Require.' Please join me in welcoming our speaker, Hiroshi Shibata.
00:00:43.160 Hello! I'm happy to present at RubyConf Taiwan again. Today's topic is a deep dive into Ruby require.
00:00:52.239 I’m an open-source software developer at Anath and a proud member of Team Ruby and the Bundler team. I work on the Ruby programming language, the Ruby ecosystem, and the infrastructure of Ruby languages. Thank you for using Ruby and Ruby Gems.
00:01:08.200 As for my hobbies, I enjoy Ruby programming and game development.
00:01:15.159 Recently, I visited the National Parks Museum to see some famous exhibits, and I even got a little crush on something there.
00:01:21.880 Now, let me introduce Anath. Anath is a company that provides a SaaS platform for the construction industry, focusing on buildings, housing, etc. We develop many products to help everyone, whether in the office or outdoors.
00:01:34.920 Our goal is to enhance happiness through digitalization, and we utilize Ruby throughout our products, including customer management, sales management, and more in the Japanese construction industry.
00:01:47.640 The construction industry in Japan is valued at over 10 trillion New Taiwan Dollars, and we need to develop products more quickly with Ruby and Rails.
00:02:01.159 Anath strongly supports the Ruby ecosystem by hiring me as a full-time open-source developer for the Ruby language.
00:02:07.920 Let’s begin with the first topic of today's presentation: Ruby's require method.
00:02:14.640 We'll explore what Ruby requires and how its functionality extends.
00:02:21.760 The require method is one of the most recognized methods that allows additional Ruby code to be loaded into your context. For example, if you wish to use the RSS library, it can be easily loaded with require.
00:02:34.599 Require is a method that returns true if you successfully load a file. If it cannot find the file, it will search through your Ruby installation path.
00:02:43.480 You should note that there are additional methods in Ruby for specific platforms, such as ASO in Linux or DL in Windows. If you require libraries that utilize big decimal, for example, you would check their availability within the required paths.
00:02:56.640 While using require, it’s important to understand that Ruby also has a method called require_relative, which only looks in the current directory path.
00:03:10.159 This is different from require, which searches through the entire load path.
00:03:19.920 You need to include the file name with its extension when using load methods, which allows you to load Ruby code multiple times.
00:03:29.440 This differs from require, which only loads a file once.
00:03:36.800 If you want to load your code with some modifications in the same process or IRB context, the load methods can be quite useful.
00:03:48.520 Another interesting feature of Ruby is `autoload`. This feature only loads Ruby code when a constant is referenced.
00:04:00.680 Unlike require, which immediately evaluates code when defined, autoload defers loading until the constant is accessed.
00:04:11.800 This results in a faster boot time since it allows you to avoid loading files immediately.
00:04:18.960 It’s essential to balance boot time with execution speed, leading us to prefer require over autoload.
00:04:27.280 Now, we will discuss how to load external libraries from Ruby Gems.
00:04:34.960 Ruby has a library structure defined in Ruby Gems, which is crucial for installing many libraries.
00:04:41.360 The two critical parts of Ruby Gems to understand are the gemspec file and the specification class.
00:04:54.680 The gemspec file defines the gem's specifications, including the gem's name, version, and platform.
00:05:06.920 Ruby Gems extend the original Ruby require method for loading libraries with gem specifications. This extended require is a primary focus of our discussion.
00:05:21.160 Tomorrow, Sam, another member of the Ruby Gems and Bundler team, will introduce how Ruby Gems works.
00:05:28.720 To begin using the gemspec, you can access the gem.loaded_specs method in IRB at any time.
00:05:39.160 For example, the gemspec can reveal the specification of a Rock gem, including its dependencies.
00:05:50.320 Dependencies are crucial in this context and often consist of various libraries that support the application.
00:06:03.040 Ruby Gems only loads from the load path. However, it extended the capability to automatically search the load paths for dependencies that may not exist.
00:06:17.360 Extended require can slow things down due to its searching behavior, so we should be aware.
00:06:25.600 For instance, I experienced slower performance in my environment compared to another setup.
00:06:31.440 This was particularly evident when loading libraries across multiple Ruby versions due to the size of the gem directory.
00:06:47.200 What we’ve learned here is that using original require with specific gems significantly reduces loading time.
00:06:55.920 Using the G method, we can specify versions to improve performance.
00:07:02.720 This allows you to load specific versions of a library while managing dependencies effectively.
00:07:11.760 Thus, extended require makes searching more efficient and results in faster loading.
00:07:18.560 Next, let’s look into how Ruby Gems addresses some performance issues by optimizing gem management.
00:07:28.320 The flexibility of Ruby Gems allows for detailed management of dependencies, ensuring speed and efficiency in loading.
00:07:40.400 Incremental improvements in gem loading methods can greatly enhance the overall speed of Ruby applications.
00:07:56.160 Bandra effectively supplements Ruby Gems by providing a user-friendly interface for managing dependencies.
00:08:07.920 Tomorrow, we will discuss Bundler's versioning capabilities and integration with Ruby processes.
00:08:21.240 As we summarize, it's clear that a deeper understanding of require and Ruby Gems enhances performance.
00:08:32.320 In the next segment, we will tackle how to transition libraries effectively from Ruby Gems.
00:08:43.720 By specifying gem sources and requirements, you can generate a detailed lock file to manage your application dependencies.
00:08:55.920 Lock files provide essential information about which versions of gems to install, ensuring compatibility.
00:09:07.680 We will also cover how to overcome common pitfalls of dependency resolution when working with Bundler.
00:09:21.440 Understanding how these systems interact will help avoid troubles in future application deployments.
00:09:33.600 Moving on, we’ll discuss addressing the issues of conflicting gem versions.
00:09:42.639 By leveraging Bundler's functionalities, it is possible to maintain distinct environments for different applications.
00:09:56.159 Now let's review how Bundler can optimize gem loading and management in Ruby.
00:10:07.360 With a structured approach, Bundler provides options to streamline your process of managing dependencies effectively.
00:10:20.079 As we continue, it's vital to understand how to specify options in your configuration for optimal performance.
00:10:34.920 The steps outlined will enhance how we handle gem versions and dependencies throughout Ruby applications.
00:10:48.400 Lastly, workplace examples are instrumental in demonstrating what works best when deploying Ruby applications.
00:11:04.280 Concluding, we will reflect on the key takeaways regarding Bundler's role in ensuring integrity and consistency.
00:11:20.160 Ultimately, Ruby's evolution makes it critical to update both applications and processes to maintain performance standards.
00:11:31.760 Looking forward to the next Ruby version, anticipating how these changes will impact the ecosystem.
00:11:42.640 This session reveals how Bundler can lead to transformative changes within the Ruby community.
00:12:00.000 Thank you for your attention, and I look forward to further discussions!
00:12:11.840 Now, let's have a Q&A session. Please raise your hand if you have any questions.
00:12:19.440 Thank you for your insightful presentation, Hiroshi Shibata!
00:12:28.000 I appreciate the depth of information you shared regarding Ruby's require method and Ruby Gems.
00:12:41.760 My question pertains to the ongoing transition of default gems becoming bundled gems.
00:12:53.520 Is the direction aiming to have all default gems become bundled?
00:13:06.240 Hiroshi responded, explaining the plan to transition various gems into the Bundled category but clarifying that core standard libraries will stay separate like net/http.
00:13:16.320 Users should keep abreast of which gems are transitioning with each Ruby release.
00:13:32.640 Furthermore, Hiroshi assured that efforts will be made to ease the migration process but highlighted challenges that remain.
00:13:43.920 Another audience member asked if the Bandra library is implemented in C or Ruby.
00:13:55.840 Hiroshi confirmed that it is only implemented in Ruby.
00:14:05.680 Participants noted concerns about warnings during version upgrades.
00:14:15.360 An interesting problem surfaces regarding how the warning mechanisms function between Ruby and J Ruby.
00:14:26.400 Hiroshi reassured everyone that new features would be highlighted within the documentation as improvements continue.
00:14:37.520 He encouraged contributions to Ruby's evolving ecosystem to highlight the collaborative efforts behind Ruby.”},{