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}
About these ads

16 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

  8. Andrew Grimm on

    when *HISTORIANS|BOARD_MEMBERS

    has to go before the others, otherwise you’ll get the board members string instead.

  9. [...] Ruby idioms : The splat operator. « The Plan A [...]

  10. Rafael Magana on

    You’re wrong with here:

    a = [[:planes, 21], [:cars, 36]]

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

    The result of that is actually:

    {[:planes, 21]=>[:cars, 36]}

    which means the key is [:planes, 21], which is valid but not expected.

    h[[:planes, 21]] # => [:cars, 36]

    to work as expected you have to do this:

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

    Thanks for sharing.

  11. Rafael Magana on

    Look, I just made a post on the splat operator => http://bit.ly/raflabs_foo_splat

  12. Chinese Girls on

    the pet store here in our area offers me a great deal of discount when i buy from them ‘”*

  13. [...] Although still in the early stages of development, phuby already brings Ruby mixins to PHP, splat capabilities, and send and respond_to methods, among other features. Planned features include [...]

  14. Graig Skanes on

    I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to construct my own blog and would like to know where u got this from. cheers

  15. How to prevent snoring on

    I like this website very much, Its a really nice spot to read and incur information.

  16. Kendrick Daye on

    Hello.This article was really remarkable, especially because I was searching for thoughts on this subject last Sunday.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: