[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What does *var mean?

Chih-Chao Lam

8/25/2006 6:53:00 PM

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


12 Answers

darren kirby

8/25/2006 7:08:00 PM

0

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 them
in the function as a list.

**kwargs will collect them in a hash.

> Thanks,
> chao

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

darren kirby

8/25/2006 7:11:00 PM

0

quoth the darren kirby:
> 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
> 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
appears to be syntactic sugar for building lists:

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

> > Thanks,
> > chao
>

-d

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

Nicholas Wright

8/25/2006 7:58:00 PM

0


Chih-Chao Lam wrote:
> 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

David Vallner

8/25/2006 8:38:00 PM

0

Chih-Chao Lam wrote:
> 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

dblack

8/25/2006 10:08:00 PM

0

darren kirby

8/25/2006 10:38:00 PM

0

quoth the dblack@wobblini.net:

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

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

**kwargs collects them in a hash.

Better?

> David

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

Hal E. Fulton

8/25/2006 11:46:00 PM

0

darren kirby wrote:
> quoth the dblack@wobblini.net:
>
>
>>>**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.
>
>
> 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

darren kirby

8/26/2006 12:20:00 AM

0

quoth the Hal Fulton:
> darren kirby wrote:
> > quoth the dblack@wobblini.net:
> >>>**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.
> >
> > 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.

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

> 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 must
define the function before calling it...

>
> Hal

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

Ezra Zygmuntowicz

8/26/2006 12:27:00 AM

0


On Aug 25, 2006, at 5:19 PM, darren kirby wrote:

> quoth the Hal Fulton:
>> darren kirby wrote:
>>> quoth the dblack@wobblini.net:
>>>>> **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.
>>>
>>> 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.
>
> 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



darren kirby

8/26/2006 12:35:00 AM

0

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 out
right there on page 84 of the Pickaxe book. I wonder why I would think it was
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 strait.

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