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
5
votes
3 answers

What does this mean in Ruby language?

Run the following code, a = [1, 2, 3, 4, 5] head, *tail = a p head p tail You will get the result 1 [2, 3, 4, 5] Who can help me to explain the statement head,*tail = a, Thanks!
Just a learner
  • 26,690
  • 50
  • 155
  • 234
5
votes
3 answers

ReactiveUI Dependency Injection Constructor

I am using the built in dependency injector/splat injector inside ReactiveUI. I have constructors where I want to pass along their applicable data repository. In other frameworks it just uses reflections with the interfaces, and uses the GetServices…
Glenn Watson
  • 2,758
  • 1
  • 20
  • 30
5
votes
2 answers

splat over JavaScript object (with new)?

How do I splat across objects without using ECMA6 features? Attempt function can(arg0, arg1) { return arg0 + arg1; } function foo(bar, haz) { this.bar = bar; this.haz = haz; } myArgs = [1,2]; With can I can just do: can.apply(this,…
A T
  • 13,008
  • 21
  • 97
  • 158
5
votes
4 answers

Why is splat argument in ruby not used all the time?

I know splat arguments are used when we do not know the number of arguments that would be passed. I wanted to know whether I should use splat all the time. Are there any risks in using the splat argument whenever I pass on arguments?
poorvank
  • 7,524
  • 19
  • 60
  • 102
5
votes
2 answers

Making a Hash from an array - how does this work?

fruit = ["apple","red","banana","yellow"] => ["apple", "red", "banana", "yellow"] Hash[*fruit] => {"apple"=>"red", "banana"=>"yellow"} Why does the splat cause the array to be so neatly parsed into the Hash? Or, more precisely, how does the…
uzo
  • 2,821
  • 2
  • 25
  • 26
5
votes
3 answers

Why is the splat used inside an array definition here?

def initialize(apps, catch=404) @apps = []; @has_app = {} apps.each { |app| add app } @catch = {} [*catch].each { |status| @catch[status] = true } end In this method from Rack::Cascade, what purpose does the splat(*) serve in the [*catch]…
uzo
  • 2,821
  • 2
  • 25
  • 26
4
votes
1 answer

Is it effcient to use a splat operator in a constructor?

In a constructor, it often happens that you want to turn the arguments into instance variables. A naive way to do it is: class A def initialize a, b, c @a, @b, @c = a, b, c end end but an easier way is: class A def initialize…
sawa
  • 165,429
  • 45
  • 277
  • 381
4
votes
2 answers

How can I splattify an anonymous object so I can use &method on it?

I'm wanting to use the &method(:method_name) idiom when there's more than one object required by method_name. Can I do this under Ruby 1.9? For example, if I've got def move_file(old_filename, new_filename) STDERR.puts "Moving…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
4
votes
2 answers

How to print a list using splat-operator (*) without spaces

I'm trying to understand splat-operators in python. I have a code: word = ['s', 't', 'a', 'c', 'k', 'o', 'v', 'e', 'r', 'f', 'l', 'o', 'w'] print(*word) output: s t a c k o v e r f l o w I can't assign *word to some variable to check its type or…
Pavel Antspovich
  • 1,111
  • 1
  • 11
  • 27
4
votes
2 answers

Why is Hash#merge using the splat operator returning an Array of Arrays of Hashes instead of an Array of Hashes?

TL;DR I solved the problem through trial and error, but there's clearly a gap in my understanding of how the splat operator and pp method are consistently giving me a different object than the one I think I have. I'd like to understand that gap, and…
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
4
votes
1 answer

Passing an empty hash through double splat in ruby

The idea is similar to what you would do with a decorator in python, I don't know what arguments the method takes so i'm passing *args, **kwargs as a general case The problem arises when I want to pass an empty hash as kwargs to a method that takes…
Asone Tuhid
  • 539
  • 4
  • 13
4
votes
2 answers

Does prolog have a spread/splat/*args operator?

In many procedural languages (such as python), I can "unpack" a list and use it as arguments for a function. For example... def print_sum(a, b, c): sum = a + b + c print("The sum is %d" % sum) print_sum(*[5, 2, 1]) This code will print: "The…
byxor
  • 5,930
  • 4
  • 27
  • 44
4
votes
1 answer

Powershell splat destroy variable

The argument to Split-Path below is incorrect and it should have been $delZipExe. This makes the $delZipCmd hash be set to nothing. I would expect WorkingDirectory value to be set to nothing in the $delZipCmd hash. Why does this…
opedroso
  • 103
  • 7
4
votes
1 answer

How to use a splat as a ruby method parameter

I'm looking at a ruby method def test(*) puts "hello" end I'm confused about the *. Obviously if I run test it returns "hello". But what if I pass an argument into test... test("this argument") How do I call that method within the test method…
thank_you
  • 11,001
  • 19
  • 101
  • 185
4
votes
2 answers

Double splat on `nil`

My understanding is that a single splat on a non-array object calls to_a and then dissociates the elements apart. And since nil.to_a is defined to be [], the following conversion happens: [:foo, *nil, :bar] # => [:foo, *nil.to_a, :bar] # => [:foo,…
sawa
  • 165,429
  • 45
  • 277
  • 381