## 获取当前的文件名称 puts "__FILE__ == #{__FILE__}" Result: __FILE__ == test01/motorcycletest.rb ## 获取当前文件的目录名称 puts "File.dirname(__FILE__) == #{File.dirname(__FILE__)}" Result: File.dirname(__FILE__) == test01 ## 获取当前文件的完整名称 当要获取完整的路径时需要require 'pathname',代码如下: require ‘pathname’ puts "Pathname.new(__FILE__).realpath == #{Pathname.new(__FILE__).realpath}" Result: Pathname.new(__FILE__).realpath == E:/workspace_ruby/test/test01/motorcycletest.rb ## 获取当前文件的完整目录 require ‘pathname’ puts "Pathname.new(File.dirname(__FILE__)).realpath == #{Pathname.new(File.dirname(__FILE__)).realpath}" Result: Pathname.new(File.dirname(__FILE__)).realpath == E:/workspace_ruby/test/test01 ## 输出当前引用的路径: puts "$: == #{$:}" $: == ["E:/workspace_ruby/.metadata/.plugins/org.rubypeople.rdt.launching/ruby/flush", "d:/Ruby192/lib/ruby/site_ruby/1.9.1", "d:/Ruby192/lib/ruby/site_ruby/1.9.1/i386-msvcrt", "d:/Ruby192/lib/ruby/site_ruby", "d:/Ruby192/lib/ruby/vendor_ruby/1.9.1", "d:/Ruby192/lib/ruby/vendor_ruby/1.9.1/i386-msvcrt", "d:/Ruby192/lib/ruby/vendor_ruby", "d:/Ruby192/lib/ruby/1.9.1", "d:/Ruby192/lib/ruby/1.9.1/i386-mingw32"]