Questions tagged [splat]

The `*` operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

The * operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

166 questions
13
votes
1 answer

What is the difference between these two Ruby symbols?

I discovered this after playing around with object ids. ObjectSpace._id2ref(2648) => :** ObjectSpace._id2ref(6688) => :** ObjectSpace._id2ref(2648) == ObjectSpace._id2ref(6688) => false The first one is the symbol for the exponentiation…
Alex Altair
  • 3,246
  • 3
  • 21
  • 37
13
votes
4 answers

How to set a default value for a splat argument in Ruby

Setting a default value for a splat argument gives an error: 1.9.3-p374 :001 > def a b, *c = nil 1.9.3-p374 :002?> end SyntaxError: (irb):1: syntax error, unexpected '=', expecting ';' or '\n' def a b, *c = nil ^ from…
jordanpg
  • 6,386
  • 4
  • 46
  • 70
12
votes
5 answers

Why invoke "apply" instead of calling function directly?

When looking at the source code for raphael or g.raphael or other libraries I've noticed the developer does something like this: var val = Math.max.apply(Math, data_array); Why not just invoke the function directly, such as: var val =…
codecraig
  • 3,118
  • 9
  • 48
  • 62
12
votes
1 answer

Best way to document "splatted" parameter with YARD?

I have a method that should take 1+ parameters of any class, similar to Array#push: def my_push(*objects) raise ArgumentError, 'Needs 1+ arguments' if objects.empty? objects.each do |obj| puts "An object was pushed: #{obj.inspect}" …
hololeap
  • 1,156
  • 13
  • 20
12
votes
1 answer

Explanation of splat

While reading about Julia on http://learnxinyminutes.com/docs/julia/ I came across this: # You can define functions that take a variable number of # positional arguments function varargs(args...) return args # use the keyword return to…
kip820
  • 305
  • 3
  • 6
12
votes
1 answer

Splat on a hash

A splat on a hash converts it into an array. [*{foo: :bar}] # => [[:foo, :bar]] Is there some hidden mechanism (such as implicit class cast) going on here, or is it a built-in primitive feature? Besides an array, are nil and hash the only things…
sawa
  • 165,429
  • 45
  • 277
  • 381
11
votes
1 answer

Understanding ruby splat in ranges and arrays

I'm trying to understand the difference between *(1..9) and [*1..9] If I assign them to variables they work the same way splat1 = *(1..9) # splat1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] splat2 = [*1..9] # splat2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] But things…
Dty
  • 12,253
  • 6
  • 43
  • 61
10
votes
1 answer

Unroll / splat arguments in common lisp

Say I have a list of arguments: > (setf format-args `(t "it's ~a" 1)) (T "it's ~a" 1) How can I then "splat" or "unroll" this into a series of arguments rather than a single list argument, for supplying to the format function? i.e I would like…
gfxmonk
  • 8,614
  • 5
  • 42
  • 53
10
votes
2 answers

Double-splat operator destructively modifies hash – is this a Ruby bug?

I noticed what I find to be a very surprising behavior with the ** (double-splat) operator in Ruby 2.1.1. When key-value pairs are used before a **hash, the hash remains unmodified; however, when key-value pairs are only used after the **hash, the…
user513951
  • 12,445
  • 7
  • 65
  • 82
10
votes
5 answers

How to define a method in ruby using splat and an optional hash at the same time?

I am able to define a method like this: def test(id, *ary, hash_params) # Do stuff here end But this makes the hash_params argument mandatory. These don't work either: def t(id, *ary, hash_params=nil) # SyntaxError: unexpected '=', expecting…
andersonvom
  • 11,701
  • 4
  • 35
  • 40
9
votes
2 answers

Why do Ruby procs/blocks with splat arguments behave differently than methods and lambdas?

Why do Ruby (2.0) procs/blocks with splat arguments behave differently than methods and lambdas? def foo (ids, *args) p ids end foo([1,2,3]) # => [1, 2, 3] bar = lambda do |ids, *args| p ids end bar.call([1,2,3]) # => [1, 2, 3] baz = proc do…
Jordan
  • 391
  • 4
  • 12
9
votes
1 answer

Parentheses in block variables

Given a = [[:a, :b, :c]] 1) I understand this a.each{|(x, y), z| p z} # => :b that there are two variables (x, y) and z, so the third element :c is thrown away, and z matches :b. And I understand this a.each{|(x, y), z| p y} # => nil that (x, y)…
sawa
  • 165,429
  • 45
  • 277
  • 381
9
votes
2 answers

CoffeeScript: Expand array in function call

In Ruby I can call methods with array elements used as positional parameters like this method(fixed_arg1, fixed_arg2, *array_of_additional_args) Here the "*" operator expands the array in place. I'm trying to do the same in CoffeeScript, but…
8
votes
2 answers

Ruby, Source Code of 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?
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
8
votes
3 answers

What does the syntax [*a..b] mean in Ruby?

NOTE: mischa's splat on GitHub has lots of cool interactive examples of * in action. By googling, I found that one way to iterate over a range of numbers in Ruby (your classic C-style for loop) for (i = first; i <= last; i++) { whatever(i); } is…
Justin Force
  • 6,203
  • 5
  • 29
  • 39
1
2
3
11 12