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=-2.8 required=3.0 tests=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 3CDA21F4B6 for ; Sun, 14 Jul 2019 17:58:39 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6580C120A15; Mon, 15 Jul 2019 02:58:32 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 0A311120A17 for ; Mon, 15 Jul 2019 02:58:29 +0900 (JST) Received: by filter0006p3iad2.sendgrid.net with SMTP id filter0006p3iad2-10128-5D2B6D47-23 2019-07-14 17:58:31.852053034 +0000 UTC m=+946028.824516573 Received: from herokuapp.com (unknown [54.172.56.155]) by ismtpd0067p1mdw1.sendgrid.net (SG) with ESMTP id xqs1Uc4BRgqUszGrjyAIxA for ; Sun, 14 Jul 2019 17:58:31.795 +0000 (UTC) Date: Sun, 14 Jul 2019 17:58:31 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 69259 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 14145 X-Redmine-Issue-Author: zverok 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0R3rOJni7=2FMfpqg0dW+S?= =?us-ascii?Q?yy+jcXjSmmyUQWJmtg6bidVXRwtR6J2pYuV7Spb?= =?us-ascii?Q?mEJK5zVuA091WxLNQ6J4Aj5y6y=2FZ0UBE=2FGiD+g1?= =?us-ascii?Q?paadyrHammuGTy1qEnida79SrTl0Zi+fno4hVVg?= =?us-ascii?Q?v+auQP6eVbexPmCuXDofHHLxILAZf25Z0IA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 93762 Subject: [ruby-core:93762] [Ruby master Feature#14145] Proposal: Better Method#inspect 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 #14145 has been updated by Eregon (Benoit Daloze). znz (Kazuhiro NISHIYAMA) wrote: > `Method#inspect` separated by ` ` (a space) and `Proc#inspect` separated by `@`. > I think it is better to be unified, but I have no opinion on which is better. I think ` ` (a space) is better, and it's what we use for Method#inspect in TruffleRuby. My motivation is that selectionby double-clicking in the terminal just works with a space, but fails with `@` and results in `0x00007f850b8f9458@-e` when double-clicking on the filename (`-e` here). I find it also easier to read. Anyone against changing `@` to ` ` (a space) in Proc#inspect? ---------------------------------------- Feature #14145: Proposal: Better Method#inspect https://bugs.ruby-lang.org/issues/14145#change-79403 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- The idea: When investigating (in example scripts, debugger or console) the library you are unfamiliar with, Ruby's reflection is very useful mechanism to understand "what it can": classes, modules, their constants, methods and so on. I propose to expose a bit more information Ruby has internally in `Method#inspect`: ```ruby # before: some_interesting_object.method(:foo) # => # # after: some_interesting_object.method(:foo) # => # ``` Dead-naive implementation: ```ruby class Method def signature recv = case receiver when Module "#{receiver.name}." else "#{receiver.class}#" end parameters.map.with_index { |(type, name), i| case type when :req then "#{name || "param#{i+1}"}" when :opt then "#{name || "param#{i+1}"} = " when :keyreq then "#{name || "kw#{i+1}"}:" when :key then "#{name || "kwparam#{i+1}"}: " when :rest then "*#{name || "rest"}" when :keyrest then "**#{name || "kwrest"}" end }.join(', ').prepend("#{recv}#{name}(") << ")" end def inspect "#<#{self.class.name} #{signature}>" end end ``` This works "sub-optimal" for methods implemented in C, yet pretty decently for Ruby-implemented methods: ```ruby # C method, default param names [1,2,3].method(:at) # => # # Ruby method, proper param names CGI.method(:escape) # => # Addressable::URI.method(:parse) # => # Addressable::URI.method(:join) => # # We can't extract default values, but at least we can say they are there Addressable::URI.method(:heuristic_parse) # => #)> ``` If the proposal is accepted, I am ready to implement it properly in C (for all callable objects: `Method`, `UnboundMethod`, `Proc`) -- https://bugs.ruby-lang.org/