Does anyone know enough about Ruby's require
to tell me if the following is valid syntax:
class Something
def initialize(mode)
case mode
when :one then require 'some_gem'
when :two then require 'other_gem'
end
end
end
s = Something.new
If so, will the require
place the gem into the global namespace as it would when at the top of the file?
When the import keyword is used as a function: const module = await import(path); It returns a promise and starts an asynchronous task to load the module. If the module was loaded successfully, then the promise resolves to the module content, otherwise, the promise rejects.
1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
Dynamic imports or Code Splitting is the practice of breaking up your JavaScript modules into smaller bundles and loading them dynamically at runtime to improve and increase the performance of your website dramatically.
If so, would the require place the gem into the global namespace as the same require at the top of the file would?
Yes. require
doesn't have scope, while load
does.
Yes it's perfectly valid and works as expected because require isn't scoped
Require pulls in the code from the specified file and attempts to use it in-place - that might mean that it isn't sensible to do but yes it can be done.
The local method scope would be unaffected and any class definition etc would be at the expected scope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With