* before string

J

Jonatas Paganini

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.
 
B

Bertram Scharpf

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
 
R

Robert Klemme

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?

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
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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.


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

7stud --

Jonatas said:
@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
 
7

7stud --

7stud said:
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
 
7

7stud --

And apparently you can do this:

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

puts x, y
p remaining

--output:--
snowboard
skis
["sled", "boots"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top