Syntax questions


#1

A few simple syntax questions:

  1. Anyway to do use the default arg for nonlast params? Something
    like:
    run(xyz, , 3)

  2. Veru often I see things like:
    a, b = meth()
    Is meth returning two objects? Or is it returning one object - like a
    tuple or something - which the parser automagically splits into a & b?
    What are the rules of using two vars with commas together?

Thanks


#2

removed_email_address@domain.invalid wrote:

A few simple syntax questions:

  1. Anyway to do use the default arg for nonlast params? Something
    like:
    run(xyz, , 3)

No. You have to use one form of keyword arguments (for example by using
a
hash). Note that proper support for this is likely to happen in Ruby
2.0.

  1. Veru often I see things like:
    a, b = meth()
    Is meth returning two objects? Or is it returning one object - like a
    tuple or something - which the parser automagically splits into a & b?
    What are the rules of using two vars with commas together?

It’s a combination of parallel assignment with multiple return values.

parallel assignment

a,b,c=1,2,3
a,b,c=[1,2,3]

multiple return values

def mu1
return 1,2,3
end

def mu2
[1,2,3]
end

def mu3
return [1,2,3]
end

irb(main):015:0> a,b,c = mu1
=> [1, 2, 3]
irb(main):016:0> a
=> 1
irb(main):017:0> b
=> 2
irb(main):018:0> c
=> 3
irb(main):019:0> a,b,c = mu2
=> [1, 2, 3]
irb(main):020:0> a
=> 1
irb(main):021:0> b
=> 2
irb(main):022:0> c
=> 3
irb(main):023:0> a,b,c = mu3
=> [1, 2, 3]
irb(main):024:0> a
=> 1
irb(main):025:0> b
=> 2
irb(main):026:0> c
=> 3

The star operator can be used to collect remaining args:

irb(main):029:0> a,*b = mu1
=> [1, 2, 3]
irb(main):030:0> a
=> 1
irb(main):031:0> b
=> [2, 3]

More strictly speaking, returning anything that implements #to_ary will
be
treated this way:

irb(main):032:0> class Foo
irb(main):033:1> def to_ary() [“foo”,2,3] end
irb(main):034:1> end
=> nil
irb(main):035:0> def foo() Foo.new end
=> nil
irb(main):036:0> a,b,c = foo
=> [“foo”, 2, 3]
irb(main):037:0> a
=> “foo”
irb(main):038:0> b
=> 2
irb(main):039:0> c
=> 3

HTH

Kind regards

robert

#3

DÅ?a Streda 22 Február 2006 02:33 removed_email_address@domain.invalid napísal:

A few simple syntax questions:

  1. Anyway to do use the default arg for nonlast params? Something
    like:
    run(xyz, , 3)

I don’t think so, would be a PITA to parse / check against typos. Use an
options hash when you need funky method invocation forms.

  1. Veru often I see things like:
    a, b = meth()
    Is meth returning two objects? Or is it returning one object - like a
    tuple or something - which the parser automagically splits into a & b?
    What are the rules of using two vars with commas together?

It’s returning an Array, which is then automagically split in the
parallel
assignment construct.

david@chello082119107152:~$ irb
irb(main):001:0> a, b = [1, 2]
=> [1, 2]
irb(main):002:0> a
=> 1
irb(main):003:0> b
=> 2

Might possibly change in 1.9 / 2.0 to reduce the ambiguity and general
mystery
surrounding parallel assignment in strange cases. (Basically, once the
number
of lvalues and rvalues differs, things get mildly funky, and it’s only
downhill from there)

David V.


#4

DÅ?a Streda 22 Február 2006 12:27 Ross B. napísal:

mystery surrounding parallel assignment in strange cases. (Basically,
once the number of lvalues and rvalues differs, things get mildly funky,
and it’s only downhill from there)

Seriously? If that’s true, I hope it never comes to pass. I don’t think
it’s mysterious, and it’s saved me a good few lines of code here and
there…

Hmm, it’s not on eigenclass, so I might have been hallucinating. IIRC,
wherever I read about that, the change was related to when the last
rvalue,
or the only rvalue was an Array - the automagical expansion being
removed or
some such. Don’t take this with any authority from me though.

David V.


#5

On Wed, 2006-02-22 at 10:50 +0900, David V. wrote:

of lvalues and rvalues differs, things get mildly funky, and it’s only
downhill from there)

Seriously? If that’s true, I hope it never comes to pass. I don’t think
it’s mysterious, and it’s saved me a good few lines of code here and
there…

a,b = [1,2,3]

=> [1, 2, 3]

a

=> 1

b

=> 2

a,*b = [1,2,3]

=> [1, 2, 3]

a

=> 1

b

=> [2, 3]

a,b,c = [1,2]

=> [1, 2]

a

=> 1

b

=> 2

c

=> nil

Which is exactly what I expected to happen.