Ruby idioms : The splat operator.

The split mode :

   pet1, pet2, pet3 = *["duck","dog","cat"]

The collect mode :

  *zoo = pet1, pet2, pet3

The splat operator can be used in a case statement :

BOARD_MEMBERS = ['Jan', 'Julie', 'Archie', 'Stewick']
HISTORIANS = ['Braith', 'Dewey', 'Eduardo']case name
 when *BOARD_MEMBERS
  "You're on the board!  A congratulations is in order."
 when *HISTORIANS
  "You are busy chronicling every deft play." 
 when *HISTORIANS|BOARD_MEMBERS
  "We welcome you all to the First International
   Symposium of Board Members and Historians Alike."
 end

We can also use the splat operator to create a Hash object form a array of pairs.

 

 

a = [[:planes, 21], [:cars, 36]]
h = Hash[*a]  # => { :planes=>21, :cars=>36}

7 comments so far

  1. [...] operator by the name of “splat.” Searching for “splat operator” turned up this page, describing how it works. Turns out *Array is special and is used to turn an Array into a list of [...]

  2. wzph on

    Hi there. What’s the functional difference between

    pet1, pet2, pet3 = *["duck", "dog", "cat"]

    and

    pet1, pet2, pet3 = ["horse", "cow", "goat"]

    I get the same results–namely, each pet gets one value from the array.

  3. Pedro on

    Maybe the examples were so simple to demonstrate the different.

    >> pets = ["dog", "cat"]
    => ["dog", "cat"]

    >> pet_store = ["mole", "duck", pets]
    => ["mole", "duck", ["dog", "cat"]]

    >> pet_store = ["mole", "duck", *pets]
    => ["mole", "duck", "dog", "cat"]

    But, the splat array must be always the last element of the array.

    pet_store = [*pets, "mole", "duck"]
    SyntaxError: compile error
    (irb):4: syntax error, unexpected ',', expecting ']'
    pet_store = [*pets, "mole", "duck"]
    ^
    (irb):4: syntax error, unexpected ',', expecting $end
    pet_store = [*pets, "mole", "duck"]
    ^
    from (irb):4

    You can use this to define variable parameter functions or methods.

    >> def pets(*mammals)
    >> mammals
    >> end
    => nil

    >> pets :monkey, :dog
    => [:monkey, :dog]

    >> pets :monkey, :dog, :cat
    => [:monkey, :dog, :cat]

  4. ekillaby on

    It may also help to note that ’splat’ returns a copy of the instance that it’s used with, whereas not using ’splat’ returns a reference to the instance.

    Ex.\
    [
    >group = ["blue", "green", "pink"]
    >painting = ["paintbrush", "canvas", group]
    >group[2] = “purple”
    >puts painting

    gives us: paintbrush, canvas, blue, green, purple
    ]

    [
    >group = ["blue", "green", "pink"]
    >painting = ["paintbrush", "canvas", *group]
    >group[2] = “purple”
    >puts painting

    gives us: paintbrush, canvas, blue, green, pink
    ]

  5. [...] closest I have come to finding any other good explanation of this on the web is this post and its comments. Interestingly, it points out how splat works in the context of assignment rather [...]

  6. [...] the *_find_page does. Can anyone explain it to me? It’s called the ’splat’ operator. see e.g. http://theplana.wordpress.com/2007/0…plat-operator/ http://redhanded.hobix.com/bits/theSiphoningSplat.html [...]

  7. Alexwebmaster on

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru


Leave a reply