سؤال
كيف أحصل على الدليل حيث يقع Rakefile.rb؟
أريد استخدام هذا كدليل الجذر الخاص بي لتحديد كل شيء.
المحلول
use __FILE__
to get the file name then you can get the directory from there:
in test.rb
puts __FILE__
output:
/users/foo/test.rb
__FILE__
resolves to the full path of the file it is in.
Use this to get the dir name:
File.dirname(__FILE__)
نصائح أخرى
You can get it by calling application.original_dir method. In task you can achieve application object using application method on task object.
لماذا لا تستخدم dir.pwd فقط
?
إذا كان هذا تطبيقًا ROR ، فيجب أن يكون RakeFile.rb في دليل Rails_Root الخاص بك. لذلك في أي برنامج نصي ، يمكنك تحديد موقع الملف مثل
config.load_paths += %W( #{RAILS_ROOT}/extras )
اعتبارا من روبي 2 يمكنك استخدام __dir__
بدلاً من File.dirname(__FILE__)
للحصول على الدليل الذي يحتوي على البرنامج النصي الحالي.
لا تنتمي إلى StackOverflow