What does *var mean?

C

Chih-Chao Lam

Apologies for a newbie ruby question:

I know an asterisk can precede a parameter in the argument list of a
method definition as in

def varargs(arg1, *rest)

But looking at the source code of Hpricot, I see that you can precede
an asterisk before any variable. Is there a formal definition for the
use of this operator?

irb(main):001:0> *d = 3
=> [3]
irb(main):002:0> d
=> [3]
irb(main):003:0> d == [*d]
=> true
irb(main):004:0> *d
SyntaxError: compile error
(irb):4: parse error, unexpected '\n', expecting '='
from (irb):4

Thanks,
chao
 
D

darren kirby

--nextPart15035682.qz6b9sZBiX
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the Chih-Chao Lam:
Apologies for a newbie ruby question:

I know an asterisk can precede a parameter in the argument list of a
method definition as in

def varargs(arg1, *rest)

def varargs(arg1, *rest)
puts arg1
rest.each |arg|
puts arg
end
end
varargs(one, two, three, four, five) one
two
three
four
five

Really, it just accumulates a variable amount of arguments and presents the=
m=20
in the function as a list.=20

**kwargs will collect them in a hash.
Thanks,
chao

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart15035682.qz6b9sZBiX
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE70qcwPD5Cr/3CJgRArPeAJkBsEk7dBuOWvyyg7Wvt5g0MynPowCgyyuw
JZRCUUB6OJ1CcsRjPB5u9JY=
=4F9V
-----END PGP SIGNATURE-----

--nextPart15035682.qz6b9sZBiX--
 
D

darren kirby

--nextPart6476853.dQsUu8Hp5F
Content-Type: text/plain;
charset="iso-8859-6"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the darren kirby:
quoth the Chih-Chao Lam:

def varargs(arg1, *rest)
puts arg1
rest.each |arg|
puts arg
end
end


one
two
three
four
five


Really, it just accumulates a variable amount of arguments and presents
them in the function as a list.

**kwargs will collect them in a hash.

Sorry guy, I guess you know that. I am not sure about the other form but it=
=20
appears to be syntactic sugar for building lists:

irb(main):001:0> *d =3D 1,2,3,4,5
=3D> [1, 2, 3, 4, 5]

-d

=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart6476853.dQsUu8Hp5F
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE70tewPD5Cr/3CJgRAkY0AKDxhlAGcxIqXij0xSmJcMWlyq8nUACgx+uj
pZsNyXmY3vPTtEnZOgQPs8o=
=8AyZ
-----END PGP SIGNATURE-----

--nextPart6476853.dQsUu8Hp5F--
 
N

Nicholas Wright

Chih-Chao Lam said:
Apologies for a newbie ruby question:

I know an asterisk can precede a parameter in the argument list of a
method definition as in

def varargs(arg1, *rest)

But looking at the source code of Hpricot, I see that you can precede
an asterisk before any variable. Is there a formal definition for the
use of this operator?

irb(main):001:0> *d = 3
=> [3]
irb(main):002:0> d
=> [3]
irb(main):003:0> d == [*d]
=> true
irb(main):004:0> *d
SyntaxError: compile error
(irb):4: parse error, unexpected '\n', expecting '='
from (irb):4

Thanks,
chao

* has a few different uses. When used like this:

def sum(*args)
args.inject(0) {|sum, i| sum + i}
end

sum(1, 23, 324, 3, 4543, 938, 9128, 42, 2134)

it's used to mean 'collect all passed arguments into an array'. When
used like this:

numbers = [1, 32, 32423, 32419879, 32517, 98172, 23478932]
sum(*numbers)

It means 'expand this list'. Which, in the above situation would
actually pass each number to sum as an individual argument, rather than
a single argument that is an array.

It has a few other usages, but they are all essentially variations on
the above two.

- Nick
 
D

David Vallner

Chih-Chao Lam said:
But looking at the source code of Hpricot, I see that you can precede an
asterisk before any variable. Is there a formal definition for the use
of this operator?

As far as I know, this, used in parallel assignments, is one of the
hairier bits of the formal grammar. Basically, it's does the same as
with the varargs use, except with the rvalues of an assignment as
opposed to method arguments.

David Vallner
 
D

dblack

Hi --

quoth the Chih-Chao Lam:

def varargs(arg1, *rest)
puts arg1
rest.each |arg|
puts arg
end
end


Really, it just accumulates a variable amount of arguments and presents them
in the function as a list.

**kwargs will collect them in a hash.

When you say "will"... do you mean in the future? Is that documented
somewhere? I can't puzzle out the reasoning behind it.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

darren kirby

--nextPart5261619.g8aRx2rz54
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the (e-mail address removed):
When you say "will"... do you mean in the future? Is that documented
somewhere? I can't puzzle out the reasoning behind it.

Eh? Are you just poking fun at me or what?

**kwargs collects them in a hash.

Better?

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart5261619.g8aRx2rz54
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE73vgwPD5Cr/3CJgRAts8AKCQkNzpejWfKTO2InvUZMk0YLrbCgCg1XYk
eoqrQH1Kk3YPn853Fu9cJXM=
=U7ju
-----END PGP SIGNATURE-----

--nextPart5261619.g8aRx2rz54--
 
H

Hal Fulton

darren said:
quoth the (e-mail address removed):




Eh? Are you just poking fun at me or what?

**kwargs collects them in a hash.

Better?

Haha... I don't think he was poking fun at you. He really
didn't know whether you were using a "real" future tense
or not.

Funny, because I had a conversation with him about my
copyeditor and this very issue.

FWIW, I used the "pseudo-future" tense a lot in the book,
and the CE always changed it to strict present. In some
cases, I changed it back.


Hal
 
D

darren kirby

--nextPart1858368.jtIOApRjkP
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the Hal Fulton:
Haha... I don't think he was poking fun at you. He really
didn't know whether you were using a "real" future tense
or not.

I thought for sure he was poking fun. I looked up a bunch of his previous=20
posts and he certainly knows a lot about Ruby, so he must know that precedi=
ng=20
a function/method definition arg with '**' collects them in a hash...
Funny, because I had a conversation with him about my
copyeditor and this very issue.

FWIW, I used the "pseudo-future" tense a lot in the book,
and the CE always changed it to strict present. In some
cases, I changed it back.

Yeah, we're computer guys, not English majors right?
Besides, it _will_ collect the args in a hash in the future, because you mu=
st=20
define the function before calling it...

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1858368.jtIOApRjkP
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE75OZwPD5Cr/3CJgRAs1AAJ9WR1nOVOSW9gu5YLHK31xp5NEcDQCgrfOE
7zQtwocQuH06AZPOkxnPEaM=
=2fBx
-----END PGP SIGNATURE-----

--nextPart1858368.jtIOApRjkP--
 
E

Ezra Zygmuntowicz

quoth the Hal Fulton:

I thought for sure he was poking fun. I looked up a bunch of his
previous
posts and he certainly knows a lot about Ruby, so he must know that
preceding
a function/method definition arg with '**' collects them in a hash...

Darren-

You must be thinking of python or something else. David was not
poking fun. **args is not legal ruby syntax currently:

irb(main):005:0> def foo(**bar)
irb(main):006:1> p bar
irb(main):007:1> end
SyntaxError: compile error
(irb):5: parse error, unexpected tPOW, expecting ')'
def foo(**bar)
^
from (irb):5
from :0
irb(main):008:0>

-Ezra
 
D

darren kirby

--nextPart1818710.n3rEFr3s9O
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the Ezra Zygmuntowicz:
Darren-

You must be thinking of python or something else. David was not
poking fun. **args is not legal ruby syntax currently:

irb(main):005:0> def foo(**bar)
irb(main):006:1> p bar
irb(main):007:1> end
SyntaxError: compile error
(irb):5: parse error, unexpected tPOW, expecting ')'
def foo(**bar)
^
from (irb):5
from :0
irb(main):008:0>

-Ezra

OK, I am a dumbass. You are right, I am thinking of Python. It is spelled o=
ut=20
right there on page 84 of the Pickaxe book. I wonder why I would think it w=
as=20
the same? I could have sworn I had done the same in Ruby...

Well, my sincere apologies to David, and thanks to Ezra for setting me stra=
it.

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1818710.n3rEFr3s9O
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE75cwwPD5Cr/3CJgRAuTfAKCceZ4bGp2mtRYcykS9gZas0JQGOgCfdCqF
kcEWeEc8RW/UUgt9zfQXZ4M=
=rYNM
-----END PGP SIGNATURE-----

--nextPart1818710.n3rEFr3s9O--
 
H

Hal Fulton

Ezra said:
Darren-

You must be thinking of python or something else. David was not
poking fun. **args is not legal ruby syntax currently:

irb(main):005:0> def foo(**bar)
irb(main):006:1> p bar
irb(main):007:1> end
SyntaxError: compile error
(irb):5: parse error, unexpected tPOW, expecting ')'
def foo(**bar)
^
from (irb):5
from :0
irb(main):008:0>

Hmm, is it a 1.9 thing? Or just planned? I remember
Matz showed it to us in 2003 or so at RubyConf.


Hal
 
D

dblack

Hi --

Hmm, is it a 1.9 thing? Or just planned? I remember
Matz showed it to us in 2003 or so at RubyConf.

Shhhhh! Maybe he's forgotten :)


David (and yes I know I'm posting to the whole list :)

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top