本博客系列翻译自 Bigbinary 的 Ruby 2.6 系列, 已得到作者允许。Ruby 2.6.0-preview2 现已发布。
Before Ruby 2.6, if we want to know file name with location and line number of source code, we would need to use Binding#eval .
Ruby 2.6 之前,如果我们要知道源码对应的文件和位置。我们需要使用 Binding#eval。
binding.eval('[__FILE__, __LINE__]')
=> ["/Users/taha/blog/app/controllers/application_controller", 2]
Ruby 2.6 在底层增加了一个更具有可读性的方法 Binding#source_location, 来得到类似的结果。 用法和效果和Method#source_location 是一样的。
binding.source_location
=> ["/Users/taha/blog/app/controllers/application_controller", 2]