"multiple assignment in conditional"

Rubies,

Given this:

def foo
[4,5]
end

Both of the following expressions give me a SyntaxError: “multiple assignment
in conditional” (this is Ruby 1.6.5 on Cygwin):

a, b = foo and puts a
(a, b = foo) and puts a

I must say that I’m perplexed by this. “Ruby in a Nutshell” (p. 18) tells me
that “and” has lower precedence than “=”. Of course, the parentheses make my
intention clear anyway, and I still get the error. So I just don’t understand.

Can someone enlighten?

···


Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd

Hello Gavin,

Wednesday, October 30, 2002, 8:36:50 AM, you wrote:

def foo
[4,5]
end

Both of the following expressions give me a SyntaxError: “multiple assignment
in conditional” (this is Ruby 1.6.5 on Cygwin):

a, b = foo and puts a
(a, b = foo) and puts a

1.7.3:

a, b = foo and puts a
syntax error
(a, b = foo) and puts a
works ok and prints 4

1.6.7 produces the same result as your 1.6.5. i think that problem is
what “and” needs ONE value in first argument and your multiple
assihnment supports TWO values :slight_smile: but in any way in 1.7.3 second
expression works. btw, what you exactly want?

a,b=foo; a and puts a
or
a,b=foo; b and puts a
or
a,b=foo; a and b and puts a

? :wink:

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi,

Given this:

def foo
[4,5]
end

Both of the following expressions give me a SyntaxError: “multiple assignment
in conditional” (this is Ruby 1.6.5 on Cygwin):

a, b = foo and puts a
(a, b = foo) and puts a

I must say that I’m perplexed by this. “Ruby in a Nutshell” (p. 18) tells me
that “and” has lower precedence than “=”. Of course, the parentheses make my
intention clear anyway, and I still get the error. So I just don’t understand.

The resulting value of multiple assignment may not be what you expect,
due to value-array conversion.

% ruby1.6 -e ‘p ((a,b = nil))’
[nil]
% ruby1.7 -e ‘p ((a,b = nil))’

That’s why I warned you.

						matz.
···

In message ““multiple assignment in conditional”” on 02/10/30, “Gavin Sinclair” gsinclair@soyabean.com.au writes:

not a clue, works fine in 1.7.2-1 (msvc)
Gavin Sinclair wrote:

···

Rubies,

Given this:

def foo
[4,5]
end

Both of the following expressions give me a SyntaxError: “multiple assignment
in conditional” (this is Ruby 1.6.5 on Cygwin):

a, b = foo and puts a
(a, b = foo) and puts a

I must say that I’m perplexed by this. “Ruby in a Nutshell” (p. 18) tells me
that “and” has lower precedence than “=”. Of course, the parentheses make my
intention clear anyway, and I still get the error. So I just don’t
understand.

Can someone enlighten?


Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd

1.7.3:

a, b = foo and puts a
syntax error
(a, b = foo) and puts a
works ok and prints 4

That’s good news. Thanks for the tip.

1.6.7 produces the same result as your 1.6.5. i think that problem is
what “and” needs ONE value in first argument and your multiple
assihnment supports TWO values :slight_smile:

That’s what I don’t get. It gets one value, not two:

a, b = 1, 2 # → [1,2]

“and” should be able to deal with an array.

I’m surprised that
a, b = foo and puts a
doesn’t work in 1.7, because, as I said, “=” binds more tightly than “and”, so
to my mind, that LOC should be exactly equivalent to
(a, b = foo) and puts a

Can anyone explain why it isn’t?

btw, what you exactly want?

a,b=foo; a and puts a
or
a,b=foo; b and puts a
or
a,b=foo; a and b and puts a

? :wink:

I want
a, b = foo and puts a
!

Since foo always returns non-nil, I want the equivalent of
a, b = foo; puts a

It’s really a matter of style. The actual line of code is like this:

results, duplicates = @scraper.get_backend_ids(last_record) and
LOG.info “Retrieved backend IDs: #{results.length} of them”

Gavin

···

From: “Bulat Ziganshin” bulatz@integ.ru

Hello Gavin,

Wednesday, October 30, 2002, 9:29:35 AM, you wrote:

a, b = foo; puts a

It’s really a matter of style. The actual line of code is like this:

results, duplicates = @scraper.get_backend_ids(last_record) and
LOG.info “Retrieved backend IDs: #{results.length} of them”

imvho, it’s better to omit “and” because in ruby “and” is logical
operator and anyone else can’t understand your code. i personally will
spend much time finding condition when first line will be “false” :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

Fair enough, too. It’s just that the following code

action1(blah, blah, blah)
LOG.info “done action 1”
action2(blah, blah, blah)
LOG.info “done action 1”
action3(blah, blah, blah)
LOG.info “done action 1”

is more readable to me as

action1(blah, blah, blah) and
LOG.info “done action 1”
action2(blah, blah, blah) and
LOG.info “done action 1”
action3(blah, blah, blah) and
LOG.info “done action 1”

Even

action1(blah, blah, blah)
LOG.info “done action 1”

action2(blah, blah, blah)
LOG.info “done action 1”

action3(blah, blah, blah)
LOG.info “done action 1”

doesn’t really cut it for me.

I like the shortcut
action and followup
for
if action
followup
end
or
followup if action

occasionally. Depends what’s going on in my head.

Gavin

···

----- Original Message -----
From: “Bulat Ziganshin” bulatz@integ.ru
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, October 30, 2002 5:38 PM
Subject: Re: “multiple assignment in conditional”

Hello Gavin,

Wednesday, October 30, 2002, 9:29:35 AM, you wrote:

a, b = foo; puts a

It’s really a matter of style. The actual line of code is like this:

results, duplicates = @scraper.get_backend_ids(last_record) and
LOG.info “Retrieved backend IDs: #{results.length} of them”

imvho, it’s better to omit “and” because in ruby “and” is logical
operator and anyone else can’t understand your code. i personally will
spend much time finding condition when first line will be “false” :slight_smile:

Hello Gavin,

Wednesday, October 30, 2002, 9:48:48 AM, you wrote:

action1(blah, blah, blah)
LOG.info “done action 1”
action2(blah, blah, blah)
LOG.info “done action 1”
action3(blah, blah, blah)
LOG.info “done action 1”

is more readable to me as

what about:

action1(blah, blah, blah)
LOG.info “done action 1”
action2(blah, blah, blah)
LOG.info “done action 1”
action3(blah, blah, blah)
LOG.info “done action 1”

? :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hello Gavin,

Wednesday, October 30, 2002, 9:48:48 AM, you wrote:

action1(blah, blah, blah)
LOG.info “done action 1”
action2(blah, blah, blah)
LOG.info “done action 1”
action3(blah, blah, blah)
LOG.info “done action 1”

is more readable to me as

what about:

action1(blah, blah, blah)
LOG.info “done action 1”
action2(blah, blah, blah)
LOG.info “done action 1”
action3(blah, blah, blah)
LOG.info “done action 1”

Bulat mailto:bulatz@integ.ru

Great idea, BUT: any editor worth its salt will reindent that the first chance
it gets (i.e. align it all). Actually, Vim, which is worth all the salt in
Australia, will re-indent

x and
y

to

x and
y

anyway, but I’m working on that. Ruby is a difficult language to handle in
editors. Even Perl is easier because of the mandatory semicola :slight_smile:

Your suggestion is what I have in my code currently. I just have to keep my
hand away from the = key in normal mode :slight_smile:

Gavin

···

From: “Bulat Ziganshin” bulatz@integ.ru