[译] Ruby 2.6 Binding 对象增长 source_location 方法

本博客系列翻译自 Bigbinary 的 Ruby 2.6 系列, 已获得做者容许。Ruby 2.6.0-preview2 现已发布html

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 .git

Ruby 2.6 以前,若是咱们要知道源码对应的文件和位置。咱们须要使用 Binding#eval。github

binding.eval('[__FILE__, __LINE__]')
=> ["/Users/taha/blog/app/controllers/application_controller", 2]
复制代码

Ruby 2.6 在底层增长了一个更具备可读性的方法 Binding#source_location, 来获得相似的结果。 用法和效果和Method#source_location 是同样的。ruby

binding.source_location
=> ["/Users/taha/blog/app/controllers/application_controller", 2]
复制代码

这里是相关的commit讨论app

原文地址ui

博客地址spa

相关文章
相关标签/搜索