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-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.0 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no 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 67FE91F463 for ; Sun, 29 Dec 2019 19:46:41 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id D0C92120BAF; Mon, 30 Dec 2019 04:46:25 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id BCB6C120BAE for ; Mon, 30 Dec 2019 04:46:23 +0900 (JST) Received: by filterdrecv-p3mdw1-56c97568b5-f2xng with SMTP id filterdrecv-p3mdw1-56c97568b5-f2xng-18-5E090294-16 2019-12-29 19:46:28.415582115 +0000 UTC m=+1105401.500700358 Received: from herokuapp.com (unknown [54.145.162.235]) by ismtpd0016p1iad2.sendgrid.net (SG) with ESMTP id qB2sD8u2QQy10JnnRDwl0Q for ; Sun, 29 Dec 2019 19:46:28.361 +0000 (UTC) Date: Sun, 29 Dec 2019 19:46:28 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72246 X-Redmine-Project: ruby-master X-Redmine-Issue-Id: 12086 X-Redmine-Issue-Author: shugo X-Redmine-Sender: Eregon 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr3ZLZj6jhNi9pxNTWivCX?= =?us-ascii?Q?DolyEgifgZCOXWMY6Zpo5TnKm+HhPJr0pqaG2K4?= =?us-ascii?Q?f8p7cnm94e6+Ejqrbiu=2FTeuy1Bh+ezqyLCd7mRn?= =?us-ascii?Q?OU9a5LEgfNj3xfD+P4U1GmrETqVEAbkWqNM0V7O?= =?us-ascii?Q?Np306kQoi6Zb1x4w6Uh5w8+tfkiLVxGDA043JC0?= =?us-ascii?Q?RfhqoPkBKEG2DVBBs=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96583 Subject: [ruby-core:96583] [Ruby master Feature#12086] using: option for instance_eval etc. 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 #12086 has been updated by Eregon (Benoit Daloze). Should we close this? I agree with @headius here. `using` with a lexical block (i.e., `using(refinement) do ... end`, not `using(&proc)`) might be a good alternative (#12281). ---------------------------------------- Feature #12086: using: option for instance_eval etc. https://bugs.ruby-lang.org/issues/12086#change-83550 * Author: shugo (Shugo Maeda) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Currently refinements can be activated only in toplevel or class/module definitions. If they can be activated in block-level, it's useful to implement internal DSLs. How about to add a new option using: for Kernel#instance_eval and Moule#{class,module}_eval? ```ruby module FixnumDivExt refine Fixnum do def /(other) quo(other) end end end p 1 / 2 #=> 0 instance_eval(using: FixnumDivExt) do p 1 / 2 #=> (1/2) end p 1 / 2 #=> 0 ``` Proof-of-concept implementation is available at . In my previous proposal before Ruby 2.0, refinements used in a class or module are implicitly activated by instance_eval and class_eval, but now I think it's better to explicitly specify refinements to be activated. Considerations: * In the PoC implementation, refined methods are not cached inline, and thus it decreases the performance of refined method call. If there is a way to guarantee that blocks never be evaluated in different environments, refined methods can be cached inline. * {instance,class,module}_exec cannot be extended in the same way, because they take arbitrary arguments and there's no way to distinguish an option hash from the last argument hash. -- https://bugs.ruby-lang.org/