ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:88445] [Ruby trunk Feature#14609] `Kernel#p` without args shows the receiver
Date: Sat, 11 Aug 2018 03:29:42 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-73500.20180811032940.5bbdbe667636aa20@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14609.20180316012909@ruby-lang.org

Issue #14609 has been updated by nobu (Nobuyoshi Nakada).


osyo (manga osyo) wrote:
> Good proposal.
> This can solve the following problem.
> 
> ```ruby
> # NG: syntax error, unexpected ':', expecting '}'
> p { name: "homu", age: 14 }

You can use parentheses.
```ruby
p(name: "home", age: 14)
```



----------------------------------------
Feature #14609: `Kernel#p` without args shows the receiver
https://bugs.ruby-lang.org/issues/14609#change-73500

* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 2.6
----------------------------------------
# Abstract

`Kernel#p(obj)` prints obj as `inspect`ed.
How about to show the receiver if an argument is not given?

# Background

We recently introduce `yield_self` which encourages block chain.

https://zverok.github.io/blog/2018-01-24-yield_self.html
Quoted from this article, we can write method chain with blocks:

```
construct_url
  .yield_self { |url| Faraday.get(url) }.body
  .yield_self { |response| JSON.parse(response) }
  .dig('object', 'id')
  .yield_self { |id| id || '<undefined>' }
  .yield_self { |id| "server:#{id}" }
```

There is a small problem at debugging.
If we want to see the middle values in method/block chain, we need to insert `tap{|e| p e}`.

With above example,

```
construct_url
  .yield_self { |url| Faraday.get(url) }.body
  .yield_self { |response| JSON.parse(response) }.tap{|e| p e} # debug print
  .dig('object', 'id')
  .yield_self { |id| id || '<undefined>' }.tap{|e| p e} # debug print
  .yield_self { |id| "server:#{id}" }
```

# Proposal

`obj.p` shows same as `p(obj)`.

We can replace
  `block{...}.tap{|e| p e}`
to 
  `block{...}.p`

For above example, we can simply add `.p` at the end of line:

```
construct_url
  .yield_self { |url| Faraday.get(url) }.body
  .yield_self { |response| JSON.parse(response) }.p # debug print
  .dig('object', 'id')
  .yield_self { |id| id || '<undefined>' }.p # debug print
  .yield_self { |id| "server:#{id}" }
```

# Compatibility issue

(1) Shorthand for `nil`

This spec change can introduce compatibility issue because `p` returns `nil` and do not output anything.
That is to say, `p` is shorthand of `nil`. Some code-golfers use it.

Maybe we can ignore them :p

(2) make public method

`Kernel#p` is private method, so if we typo `obj.x` to `obj.p` (not sure how it is feasible), it will be `NoMethodError` because of visibility.
We need to change this behavior.

# Note

## Past proposal and discussion

Endoh-san proposed same idea 10+ years ago [ruby-dev:29736] in Japanese.
I think we should revisit this idea because of `yield_self` introduction.

At this thread, Matz said "simple `p` shows `p(self)`, it is not clear".

[ruby-dev:30903]

```
  p

はどう動くのかとか(p selfと同じ、は変な気が)

  self.p(obj)

はどうなのかとか。その辺が解決(納得)できたら、ということで。
```

English translation:

```
What the behavior of (I feel strange that it is similar to `p(self)`):

  p

What happen on

  self.p(obj)
```

## pp

If this proposal is accepted, we also need to change `pp` behavior.

## gems

`tapp` method is provided by gem.
https://github.com/esminc/tapp 

I'd thought to propose this method into core. But I found that `p` is more shorter than `tapp`.
Disadvantage is `p` is too short and difficult to grep.




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

  parent reply	other threads:[~2018-08-11  3:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14609.20180316012909@ruby-lang.org>
2018-03-16  1:29 ` [ruby-core:86152] [Ruby trunk Feature#14609] `Kernel#p` without args shows the receiver ko1
2018-03-16  3:11 ` [ruby-core:86153] " mame
2018-03-16  8:00 ` [ruby-core:86155] " zverok.offline
2018-03-16 10:55 ` [ruby-core:86156] " hanmac
2018-08-10  7:03 ` [ruby-core:88426] " ko1
2018-08-10 13:09 ` [ruby-core:88438] " hanmac
2018-08-11  1:49 ` [ruby-core:88444] " manga.osyo
2018-08-11  3:29 ` nobu [this message]
2018-08-18 17:59 ` [ruby-core:88546] " shevegen
2018-09-13  6:04 ` [ruby-core:88971] " nobu
2018-09-13  6:16 ` [ruby-core:88973] " matz
2018-10-13 13:57 ` [ruby-core:89400] " lukas
2018-10-16 11:13 ` [ruby-core:89416] " zverok.offline
2018-10-16 13:10 ` [ruby-core:89419] " nobu
2018-10-16 13:18 ` [ruby-core:89420] " zverok.offline
2018-10-17 16:21 ` [ruby-core:89449] " ko1
2019-03-11  4:50 ` [ruby-core:91744] " akr
2019-03-11  4:54 ` [ruby-core:91745] " nobu
2019-03-11  4:59 ` [ruby-core:91746] " ko1

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-list from there: mbox

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

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

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

  git send-email \
    --in-reply-to=redmine.journal-73500.20180811032940.5bbdbe667636aa20@ruby-lang.org \
    --to=ruby-core@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 read-only IMAP folder(s) and NNTP newsgroup(s).