andand 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +3 -1
- data/lib/andand.rb +70 -11
- data/lib/andand/version.rb +2 -2
- data/website/index.html +2 -2
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
== 1.
|
1
|
+
== 1.3.0 2008-07-12
|
2
2
|
|
3
3
|
* 1 major enhancement:
|
4
4
|
* Initial release
|
@@ -10,3 +10,5 @@
|
|
10
10
|
* Object#dont
|
11
11
|
* 5 bug fix
|
12
12
|
* Plays well with other patch-artists
|
13
|
+
* 6 minor enhancement
|
14
|
+
* uses existing BlankSlate class if already defined
|
data/lib/andand.rb
CHANGED
@@ -2,8 +2,24 @@ $:.unshift File.dirname(__FILE__)
|
|
2
2
|
|
3
3
|
module AndAnd
|
4
4
|
|
5
|
+
# This module is included in Object, so each of these methods are added
|
6
|
+
# to Object when you require 'andand'. Each method is an *adverb*: they are
|
7
|
+
# intended to be enchained with another method, such as receiver.adverb.method
|
8
|
+
#
|
9
|
+
# The purpose of an adverb is to modify what the primary method returns.
|
10
|
+
#
|
11
|
+
# Adverbs also take blocks or procs, passing the receiver as an argument to the
|
12
|
+
# block or proc. They retain the same semantics with a block or proc as they
|
13
|
+
# do with a method. This behaviour weakly resembles a monad.
|
5
14
|
module ObjectGoodies
|
6
15
|
|
16
|
+
# Returns nil if its receiver is nil, regardless of whether nil actually handles the
|
17
|
+
# actual method ot what it might return.
|
18
|
+
#
|
19
|
+
# 'foo'.andand.size => 3
|
20
|
+
# nil.andand.size => nil
|
21
|
+
# 'foo'.andand { |s| s << 'bar' } => 'foobar'
|
22
|
+
# nil.andand { |s| s << 'bar' } => nil
|
7
23
|
def andand (p = nil)
|
8
24
|
if self
|
9
25
|
if block_given?
|
@@ -21,7 +37,24 @@ module AndAnd
|
|
21
37
|
end
|
22
38
|
end
|
23
39
|
end
|
24
|
-
|
40
|
+
|
41
|
+
# Invokes the method and returns the receiver if nothing is raised. Therefore,
|
42
|
+
# the purpose of calling the method is strictly for side effects. In the block
|
43
|
+
# form, it resembles #tap from Ruby 1.9, and is useful for debugging. It also
|
44
|
+
# resembles #returning from Rails, with slightly different syntax.
|
45
|
+
#
|
46
|
+
# Object.new.me do |o|
|
47
|
+
# def o.foo
|
48
|
+
# 'foo'
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
# => your new object
|
52
|
+
#
|
53
|
+
# In the method form, it is handy for chaining methods that don't ordinarily
|
54
|
+
# return the receiver:
|
55
|
+
#
|
56
|
+
# [1, 2, 3, 4, 5].me.pop.reverse
|
57
|
+
# => [4, 3, 2, 1]
|
25
58
|
def me (p = nil)
|
26
59
|
if block_given?
|
27
60
|
yield(self)
|
@@ -34,8 +67,19 @@ module AndAnd
|
|
34
67
|
end
|
35
68
|
end
|
36
69
|
|
37
|
-
|
70
|
+
unless Object.instance_methods.include?('tap')
|
71
|
+
alias :tap :me
|
72
|
+
end
|
38
73
|
|
74
|
+
# Does not invoke the method or block and returns the receiver.
|
75
|
+
# Useful for comemnting stuff out, especially if you are using #me for
|
76
|
+
# debugging purposes: change the .me to .dont and the semantics of your
|
77
|
+
# program are unchanged.
|
78
|
+
#
|
79
|
+
# [1, 2, 3, 4, 5].me { |x| p x }
|
80
|
+
# => prints and returns the array
|
81
|
+
# [1, 2, 3, 4, 5].dont { |x| p x }
|
82
|
+
# => returns the array without printing it
|
39
83
|
def dont (p = nil)
|
40
84
|
if block_given?
|
41
85
|
self
|
@@ -53,26 +97,41 @@ end
|
|
53
97
|
class Object
|
54
98
|
include AndAnd::ObjectGoodies
|
55
99
|
end
|
100
|
+
|
101
|
+
unless Module.constants.include?('BlankSlate')
|
102
|
+
module AndAnd
|
103
|
+
class BlankSlate
|
104
|
+
def self.wipe
|
105
|
+
instance_methods.reject { |m| m =~ /^__/ }.each { |m| undef_method m }
|
106
|
+
end
|
107
|
+
def initialize
|
108
|
+
BlankSlate.wipe
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
56
113
|
|
57
114
|
module AndAnd
|
58
115
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
116
|
+
# A proxy that returns its target without invoking the method you
|
117
|
+
# invoke. Useful for nil.andand and #dont
|
118
|
+
class MockReturningMe < BlankSlate
|
63
119
|
def initialize(me)
|
64
|
-
|
120
|
+
super()
|
65
121
|
@me = me
|
66
122
|
end
|
67
|
-
end
|
68
|
-
|
69
|
-
class MockReturningMe < BlankSlate
|
70
123
|
def method_missing(*args)
|
71
124
|
@me
|
72
125
|
end
|
73
126
|
end
|
74
|
-
|
127
|
+
|
128
|
+
# A proxy that returns its target after invoking the method you
|
129
|
+
# invoke. Useful for #me
|
75
130
|
class ProxyReturningMe < BlankSlate
|
131
|
+
def initialize(me)
|
132
|
+
super()
|
133
|
+
@me = me
|
134
|
+
end
|
76
135
|
def method_missing(sym, *args, &block)
|
77
136
|
@me.__send__(sym, *args, &block)
|
78
137
|
@me
|
data/lib/andand/version.rb
CHANGED
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>Object#andand</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/andand"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/andand" class="numbers">1.
|
36
|
+
<a href="http://rubyforge.org/projects/andand" class="numbers">1.3.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘andand’</h1>
|
39
39
|
|
@@ -217,7 +217,7 @@
|
|
217
217
|
|
218
218
|
<p>Comments are welcome. Send an email to <a href="mailto:raganwald+rubyforge@gmail.com">Reginald Braithwaite</a>.</p>
|
219
219
|
<p class="coda">
|
220
|
-
<a href="http://weblog.raganwald.com">Reginald Braithwaite</a>,
|
220
|
+
<a href="http://weblog.raganwald.com">Reginald Braithwaite</a>, 12th July 2008<br>
|
221
221
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
222
222
|
</p>
|
223
223
|
</div>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: andand
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reginald Braithwaite
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-13 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|