.

Thursday, September 3, 2020

The Require Method in Ruby

The Require Method in Ruby So as to make reusable segments, ones that can be effortlessly 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 just executing the entirety of the announcements in the document, the require technique likewise monitors which records have been recently required and, in this way, won't require a record twice. Utilizing the 'require' Method The require strategy 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 strategy 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 indexes on your framework for that document. Utilizing the abbreviated name is the most widely recognized method of utilizing the require strategy. 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 record test_program.rb. This document stacks the test_library.rb record utilizing the require technique and makes another TestClass object. puts test_library includedclass TestClassdef initializeputs TestClass object createdendend #!/usr/canister/env rubyrequire test_library.rbt TestClass.new Evade Name Clashes When composing reusable parts, its best not to proclaim numerous factors in the worldwide extension outside any classes or strategies or by utilizing the $ prefix. This is to forestall something many refer to as namespace contamination. In the event that you pronounce such a large number of names, another program or library may announce a similar name and cause a name conflict. At the point when two totally random libraries begin changing every others factors incidentally, things will break apparently at arbitrary. This is a 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 strategy by a completely qualified name, for example, MyLibrary::my_method, yet 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 explanation. The accompanying model rehashes the past model however encases everything in a MyLibrary module. Two variants of my_program.rb are given; one that utilizes the incorporate proclamation 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 frequently 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 record must be in that precise area so as to work. On the off chance that the content is ever moved or your home registry ever changes, that require proclamation will quit working. Rather than total ways, its regularly normal to make a ./lib index in your Ruby projects catalog. The ./lib catalog is added to the $LOAD_PATH variable which stores the indexes in which the require strategy looks for Ruby records. From that point onward, if the document my_library.rb is put away in the lib index, it tends to be stacked into your program with a basic require my_library proclamation. The accompanying model is equivalent to the past test_program.rb models. Notwithstanding, it accept the test_library.rb document is put away in the ./lib catalog and loads it utilizing the strategy depicted previously. #!/usr/container/env ruby$LOAD_PATH ./librequire test_library.rbt TestClass.new

No comments:

Post a Comment