Forum: Ruby * before string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
93b1c03b74dff7a41060d41a2da750ce?d=identicon&s=25 Jonatas Paganini (jonatas)
on 2009-02-12 17:52
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.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-12 18:24
(Received via mailing list)
Hi,

Am Freitag, 13. Feb 2009, 01:51:24 +0900 schrieb Jonatas Paganini:
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-12 18:32
(Received via mailing list)
On 12.02.2009 17:51, Jonatas Paganini 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-02-12 19:09
(Received via mailing list)
On Thu, Feb 12, 2009 at 12:22 PM, Bertram Scharpf
<lists@bertram-scharpf.de>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/
Twitter: http://twitter.com/RickDeNatale
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-12 19:31
Jonatas Paganini 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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-12 19:44
7stud -- wrote:
> Jonatas Paganini 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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-12 19:49
And apparently you can do this:

x, y, *remaining = ["snowboard", "skis", "sled", "boots"]

puts x, y
p remaining

--output:--
snowboard
skis
["sled", "boots"]
This topic is locked and can not be replied to.