Skip to content

Enhanced RDoc for Enumerable - first 5 methods #4025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Enhanced RDoc for Enumerable - first 5 methods #4025

wants to merge 4 commits into from

Conversation

BurdetteLamar
Copy link
Member

@BurdetteLamar BurdetteLamar commented Jan 5, 2021

Methods:

  • grep
  • grep_v
  • count
  • find
  • find_index

I have named this branch '5'; next, containing 5 more, will be '10'.

@BurdetteLamar BurdetteLamar added the Documentation Improvements to documentation. label Jan 5, 2021
@stomar
Copy link
Contributor

stomar commented Jan 5, 2021

Please separate code blocks and lists (latter does not apply for this PR) by empty lines from the surrounding text, like it is done throughout most parts of the code base.

It doesn't matter for generated HTML, but highly improves readability of generated ri documentation and of the source code itself.

@BurdetteLamar
Copy link
Member Author

Please separate code blocks and lists (latter does not apply for this PR) by empty lines from the surrounding text, like it is done throughout most parts of the code base.

It doesn't matter for generated HTML, but highly improves readability of generated ri documentation and of the source code itself.

@stomar: After only, or both before and after?

@stomar
Copy link
Contributor

stomar commented Jan 5, 2021

Both (like in the original version).

@stomar
Copy link
Contributor

stomar commented Jan 5, 2021

Also, aligning comments for e.g. output values (# => ...) at a specific column can also increase readability, see also the previous version. (Sometimes it makes sense, sometimes not so much.)

enum.c Outdated
* grep(object) -> new_array
* grep(object) {|element| ... } -> new_array
*
* Returns a new \Array selected by a given object or block.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why "new"? (the receiver needs not be an array)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the return always a new Array?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a new Array 👍

Copy link
Contributor

@stomar stomar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BurdetteLamar @marcandre

compare to

array.join -> string
array.reverse -> new_array

In both cases the returned object is newly created, but "new_thing" is usually used when the receiver is a "thing" and the method returns a different "thing" (and not self).

enum.c Outdated
* grep(object) -> new_array
* grep(object) {|element| ... } -> new_array
*
* Returns a new \Array selected by a given object or block.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the block does not select

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@stomar stomar self-requested a review January 5, 2021 15:51
@stomar
Copy link
Contributor

stomar commented Jan 5, 2021

I do not yet understand the rigorous escaping of class names, e.g. \Array: it makes sense in the documentation of an array method, but in different modules/classes, a link to the Array class might be useful and would make the docs more discoverable.

@BurdetteLamar
Copy link
Member Author

Both (like in the original version).

Fixed.

@BurdetteLamar
Copy link
Member Author

Also, aligning comments for e.g. output values (# => ...) at a specific column can also increase readability, see also the previous version. (Sometimes it makes sense, sometimes not so much.)

Fixed.

@BurdetteLamar
Copy link
Member Author

I do not yet understand the rigorous escaping of class names, e.g. \Array: it makes sense in the documentation of an array method, but in different modules/classes, a link to the Array class might be useful and would make the docs more discoverable.

I have not changed these. I'm hoping others will comment with more specific guidelines over at method_documentation.rdoc.

@BurdetteLamar
Copy link
Member Author

@stomar, I think this is ready for your review.

@marcandre
Copy link
Member

LGTM 👍

* grep(pattern) -> new_array
* grep(pattern) {|element| ... } -> new_array
*
* Returns a new \Array of objects either selected by the given +pattern+
Copy link
Contributor

@stomar stomar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why "new" both in the call-seq and description (this seems to imply that the receiver is an array and the method returns a new, different array; see my explanation above)?
  2. Why "\Array" and not "Array" (this would produce a helpful link to the Array class)?
  3. "either selected by the given pattern or returned by the block" does not properly describe the behavior; in both cases the selection via the given pattern applies

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The returned object is in fact new, regardless of the class of the receiver.
  2. Take this up over at method_documentation.rdoc.
  3. Disagree. Returned Array is in fact either elements selected by the pattern or objects returned by the block. The 1-line summary need not specify on what terms the block is called.

Copy link
Contributor

@stomar stomar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1. yes, they are (of course) new, so no need to mention it. See the explanation already given above:

The established practice is like

array.join -> string
array.reverse -> new_array

In both cases the returned object is newly created, but "new_thing" is usually used when the receiver is a "thing" and the method returns a different "thing" (only to make clear that the method dos not return self).

There is no need to write explicitly "string.size returns a new integer ..." and similarly in most of the method docs.

3. Disagree, the selection based on the pattern takes place always, the block only maps/filters the selected elements; the new version can be misunderstood in such a way, that, with block given, no selection happens.

Copy link
Member Author

@BurdetteLamar BurdetteLamar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If I thought establish practice was immutable, I wouldn't be doing these revisions.
  2. If you have a suggested revision for the 1-liner, please put it here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1. These documentations have been improved and tweaked over the course of years, by many contributors; and there are reasons for the common practices that evolved in this process; so lightheartedly waving everything aside with such an argument seems a bit extreme.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I have an approval for this PR, and so would like to squash and merge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I agree that array is better than new_array here, since the receiver is not an array.
  2. The decision for Array vs \Array can be made by the person doing the work (so whatever @BurdetteLamar prefers in this case).
  3. either selected by the given pattern or returned by the block is ambiguous, and the literal interpretation does not accurately describe the behavior, as @stomar mentioned. I can't think of a better version that is shorter, but we may want to use a more verbose form, such as selected by the given pattern, and returned by the block if given.

@stomar
Copy link
Contributor

stomar commented Jan 6, 2021

I do not have time before tomorrow to have a closer look at the rest of the changes, but I would like to review them also.

@BurdetteLamar
Copy link
Member Author

I do not yet understand the rigorous escaping of class names, e.g. \Array: it makes sense in the documentation of an array method, but in different modules/classes, a link to the Array class might be useful and would make the docs more discoverable.

I've looked into this in the existing code. On the one hand, I seem to be the only one doing this; on the other hand, I've been doing this for will over a year (hundreds of instances) and the 12-15 reviewers have no complained. I've opened a PR about this, #4027, so let's take the discussion there to get a consensus.

@BurdetteLamar BurdetteLamar requested a review from stomar January 6, 2021 17:25
@BurdetteLamar
Copy link
Member Author

I have an approval from @marcandre, and so propose to squash and commit. Anything more can be in a new PR offering further improvements.

* grep(pattern) -> new_array
* grep(pattern) {|element| ... } -> new_array
*
* Returns a new \Array of objects either selected by the given +pattern+
Copy link
Contributor

@stomar stomar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns an Array of objects selected by the given +pattern+.

(The block version is explained below.)

* Returns a new \Array of objects either selected by the given +pattern+
* or returned by the block.
*
* With no block given, returns a new \Array containing each element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* With no block given, returns a new \Array containing each element
* With no block given, returns an \Array containing each element of the receiver

* res = c.grep(/SEEK/) { |v| IO.const_get(v) }
* res #=> [0, 1, 2]
* With block given,
* calls the block with each matching element, returns a new \Array containing each
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* calls the block with each matching element, returns a new \Array containing each
* calls the block with each matching element and returns an \Array containing each

* enum.grep(pattern) { |obj| block } -> array
* call-seq:
* grep(pattern) -> new_array
* grep(pattern) {|element| ... } -> new_array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* grep(pattern) {|element| ... } -> new_array
* grep(pattern) {|element| ... } -> array

* enum.grep_v(pattern) -> array
* enum.grep_v(pattern) { |obj| block } -> array
* call-seq:
* grep_v(pattern) -> new_array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* grep_v(pattern) -> new_array
* grep_v(pattern) -> array

* enum.find(ifnone = nil) -> an_enumerator
* call-seq:
* find(if_none_proc = nil) {|element| ... } -> obj or nil
* find(if_none_proc = nil) -> new_enumerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* find(if_none_proc = nil) -> new_enumerator
* find(if_none_proc = nil) -> enumerator

* returns the first element for which the block returns a truthy value:
*
* (0..9).find {|element| element > 2} # => 3
* (0..9).find(proc {false}) {|element| element > 12} # => false
Copy link
Contributor

@stomar stomar Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation for if_none_proc follows below, so this example should be moved down.

Missing here: example for return value nil.

* (1..10).find(-> {0}) { |i| i % 5 == 0 && i % 7 == 0 } #=> 0
* (1..100).detect { |i| i % 5 == 0 && i % 7 == 0 } #=> 35
* (1..100).find { |i| i % 5 == 0 && i % 7 == 0 } #=> 35
* With no block given, returns a new \Enumerator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* With no block given, returns a new \Enumerator:
* With no block given, returns an Enumerator:

(Link would be useful.)

* call-seq:
* find_index(object) -> integer or nil
* find_index {|element| ... } -> integer or nil
* find_index -> new_enumerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* find_index -> new_enumerator
* find_index -> enumerator

* (1..10).find_index { |i| i % 5 == 0 && i % 7 == 0 } #=> nil
* (1..100).find_index { |i| i % 5 == 0 && i % 7 == 0 } #=> 34
* (1..100).find_index(50) #=> 49
* With no argument and no block given, returns a new \Enumerator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* With no argument and no block given, returns a new \Enumerator:
* With no argument and no block given, returns an Enumerator:

* grep_v(pattern) -> new_array
* grep_v(pattern) {|element| ... } -> new_array
*
* Returns a new \Array of objects either selected by the given +pattern+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first paragraph should mention the relation to #grep (inverted); there should also be a link to Enumerable#grep. Having the same description as for grep would be very confusing.

Regarding "either ... or ..." see grep.

@BurdetteLamar
Copy link
Member Author

Basta.

@BurdetteLamar
Copy link
Member Author

So, @marcandre and @jeremyevans. How can we get things resolved here? This is an important module, deserving of good treatment.

@BurdetteLamar
Copy link
Member Author

Should have added: this can't be re-opened (repo no longer exists), but could be put into new PR.

@jeremyevans
Copy link
Contributor

Probably best to open a new PR, but please make sure it addresses @stomar's comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Improvements to documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants