Syntax questions

L

listrecv

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
 
D

David Vallner

D=C5=88a Streda 22 Febru=C3=A1r 2006 02:33 (e-mail address removed) nap=C3=ADsal:
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=20
options hash when you need funky method invocation forms.
2) Veru often I see things like:
a, b =3D 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=
=20
assignment construct.

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

Might possibly change in 1.9 / 2.0 to reduce the ambiguity and general myst=
ery=20
surrounding parallel assignment in strange cases. (Basically, once the numb=
er=20
of lvalues and rvalues differs, things get mildly funky, and it's only=20
downhill from there)

David Vallner
 
R

Ross Bamford

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

Robert Klemme

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
 
D

David Vallner

D=C5=88a Streda 22 Febru=C3=A1r 2006 12:27 Ross Bamford nap=C3=ADsal:
david@chello082119107152:~$ irb
irb(main):001:0> a, b =3D [1, 2]
=3D> [1, 2]
irb(main):002:0> a
=3D> 1
irb(main):003:0> b
=3D> 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,=20
wherever I read about that, the change was related to when the last rvalue,=
=20
or the only rvalue was an Array - the automagical expansion being removed o=
r=20
some such. Don't take this with any authority from me though.

David Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top