tutorial - html ruby on rails




What does mean both the operators*and(*) in ruby? (2)

(*) really just reads one element from the right side. Consider this example, which has a fifth element in the langs array:

langs = ["java", "csharp", "ruby", "haskell", "python" ]

So when you use a normal splat, you get:

l1,*,l2 = langs

l1 #=> "java"
l2 #=> "python"

in contrast to your example with the parentheses:

l1,(*),(*),l2 = langs

l1 #=> "java"
l2 #=> "haskell"

I want to mention though, that for this case you would normally use _ to assign the middle values to "nothing" (equivalent to the last example):

l1,_,_,l2 = langs

l1 #=> "java"
l2 #=> "haskell"

If you want to see what ended up being inside of the middle values, you can explicitly assign the values to variables like so:

l1,*m,l2 = *langs

l1 #=> "java"
l2 #=> "python"
m  #=> ["csharp","ruby","haskell"]

or with the other example:

l1,(*m1),(*m2),l2 = langs

l1 #=> "java"
l2 #=> "haskell"
m1 #=> ["csharp"]
m2 #=> ["ruby"]

So i hope this makes clear that (*) isnt an operator on its own, but really just a splat inside parentheses and therefore has no own name.

I just introduced myself to the Ruby splat oprator. And I played with it lot's of way. but the below experiment somehow made me think about it twice :)

langs = ["java", "csharp", "ruby", "haskell" ]
# => ["java", "csharp", "ruby", "haskell"]

 l1,*,l2 = *langs
# => ["java", "csharp", "ruby", "haskell"]
 l1
# => "java"
 l2
# => "haskell"

 l1,*,*,l2 = *langs
SyntaxError: (irb):27: syntax error, unexpected tSTAR
l1,*,*,l2 = *langs
      ^
    from /usr/bin/irb:12:in `<main>'

Yes, the error is obvious, as I used more then 1 *(splat) operators in the same argument list.

Now I tried to play with it.

l1,(*),(*),l2 = *langs
# => ["java", "csharp", "ruby", "haskell"]

Ahh! here it works. But couldn't understand why so?

 l1
# => "java"
 l2
# => "haskell"
 l1,(*),l2 = *langs
# => ["java", "csharp", "ruby", "haskell"]
 l1
# => "java"
 l2
# => "ruby"

From the above example it seems that it is doing skipping of array elements.

Questions are :

  • (a) what the operator (*) is called?

  • (b) when I used in splat(*) in the line l1,*,l2 = *langs it consumes all the elements - "csharp", "ruby". Is there any way to see what * consumes there technically? Obviously I am teling with the use if l1,*,l2 = *langs not by l1,l*,l2 = *langs.


Think of it this way: The parenthesis by themselves (ignoring a variable name or splat operator) are accessing a single element from the array:

l1,  (), (), l2 = *['a', 'b', 'c', 'd']
 ^   ^   ^   ^
'a' 'b' 'c' 'd'

If it were an array of arrays, it would make more sense to use parenthesis:

>> l1,(l3, l4),(l5, l6),l2 = *['a', ['b', 'c'], ['d', 'e'], 'f']
=> ["a", ["b", "c"], ["d", "e"], "f"]
>> l1
=> "a"
>> l3
=> "b"
>> l4
=> "c"
>> l5
=> "d"
>> l6
=> "e"
>> l2
=> "f"

Therefore the (*) is taking a single element from the array, and splat-assigning it. The parenthesis themselves take a SINGLE element, the splat then takes that single element and "splats" it.

It's good to note that when performing multi-variable assignment from an array, the splat is not necessary on the array side:

>> a,b,c = ['a', 'b', 'c']
=> ["a", "b", "c"]
>> a
=> "a"
>> b
=> "b"
>> c
=> "c"




ruby-1.9.3