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
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 V.
Chih-Chao Lam wrote:
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.
quoth the darren kirby:
rest.each |arg|
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
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
quoth the [email protected]:
**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 wrote:
**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
Hi –
On Sat, 26 Aug 2006, darren kirby wrote:
puts arg1
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.
When you say “will”… do you mean in the future? Is that documented
somewhere? I can’t puzzle out the reasoning behind it.
David
On Aug 25, 2006, at 5:19 PM, darren kirby wrote:
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
quoth the Hal F.:
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
Ezra Z. wrote:
(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
quoth the Ezra Z.:
(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
Hi –
On Sat, 26 Aug 2006, Hal F. wrote:
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.
Shhhhh! Maybe he’s forgotten 
David (and yes I know I’m posting to the whole list 