From: "headius (Charles Nutter)" Date: 2013-06-24T03:51:28+09:00 Subject: [ruby-core:55612] [ruby-trunk - Feature #6373] public #self Issue #6373 has been updated by headius (Charles Nutter). phluid61 (Matthew Kerwin) wrote: > laise (Alexey Chernenkov) wrote: > > It is a VERY usefull feature! Can't understand why #tap still need to be used with block only. > > Because it's called "tap." Tap doesn't "return self", it taps into an execution flow, extracting an intermediate value for inspection without interrupting the original flow. The analogy is literally tapping a hole in a pipe, to extract liquid samples at various phases in a process. (Also think of tapping a phone line.) Changing its semantics to a straight-up "returns self" method would just make it idiosyncratic, instead of metaphoric. I still like #tap. 1. It would only require removal of the block-yielding requirement. 2. It's functionally equivalent to tapping into execution flow but doing nothing, as in tap {}. I think the objection that it no longer means we're tapping execution flow is a bit pedantic; we *are* tapping execution flow, but the only operation we seek is the reference to the object. My favorite remains #self, but I can appreciate objections, especially confusion over "self" and "obj.self" and especially "self.self" if #self can be overridden. It does have the advantage that there's probably fewer libraries that define their own "self" method than any other suggestion here. Other systems I know would use #identity. It also maps to functional programming language for a function that just returns its sole argument (in this case, the 0th argument, the object itself). I would support #identity as well. > Matz wrote: > > The point is when we see the code like: > > > > [1,2,3,4,5,1,2,2,3].group_by(&:self) > > > > sometimes it would be less intuitive that self refers elements in the array, not self in the scope. > > I have trouble imagining such a scenario. I actually think the `some_array.group_by(&:self)` example is a strong plus for this feature. > > I'm also not entirely convinced it's really YAM, since 'self' is already a word in the language, all we're doing is pushing it from keyword to keyword+method, i.e. making it more easily accessible from outside the instance. Even more so since this use-case is really the only place it will ever show up; if there was another way to to_proc the 'self' keyword to make it easy to pass to a method like group_by I'd be for that as well/instead. Another option based on matz's objection: #reference. We want a method that returns the reference to the object we're calling against. #reference seems logical. [1,2,3,4,5,1,2,2,3].group_by(&:reference) Variations on this might be #self_reference, #self_ref, #selfref. Also #self_object, #self_obj, #selfobj. #self and #identity are probably the most likely to be guessed by a new user. ---------------------------------------- Feature #6373: public #self https://bugs.ruby-lang.org/issues/6373#change-40101 Author: trans (SYSTEM ERROR) Status: Feedback Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: next minor =begin This was recently suggested to me as an extension: class Object # An identity method that provides access to an object's 'self'. # # Example: # [1,2,3,4,5,1,2,2,3].group_by(&:identity) # #=> {1=>[1, 1], 2=>[2, 2, 2], 3=>[3, 3], 4=>[4], 5=>[5]} # def identity self end end First, is such a method commonly useful enough to warrant existence? Second, it makes me wonder if #self should be a public method in general. =end -- http://bugs.ruby-lang.org/