Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
140: Asset Pipeline vs Import Maps
I've been staring at a Rails project from a few years ago, and I'm reminded of why I used to dread adding a single JavaScript library. I wanted to add a simple data visualization to a dashboard, and suddenly I was diving into a rabbit hole of yarn install, node_modules taking up 400MB of disk space, and the dreaded "Compilation failed" error during deployment. This is the world of the traditional Asset Pipeline (Sprockets) and its successor, Webpacker.
The friction of the "Build Step"
Let's look at how we used to do it. If I wanted to use Chart.js in the old pipeline, I'd have to install it via npm or yarn, import it into a application.js file, and then rely on a bundler to minify, mangle, and concatenate everything into one giant blob of JS that the browser could understand.
# The old flow (simplified)
yarn add chart.js
// in application.js
import Chart from 'chart.js/auto';
It works, but there's a mental tax. Every time I change a line of JS, I'm waiting for a watcher process to recompile. If the watcher crashes—which it often did—I'm left wondering why my changes aren't appearing in the browser. I'm essentially running a Node.js project inside a Ruby project. It feels like two different worlds fighting for control of the public/assets folder.
Trying a different path with Import Maps
I decided to start a fresh Rails 7 app to see if the new "Import Map" approach actually removes that friction. I'll try to get that same Chart.js functionality working, but this time, I'm not touching Node.js. Not once.
First, I run the pin command:
bin/importmap pin chart.js
When I run this, Rails doesn't download a package to my hard drive. Instead, it adds a line to config/importmap.rb. I opened the file and saw this:
pin "chart.js", to: "https://ga.jspm.io/npm:chart.js@4.4.1/dist/chart.umd.js"
This is the "Aha!" moment. I'm not bundling. I'm just telling the browser: "When I say import 'chart.js', go fetch it from this specific URL." The browser handles the loading natively using ES modules. I don't need a compiler, I don't need node_modules, and I certainly don't need to wait for a build step to finish before I refresh the page.
Testing the execution
Now I'll try to actually use it in my view. I'll create a small canvas element and a script tag to initialize the chart.
<canvas id="myChart"></canvas>
<script type="module">
import Chart from 'chart.js'
const ctx = document.getElementById('myChart');
new Chart(ctx, {
type: 'bar',
data: { labels: ['Red', 'Blue'], datasets: [{ data: [12, 19] }] }
});
</script>
I refresh the page. It just... works. No webpacker:compile, no yarn errors. The browser requested the JS file directly from the CDN (or the local proxy), executed it, and rendered the chart. The development loop is now instantaneous.
Where the trade-off hits
Now, I have to be honest: Import Maps aren't a magic bullet for every single project. As I started playing with more complex ideas, I hit a wall. I wanted to use JSX for a small component. I tried to pin a React component, but the browser gave me a syntax error.
Why? Because JSX isn't valid JavaScript. Browsers can't read it. To use JSX, you must transpile it into standard JS using something like Babel. Import Maps skip the transpilation step entirely.
So, here is the mental model I've settled on: If you are writing standard modern JavaScript (ES6+), Import Maps are a godsend. They strip away the complexity of the Node ecosystem. But the moment you need "non-standard" JS—like TypeScript or JSX—you have to go back to a bundler (like esbuild or Webpacker) because you need a tool to translate that code before the browser ever sees it.
📋 Practical Task
Implementing a Live Weather Widget with Import Maps
Your goal is to integrate the confetti-js library into a Rails application using Import Maps to celebrate when a user "checks in" to a location.
- Step 1: Use the
bin/importmap pincommand to add theconfetti-jslibrary to your application. - Step 2: In your
app/javascript/application.js, import the library and create a function calledtriggerCelebration()that initializes a confetti burst on the screen. - Step 3: Create a simple view with a button labeled "Check In". Use a
data-controlleror a simpleonclickattribute to call yourtriggerCelebration()function. - Step 4: Verify that the confetti triggers without any Node.js build processes running in the background.
There are no comments for now.