[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Syntax questions

List Recv

2/22/2006 1:30:00 AM

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

4 Answers

David Vallner

2/22/2006 1:50:00 AM

0

Dna Streda 22 Február 2006 02:33 listrecv@gmail.com 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.

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

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 Vallner


Ross Bamford

2/22/2006 11:28:00 AM

0

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

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

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.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Robert Klemme

2/22/2006 1:00:00 PM

0

listrecv@gmail.com 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.

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

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



David Vallner

2/23/2006 12:45:00 AM

0

Dna Streda 22 Február 2006 12:27 Ross Bamford napísal:
> On Wed, 2006-02-22 at 10:50 +0900, David Vallner wrote:
> > 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)
>
> 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 Vallner