.

Thursday, September 3, 2020

The Require Method in Ruby

The Require Method in Ruby So as to make reusable parts, ones that can be effectively utilized in different projects, a programming language must have some method of easily bringing in that code at run-time. In Ruby, the require strategy is utilized to stack another record and execute every one of its announcements. This serves to import all class and technique definitions in the document. Notwithstanding basically executing the entirety of the announcements in the document, the require strategy likewise monitors which records have been recently required and, consequently, won't require a record twice. Utilizing the 'require' Method The require technique takes the name of the document to require, as a string, as a solitary contention. This can either be a way to the document, for example, ./lib/some_library.rb or an abbreviated name, for example, some_library. In the event that the contention is a way and complete filename, the require technique will look there for the document. In any case, if the contention is an abbreviated name, the require technique will look through various pre-characterized catalogs on your framework for that document. Utilizing the abbreviated name is the most widely recognized method of utilizing the require technique. The accompanying model shows how to utilize the require proclamation. The record test_library.rb is in the primary code square. This document prints a message and characterizes another class. The subsequent code square is the document test_program.rb. This document stacks the test_library.rb record utilizing the require strategy and makes another TestClass object. puts test_library includedclass TestClassdef initializeputs TestClass object createdendend #!/usr/canister/env rubyrequire test_library.rbt TestClass.new Keep away from Name Clashes When composing reusable parts, its best not to pronounce numerous factors in the worldwide extension outside any classes or techniques or by utilizing the $ prefix. This is to forestall something many refer to as namespace contamination. On the off chance that you announce an excessive number of names, another program or library may pronounce a similar name and cause a name conflict. At the point when two totally disconnected libraries begin changing every others factors inadvertently, things will break apparently at irregular. This is an extremely troublesome bug to find and its best just to maintain a strategic distance from it. To maintain a strategic distance from name conflicts, you can encase everything in your library within a module proclamation. This will expect individuals to allude to your classes and technique by a completely qualified name, for example, MyLibrary::my_method, however its justified, despite all the trouble since name conflicts for the most part wont happen. For individuals who need to have the entirety of your group and technique names in the worldwide extension, they can do that utilizing the incorporate proclamation. The accompanying model rehashes the past model yet encases everything in a MyLibrary module. Two variants of my_program.rb are given; one that utilizes the incorporate articulation and one that doesn't. puts test_library includedmodule MyLibraryclass TestClassdef initializeputs TestClass object createdendendend #!/usr/receptacle/env rubyrequire test_library2.rbt MyLibrary::TestClass.new #!/usr/receptacle/env rubyrequire test_library2.rbinclude MyLibraryt TestClass.new Maintain a strategic distance from Absolute Paths Since reusable segments regularly get moved around, its additionally best not to utilize outright ways in your require calls. A flat out way is a way like/home/client/code/library.rb. Youll notice that the document must be in that careful area so as to work. In the event that the content is ever moved or your home index ever changes, that require explanation will quit working. Rather than total ways, its regularly basic to make a ./lib catalog in your Ruby projects index. The ./lib index is added to the $LOAD_PATH variable which stores the catalogs wherein the require technique scans for Ruby documents. From that point onward, if the record my_library.rb is put away in the lib catalog, it very well may be stacked into your program with a basic require my_library articulation. The accompanying model is equivalent to the past test_program.rb models. In any case, it expect the test_library.rb document is put away in the ./lib index and loads it utilizing the strategy portrayed previously. #!/usr/canister/env ruby$LOAD_PATH ./librequire test_library.rbt TestClass.new