In Ruby, the following code obtains the absolute path of the current Script: eiztz5 [0
51testing Software Testing Network + r1oc, tlt
Require 'pathname' puts pathname. New (_ file _). realpath
N: ^ qsxd
? /M0
51testing software testing network ugfb + E) |
Save the above Code in test1.rb and put it in the root directory of drive D. During the test, we enter drive C and execute Ruby D:/test1.rb. The console will output the absolute path of the current Script: D:/test1.rb.
Rp0_bd + n {(h0
51testing Software Testing Network + a1pog; we ~ Nu
Someone may ask, is it possible to obtain dir. pwd? Actually, it is not feasible. Dir. pwd obtains the working directory. The working directory that I understand is the Ruby call directory. That is to say, the directory in which you get the called script, rather than the directory in which the script is located. We can do the following experiment, first create a test2.rb file, and enter the code as follows: 51testing software testing network f [�m ^ FX ^ 5njf % I
51testing software testing network ~ # Su {o "t) j
Puts dir. pwd51testing software testing network 0] RQ (E ,? X
# U % Y. vbq0
Warranty
Save the file to the root directory of drive D, and then run the CMD command line to access drive C. (Note that drive C is used !), Then execute: Ruby
Test2.rb, what do you think it will output? Is it D? Error: C :/
! For more information, go to the C:/Windows/system directory under the CMD command line and execute Ruby
D:/test2.rb. The output result is C:/Windows/system. This is clear. In which directory do you call the script, dir. pwd returns
Directory, but the actual path of the script is irrelevant. The official API writes a back-to-work Directory, which is easily understood as an absolute path. 51testing software testing network U: pux! B % Ji) _ RQ
51testing software testing network _ '4w) RL & R & v | GZ
Other methods include: 51testing software testing network XK r5a3 @ LG
51testing software testing network 'dmdq: E
O
~
Get the file name of the current execution file:
8 aeba % h4c)?] /Q0
51testing software testing network % rqjvma. h
_ File __
8lg;} 7'j: e6n0
+ {2 ^) okem0
Obtain the directory of the current file
1 mkdum/uz4.7/K0
Urba
N
BH "I0
File. dirname (_ file _) 51testing software testing network ykm} _ 4s_5gk7z
51testing software testing network] ~ C/z2am @/
Obtain the complete path of the current execution file: 51testing Software Testing Network A & oo # RQ & | S @ r
* Wv_ B] O0
Require 'pathname'
S/J $] B0
Pathname. New (_ file _). realpath51testing 6q [tu8 ^ & ^; U ~ : _ Y
51testing software testing network e3ve. Nu ^
Obtain the full directory path of the current execution file: 51testing software testing network gy7gv "? + S "| 3I
51testing software testing network; nu a. DTS; ne?
Require 'pathname'
Pa} 2 @ Ms 'l0
Pathname. New (file. dirname (_ file _). realpath