Project

General

Profile

Actions

Feature #6717

closed

Method like #instance_eval that returns self (like #tap)

Added by alexeymuranov (Alexey Muranov) almost 12 years ago. Updated almost 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:46296]

Description

=begin
How about adding a method that acts like (({#instance_eval})), but returns (({self}))?

Call it (({Object#instance_tap})) for example, or (({Object#tweak!})):

class Object
def tweak!(*args, &block)
instance_eval(*args, &block); self
end
end

This can be used to initialize a hash. Instead of:

room_numbers = (1..10).each_with_object({}) { |n, o| o[n] = 100 + n }

one would write:

room_numbers = {}.tweak! { (1..10).each { |n| self[n] = 100 +n } }

Googling for this kind of method gave me this:
http://yuroyoro-blog.tumblr.com/post/24113917395/ruby-tap-instance-eval-tapeval
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0