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.4 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 05A221F454 for ; Tue, 5 Nov 2019 20:57:09 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id D95C0120A43; Wed, 6 Nov 2019 05:56:57 +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 95128120A42 for ; Wed, 6 Nov 2019 05:56:55 +0900 (JST) Received: by filter0138p3mdw1.sendgrid.net with SMTP id filter0138p3mdw1-12948-5DC1E21B-12 2019-11-05 20:56:59.162004271 +0000 UTC m=+7999.143480356 Received: from herokuapp.com (unknown [54.204.69.61]) by ismtpd0005p1iad1.sendgrid.net (SG) with ESMTP id 7EvDVGj0Rw6ND7y32gm7mg for ; Tue, 05 Nov 2019 20:56:59.073 +0000 (UTC) Date: Tue, 05 Nov 2019 20:56:59 +0000 (UTC) From: matthew@kerwin.net.au Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71299 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16295 X-Redmine-Issue-Author: byroot X-Redmine-Sender: phluid61 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?dc6bl8=2FRfjZl6IUFmz6hrXGqzg+6U4v9dH0u7=2FcQTE9U+lwhuWsTtSSSQuj5CQ?= =?us-ascii?Q?CaERYEx3vBcOydvgBawiG568bjNJuMWt=2FLD1r2Z?= =?us-ascii?Q?k=2FnfC4rKNKBNcY4MUl9Iz4b4bOka3+h5ntu03W6?= =?us-ascii?Q?hqOw1suPmycY4Q1TD1BRgsIxjq1RdXcgssZcZit?= =?us-ascii?Q?cIniRR0aGliDRXyaHdb=2F=2F49EdKySF5tBIjQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95707 Subject: [ruby-core:95707] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@ 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 #16295 has been updated by phluid61 (Matthew Kerwin). It doesn't exactly fit the way messages are named in Ruby, but how about: ```ruby alias -@ frozen alias +@ thawed ``` ---------------------------------------- Feature #16295: Chainable aliases for String#-@ and String#+@ https://bugs.ruby-lang.org/issues/16295#change-82491 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Original discussion https://bugs.ruby-lang.org/issues/16150?next_issue_id=16147&prev_issue_id=16153#note-40 In #16150, @headius raised the following concern about `String#-@` and `String#+@`: headius (Charles Nutter) wrote: > > Not exactly, -@ and +@ makes this much simpler > > I do like the unary operators, but they also have some precedence oddities: > > ``` > >> -"foo".size > => -3 > >> (-"foo").size > => 3 > ``` > > And it doesn't work at all if you're chaining method calls: > > ``` > >> +ary.to_s.frozen? > NoMethodError: undefined method `+@' for false:FalseClass > from (irb):8 > from /usr/bin/irb:11:in `
' > ``` > > But you are right, instead of the explicit `dup` with possible freeze you could use `-` or `+` on the result of `to_s`. However it's still not safe to modify it since it would modify the original string too. After working for quite a while with those, I have to say I agree. They very often force to use parentheses, which is annoying, and an indication that regular methods would be preferable to unary operators. In response @matz proposed to alias them as `String#+` and `String#-` without arguments: > How about making String#+ and #- without argument behave like #+@ and #-@ respectively, so that we can write: > > ``` > "foo".-.size > ary.+.to_s.frozen? > ``` My personal opinion is that descriptive method names would be preferable to `+/-`: > IMHO `.-` and `.+` is not very elegant. Proper method names explaining the intent would be preferable. > > - `-@` could be `dedup`, or `deduplicate`. > - `+@` could be `mutable` or `mut`. -- https://bugs.ruby-lang.org/