What does *var mean?

(Chih-Chao Lam) #1

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

(darren kirby) #2

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

(Nicholas Wright) #3

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) #4

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

(darren kirby) #5

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

(David A. Black) #6

Hi --

···

On Sat, 26 Aug 2006, darren kirby wrote:

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.

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.

(darren kirby) #7

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

(HAL 9000) #8

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) #9

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

(Ezra Zygmuntowicz) #10

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
irb(main):008:0>

-Ezra

···

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

(darren kirby) #11

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

(HAL 9000) #12

Ezra Zygmuntowicz wrote:

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

(David A. Black) #13

Hi --

···

On Sat, 26 Aug 2006, Hal Fulton wrote:

Ezra Zygmuntowicz wrote:

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.

Shhhhh! Maybe he's forgotten :slight_smile:

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

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