From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 5E9701F5AE for ; Fri, 31 Jul 2020 07:35:04 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 9E40B120B54; Fri, 31 Jul 2020 16:34:31 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 0DB70120B53 for ; Fri, 31 Jul 2020 16:34:28 +0900 (JST) Received: by filterdrecv-p3iad2-d8cc6d457-kzc2c with SMTP id filterdrecv-p3iad2-d8cc6d457-kzc2c-19-5F23C9A0-56 2020-07-31 07:34:56.601439112 +0000 UTC m=+134798.448948396 Received: from herokuapp.com (unknown) by ismtpd0012p1iad1.sendgrid.net (SG) with ESMTP id NCQaB6e7SH-E4XYhI36i2w for ; Fri, 31 Jul 2020 07:34:56.460 +0000 (UTC) Date: Fri, 31 Jul 2020 07:34:56 +0000 (UTC) From: hanmac@gmx.de Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75241 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17093 X-Redmine-Issue-Author: mpavel X-Redmine-Sender: Hanmac X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?z6kUDlxbGclXLcwluHLUb5EO6uh6mW1Jn0ZSGyxj3H3z8WN6YyIvU=2FdGWlymwe?= =?us-ascii?Q?DSBmrl3Biwnre0AQAaaNsffdxRSThZ+oo+W5x0e?= =?us-ascii?Q?5bRPOBhIgiwSW3qPtgqO+kl8GYb5DdJaPIetJZ1?= =?us-ascii?Q?o0vzl1f4FoJ+RU3epKh4H6jpn3cpg9Jck6mKgAB?= =?us-ascii?Q?lS9fYSqlgch9jens8a=2FtQHbl0vw2zHkWnfQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99414 Subject: [ruby-core:99414] [Ruby master Bug#17093] attr_accessor works strange X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17093 has been updated by Hanmac (Hans Mackowiak). This: ``if type.nil? type = 'default' end`` is different from this: `type = 'default' if type.nil?` because of the lexical reading, it reads the setting of local variable first and now assumes that type is local variable in this case ---------------------------------------- Bug #17093: attr_accessor works strange https://bugs.ruby-lang.org/issues/17093#change-86861 * Author: mpavel (pavel m) * Status: Closed * Priority: Normal * ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- require 'rubygems' class A def initialize(type:) @type = type end def b p type p type.nil? type = 'default' if type.nil? type end private attr_accessor :type end RSpec.describe A do let(:type) { 'whoaaa' } it 'return default' do expect(A.new(type: type).b).to eq('default') end it 'instance variable is "whoaaa"' do expect(A.new(type: type).instance_variable_get(:@type)).to eq(type) end end all tests green output A "whoaaa" false return default instance variable is "whoaaa" -- https://bugs.ruby-lang.org/