Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby, Source Code of Splat?

Tags:

ruby

splat

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?

like image 610
Dan Rosenstark Avatar asked May 28 '09 15:05

Dan Rosenstark


2 Answers

Some quick Google searching turned up that it's implemented in eval.c. You can find references to "splat" in a few places in the file, but I'm not familiar enough with the inner workings of Ruby to make any sense of it.

like image 178
Martin Gordon Avatar answered Oct 19 '22 04:10

Martin Gordon


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

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
like image 28
anothermh Avatar answered Oct 19 '22 05:10

anothermh