-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafter_sending.rb
More file actions
43 lines (42 loc) · 1.08 KB
/
after_sending.rb
File metadata and controls
43 lines (42 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Object
# Return the receiver after sending it a message.
#
# Usage:
# class Example; def sideeffect!; puts "Hello"; end; end
#
# Example.new.after_sending{ sideeffect! }
# Hello
# => "#<Example:...>"
#
# Questionable abbreviated usage:
#
# Example.new.after_sending.sideeffect!
# Hello
# => "#<Example:...>"
#
# Real-world example from a unit test of a Rails model:
#
# assert_equals bogus_model.after_sending{valid?}.errors[:base], "totally bogus!"
#
# Similar to standard Object#tap, but with a better name and better syntax,
# since we evaluate the block with self set to the receiver, instead of
# passing the receiver in as an argument.
def after_sending(&block)
if block.nil?
AfterSender.new self
else
self.instance_eval &block
self
end
end
end
# Helper class used by Object#after_sending. Nothing to see here.
class AfterSender
def initialize(receiver)
@receiver = receiver
end
def method_missing(name, *args, &block)
@receiver.send name, *args, &block
@receiver
end
end