ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:51056] [Ruby master Bug#4443] odd evaluation order in a multiple assignment
Date: Tue, 20 Apr 2021 00:11:32 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91614.20210420001132.18@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-4443.20110224232852.18@ruby-lang.org>

Issue #4443 has been updated by jeremyevans0 (Jeremy Evans).


I have submitted a pull request to fix multiple assignment evaluation order: https://github.com/ruby/ruby/pull/4390

----------------------------------------
Bug #4443: odd evaluation order in a multiple assignment
https://bugs.ruby-lang.org/issues/4443#change-91614

* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: -
----------------------------------------
遠藤です。core に投げてしまったようなので登録し直し。

Ruby は左から右に評価が進むと信じていたのですが、多重代入で裏切られました。

```ruby
  def foo
    p :foo
    []
  end
  def bar
    p :bar
  end

  x, foo[0] = bar, 0
```

bar より foo が左にあるので、:foo 、:bar の順に出力されることを期待するのですが、なんと :bar 、:foo になります。

具体的に何が困るかというと、例えば

```ruby
  obj, obj.foo = obj.foo, obj
```

には swap を期待するわけですが、そうなりません。こういうコードは実際に、木の回転などを実装するときにしばしば書きたくなります。この挙動に気がついたのも splay tree を実装していたときでした。こんなの:

```ruby
  t.left, t.left.right, t = t.left.right, t, t.left
```

1.9 系列で修正すべきとまでは思いませんが、2.0 で直る可能性はあるでしょうか。

IRC で話したら「それで普通」みたいな反応もありましたが、

```
  foo[0] = bar
```

はちゃんと :foo 、:bar の順に出ます。

-- 
Yusuke Endoh <mame@tsg.ne.jp>




-- 
https://bugs.ruby-lang.org/

       reply	other threads:[~2021-04-20  0:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-4443.20110224232852.18@ruby-lang.org>
2021-04-20  0:11 ` merch-redmine [this message]
2021-05-05 15:46 ` [ruby-dev:51059] " eregontp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-91614.20210420001132.18@ruby-lang.org \
    --to=merch-redmine@jeremyevans.net \
    --cc=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).