Rails - Difference between @import and *= require?

Taylor Huston

So I've got a relatively simple Rails app, and I wanted to add some material design styling to it through Bootstrap.

I've added the following gems to my Gemfile:

gem 'bootstrap-sass'
gem 'bootstrap-material-design'

Now they both work, my question is to why I seem to have to add them to my app in different ways. For vanilla Boostrap I just import it into the view specific (I think that's the right term) scss file like normal.

@import "bootstrap-sprockets";
@import "bootstrap";

But for the Material Design gem I have to 'require' it to the root application.css file instead

 *= require bootstrap-material-design

Why the difference, and what is that require syntax actually doing?

Jérémy Buttice

The include and require methods do very different things.

The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method.

The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.

So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require.

Oddly enough, Ruby's require is analogous to C's include, while Ruby's include is almost nothing like C's include.

More info

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between Use, Require and Import

Elixir: Difference between require and import

What's the difference between loadfile, require, and import

The difference between "require(x)" and "import x"

Difference between use and post, require and import in Express

Difference between import http = require('http'); and import * as http from 'http';?

Difference between sprockets //=require and browserify-rails require()?

What is the difference between custom element and just an import using require in Aurelia

Laravel / Vue - App.js difference between import and require

difference between use and require

Relationship between import, require and /// <reference

OSGi: What's the difference between Import-Package/Export-Package and Require-Capability/Provide Capability?

Difference between import * as & import { default as }

What is the difference between require() and new require()?

Difference between require('mongoose').Mongoose and require('mongoose')

Difference between import and execfile

difference between import commands

Difference between #import and @class

Difference between import style

What is the difference between require() and library()?

Difference between "include" and "require" in php

What is the difference between require and assert?

Difference between 'in?' and 'include?' in Rails

What is the difference between :to and => in rails

Difference between <%= ... %> and <% ... %> Ruby on Rails

Freemarker: difference between include and import?

Difference between Update and Import in CloudFormation

What is the difference between these import statements?

difference between `include and import in SystemVerilog