-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Conversation
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 |
@stomar: After only, or both before and after? |
Both (like in the original version). |
Also, aligning comments for e.g. output values ( |
enum.c
Outdated
* grep(object) -> new_array | ||
* grep(object) {|element| ... } -> new_array | ||
* | ||
* Returns a new \Array selected by a given object or block. |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
I do not yet understand the rigorous escaping of class names, e.g. |
Fixed. |
Fixed. |
I have not changed these. I'm hoping others will comment with more specific guidelines over at method_documentation.rdoc. |
@stomar, I think this is ready for your review. |
LGTM 👍 |
* grep(pattern) -> new_array | ||
* grep(pattern) {|element| ... } -> new_array | ||
* | ||
* Returns a new \Array of objects either selected by the given +pattern+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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)?
- Why "\Array" and not "Array" (this would produce a helpful link to the Array class)?
- "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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The returned object is in fact new, regardless of the class of the receiver.
- Take this up over at method_documentation.rdoc.
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- If I thought establish practice was immutable, I wouldn't be doing these revisions.
- If you have a suggested revision for the 1-liner, please put it here.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I agree that
array
is better thannew_array
here, since the receiver is not an array. - The decision for
Array
vs\Array
can be made by the person doing the work (so whatever @BurdetteLamar prefers in this case). 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 asselected by the given pattern, and returned by the block if given
.
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. |
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. |
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+ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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+ |
There was a problem hiding this comment.
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.
Basta. |
So, @marcandre and @jeremyevans. How can we get things resolved here? This is an important module, deserving of good treatment. |
Should have added: this can't be re-opened (repo no longer exists), but could be put into new PR. |
Probably best to open a new PR, but please make sure it addresses @stomar's comments. |
Methods:
I have named this branch '5'; next, containing 5 more, will be '10'.