Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: how to splice an array into a list Lisp-style?

Tags:

ruby

lisp

This is something I find myself wanting to do occasionally. Say I have a list of arguments. In Lisp, I can go like

`(imaginary-function ,@args)

in order to call the function with the array turned from one element into the right number of arguments.

Is there similar functionality in Ruby? Or am I just using a completely wrong idiom here?

like image 310
TG-T Avatar asked Aug 29 '12 15:08

TG-T


2 Answers

Yes! It's called the splat operator.

a = [1, 44]
p(*a)
like image 174
David Grayson Avatar answered Nov 11 '22 13:11

David Grayson


This is the splat operator: function(*list)

like image 26
dfb Avatar answered Nov 11 '22 13:11

dfb