__FILE__ 在 Ruby 中是什么意思? [英] What does __FILE__ mean in Ruby?

查看:48
本文介绍了__FILE__ 在 Ruby 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Ruby 中看到这一点:

I see this all the time in Ruby:

require File.dirname(__FILE__) + "/../../config/environment"  

__FILE__ 是什么意思?

推荐答案

是对当前文件名的引用.在文件 foo.rb 中,__FILE__ 将被解释为 "foo.rb".

It is a reference to the current file name. In the file foo.rb, __FILE__ would be interpreted as "foo.rb".

Ruby 1.9.2 和 1.9.3 的行为似乎与 Luke Bayes 在 他的评论.使用这些文件:

Ruby 1.9.2 and 1.9.3 appear to behave a little differently from what Luke Bayes said in his comment. With these files:

# test.rb
puts __FILE__
require './dir2/test.rb'

# dir2/test.rb
puts __FILE__

运行ruby test.rb会输出

test.rb
/full/path/to/dir2/test.rb

这篇关于__FILE__ 在 Ruby 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆