# \* before string

**URL:** <https://www.ruby-forum.com/t/before-string/160183>\
**Category:** Ruby\
**Created:** [February 12, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/before-string/160183 "2009-02-12T17:52:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/before-string/160183/1 "2009-02-12T17:52:47Z")

</div>

Hi, I’m looking for cucumber/generators and didn’t understand why use  
‘\*’ before string on initialize method.  
I put it on irb and the result is the same.

class NamedArg  
attr\_reader :name

```
def initialize(s)
  @name, @type = *s.split(':')
end

```

end

Why and where use \* before string?

thanks.

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/before-string/160183/2 "2009-02-12T18:24:23Z")

</div>

Hi,

Am Freitag, 13. Feb 2009, 01:51:24 +0900 schrieb Jonatas P.:

> class NamedArg  
> def initialize(s)  
> @name, @type = \*s.split(’:’)  
> end  
> end
> 
> Why and where use \* before string?

Actually, you use the asterisk in front of an array. The asterisk  
makes the array to a list of arguments. Examples:

ary.push [:a, :b] # pushes an array  
ary.push \*[:a, :b] # pushes two symbols

You may even do this:

done = %(exit quit bye)  
case str.downcase  
when \*done then return  
end

In your example, the conversion will be done automatically when  
the right side is an array. When I code, I write the asterisk  
explicitly every time to remind myself what I meant.

Bertram

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/before-string/160183/3 "2009-02-12T18:32:11Z")

</div>

On 12.02.2009 17:51, Jonatas P. wrote:

> end
> 
> Why and where use \* before string?

It’s not before a String but before an Array. You can see for yourself  
by firing up IRB and experimenting a bit with this. The short story is  
that it’s called “splash operator” IIRC and it will distribute the Array  
across all parameters or local variables. Other example uses are

def foo(a,b,c)  
puts a,b,c  
end

foo(1,2,3)  
ar = [1,2,3]  
foo(\*ar)  
foo(ar) # -\> error because you do not provide enough arguments

HTH

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 7:09pm UTC](https://www.ruby-forum.com/t/before-string/160183/4 "2009-02-12T19:09:21Z")

</div>

On Thu, Feb 12, 2009 at 12:22 PM, Bertram S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> when \*done then return  
> end
> 
> In your example, the conversion will be done automatically when  
> the right side is an array. When I code, I write the asterisk  
> explicitly every time to remind myself what I meant.

All true, except that in this case, it’s in the context of a parallel  
assignment statement rather than a method call.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/before-string/160183/5 "2009-02-12T19:44:25Z")

</div>

7stud – wrote:

> Jonatas P. wrote:
> 
> > ```
> > @name, @type = *s.split(':')
> > 
> > ```
> > 
> > Why and where use \* before string?
> 
> Your statement is equivalent to:
> 
> @name, @type = \*(s.split(’:’) )

…and it looks like the \* is redundant:

x, y = [“snowboard”, “skis”, “sled”]  
puts x, y

–output:–  
snowboard  
skis

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/before-string/160183/6 "2009-02-12T19:31:28Z")

</div>

Jonatas P. wrote:

> ```
> @name, @type = *s.split(':')
> 
> ```
> 
> Why and where use \* before string?

Your statement is equivalent to:

@name, @type = \*(s.split(’:’) )

Here is a simple example:

arr = [“apple”, “banana”]  
x, y = \*arr  
puts x, y

–output:–  
apple  
banana

Or:

s = “hello world”  
x, y = \*s.split()  
puts x, y

–output:–  
hello  
world

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [February 12, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/before-string/160183/7 "2009-02-12T19:49:49Z")

</div>

And apparently you can do this:

x, y, \*remaining = [“snowboard”, “skis”, “sled”, “boots”]

puts x, y  
p remaining

–output:–  
snowboard  
skis  
[“sled”, “boots”]
