Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd rather have this check if it's a String, instead of if it responds to :to_str; the contract (if there even is such a thing) on what
to_s
means vsto_str
is not really clear in the docs and certainly not clear to me as a reader :PI'll change this post-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.
Sure, I actually wasn't sure if I should check for the type or the method. :)
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 don't think there's any strict rule to play here, Ruby's a very flexible language in this respect. My personal habit is to either check for type or check for a method. Oddly, I've never seen this 'to_ary' and 'to_str' methods used in 7 years of ruby programming. Weird! :P
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.
For my own learning -
Poking around
String#to_str
has really poor docs "returns the receiver" as doesArray#to_ary
which says "returns self".Wondering what else has
to_ary
I asked forEnumerator#to_ary
docs, and this method doesn't exist.Range#to_ary
also does not exist.Based on this, I think
to_str
andto_ary
are weird, unclear, and we should not use them.I'll merge as-is and switch to using type checks.
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 do believe that in most cases multiple ways of getting the object type will work.
As for this...
response_to?
makes it (imho) more duck-type-like than just checking the type of object.You dont really get advantages from
respond_to?
in this case.Nevertheless
to_str
andto_ary
are no methods, they are symbols for therespond_to?
method.