Ruby,Splat 的源代码? [英] Ruby, Source Code of Splat?

查看:39
本文介绍了Ruby,Splat 的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天有人问了关于 splat 运算符的问题,我想看看源代码……是用 C 还是用 Ruby 编写的?在哪里可以找到?

Someone asked about the splat operator yesterday, and I wanted to see the source code... would that be written in C or in Ruby? Where would it be found?

推荐答案

从 Ruby 2.4 开始,核心 Ruby 文档中对 splat 运算符的记录很差.不过,这是该语言的核心功能,可以在 rb_yield_splat(VALUE values).

The splat operator is poorly documented in the core Ruby documentation as of Ruby 2.4. It's a core feature of the language, though, and the source code for the splat operator can be found in vm_eval.c under rb_yield_splat(VALUE values).

对于 rb_yield_splat 更清楚地说明发生了什么:

The unit test for rb_yield_splat makes it clearer what is happening:

it "yields with passed array's contents" do
  ret = nil
  @s.rb_yield_splat([1, 2]) { |x, y| ret = x + y }
  ret.should == 3
end

这篇关于Ruby,Splat 的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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