extensions 0.4.0 → 0.5.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.
@@ -1,164 +1,164 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
- #
4
- # == extensions/object.rb
5
- #
6
- # Adds methods to the builtin Object class.
7
- #
8
-
9
- require 'extensions/_base'
10
-
11
-
12
- #
13
- # Object#singleton_class
14
- #
15
- ExtensionsProject.implement(Object, :singleton_class) do
16
- class Object
17
- #
18
- # Returns the singleton class associated with this object. How useful this
19
- # is I don't know, but it's an idiom that has appeared on ruby-talk several
20
- # times.
21
- #
22
- def singleton_class
23
- class << self
24
- self
25
- end
26
- end
27
- end
28
- end
29
-
30
-
31
- #
32
- # * Object.in?
33
- # This has special treatment: it's included here and in enumerable.rb, so we don't
34
- # want a warning if it's already defined.
35
- #
36
- unless Object.method_defined?(:in?)
37
- ExtensionsProject.implement(Object, :in?) do
38
- class Object
39
- #
40
- # Test this object for inclusion in a given collection.
41
- #
42
- # 45.in? (1...100) => true
43
- #
44
- # This method is contained in <tt>object.rb</tt> and
45
- # <tt>enumerable.rb</tt>, because it logically belongs in both.
46
- #
47
- def in?(enumerable)
48
- enumerable.include?(self)
49
- end
50
- end
51
- end
52
- end
53
-
54
-
55
- #
56
- # * Object.not_nil?
57
- #
58
- ExtensionsProject.implement(Object, :not_nil?) do
59
- class Object
60
- #
61
- # The opposite of <tt>#nil?</tt>.
62
- #
63
- # "hello".not_nil? # -> true
64
- # nil.not_nil? # -> false
65
- #
66
- def not_nil?
67
- not self.nil?
68
- end
69
- end
70
- end
71
-
72
-
73
- #
74
- # * Object.non_nil?
75
- #
76
- ExtensionsProject.implement(Object, :non_nil?) do
77
- class Object
78
- #
79
- # The opposite of <tt>#nil?</tt>.
80
- #
81
- # "hello".non_nil? # -> true
82
- # nil.non_nil? # -> false
83
- #
84
- def non_nil?
85
- not self.nil?
86
- end
87
- end
88
- end
89
-
90
-
91
- #
92
- # Object#pp_s
93
- #
94
- ExtensionsProject.implement(Object, :pp_s) do
95
- require 'pp'
96
- require 'stringio'
97
- class Object
98
- #
99
- # Returns a pretty-printed string of the object. Requires libraries +pp+ and
100
- # +stringio+ from the Ruby standard library.
101
- #
102
- # The following code pretty-prints an object (much like +p+ plain-prints an
103
- # object):
104
- #
105
- # pp object
106
- #
107
- # The following code captures the pretty-printing in +str+ instead of
108
- # sending it to +STDOUT+.
109
- #
110
- # str = object.pp_s
111
- #
112
- def pp_s
113
- pps = StringIO.new
114
- PP.pp(self, pps)
115
- pps.string
116
- end
117
- end
118
- end
119
-
120
- #
121
- # Object#pp_s
122
- #
123
- ExtensionsProject.implement(Object, :define_method) do
124
- class Object
125
- #
126
- # Defines a singleton method on the object. For example, the following are
127
- # equivalent (assume <tt>o = Object.new</tt>):
128
- #
129
- # def o.add(x, y)
130
- # x + y
131
- # end
132
- #
133
- # o.define_method(:add) do |x, y|
134
- # x + y
135
- # end
136
- #
137
- # The difference is that with <tt>define_method</tt>, you can use variables
138
- # local to the _current_ scope.
139
- #
140
- # x = 5
141
- # o.define_method(:add_x) do |n|
142
- # x + n
143
- # end
144
- # o.add_x(11) # -> 16
145
- #
146
- # You can't define such a method as <tt>add_x</tt> above with <tt>def
147
- # o.add_x; x + n; end</tt>, as +def+ introduces a new scope.
148
- #
149
- # There are three ways to provide the body of the method: with a block (as
150
- # in both examples above), or with a +Proc+ or +Method+ object. See the
151
- # built-in method <tt>Module#define_method</tt> for details.
152
- #
153
- # (This method is exactly equivalent to calling <tt>Module#define_method</tt>
154
- # in the scope of the singleton class of the object.)
155
- #
156
- def define_method(*args, &block)
157
- singleton_class = class << self; self; end
158
- singleton_class.module_eval do
159
- define_method(*args, &block)
160
- end
161
- end
162
- end
163
- end
164
-
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ #
4
+ # == extensions/object.rb
5
+ #
6
+ # Adds methods to the builtin Object class.
7
+ #
8
+
9
+ require 'extensions/_base'
10
+
11
+
12
+ #
13
+ # Object#singleton_class
14
+ #
15
+ ExtensionsProject.implement(Object, :singleton_class) do
16
+ class Object
17
+ #
18
+ # Returns the singleton class associated with this object. How useful this
19
+ # is I don't know, but it's an idiom that has appeared on ruby-talk several
20
+ # times.
21
+ #
22
+ def singleton_class
23
+ class << self
24
+ self
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ #
32
+ # * Object.in?
33
+ # This has special treatment: it's included here and in enumerable.rb, so we don't
34
+ # want a warning if it's already defined.
35
+ #
36
+ unless Object.method_defined?(:in?)
37
+ ExtensionsProject.implement(Object, :in?) do
38
+ class Object
39
+ #
40
+ # Test this object for inclusion in a given collection.
41
+ #
42
+ # 45.in? (1...100) => true
43
+ #
44
+ # This method is contained in <tt>object.rb</tt> and
45
+ # <tt>enumerable.rb</tt>, because it logically belongs in both.
46
+ #
47
+ def in?(enumerable)
48
+ enumerable.include?(self)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ #
56
+ # * Object.not_nil?
57
+ #
58
+ ExtensionsProject.implement(Object, :not_nil?) do
59
+ class Object
60
+ #
61
+ # The opposite of <tt>#nil?</tt>.
62
+ #
63
+ # "hello".not_nil? # -> true
64
+ # nil.not_nil? # -> false
65
+ #
66
+ def not_nil?
67
+ not self.nil?
68
+ end
69
+ end
70
+ end
71
+
72
+
73
+ #
74
+ # * Object.non_nil?
75
+ #
76
+ ExtensionsProject.implement(Object, :non_nil?) do
77
+ class Object
78
+ #
79
+ # The opposite of <tt>#nil?</tt>.
80
+ #
81
+ # "hello".non_nil? # -> true
82
+ # nil.non_nil? # -> false
83
+ #
84
+ def non_nil?
85
+ not self.nil?
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ #
92
+ # Object#pp_s
93
+ #
94
+ ExtensionsProject.implement(Object, :pp_s) do
95
+ require 'pp'
96
+ require 'stringio'
97
+ class Object
98
+ #
99
+ # Returns a pretty-printed string of the object. Requires libraries +pp+ and
100
+ # +stringio+ from the Ruby standard library.
101
+ #
102
+ # The following code pretty-prints an object (much like +p+ plain-prints an
103
+ # object):
104
+ #
105
+ # pp object
106
+ #
107
+ # The following code captures the pretty-printing in +str+ instead of
108
+ # sending it to +STDOUT+.
109
+ #
110
+ # str = object.pp_s
111
+ #
112
+ def pp_s
113
+ pps = StringIO.new
114
+ PP.pp(self, pps)
115
+ pps.string
116
+ end
117
+ end
118
+ end
119
+
120
+ #
121
+ # Object#pp_s
122
+ #
123
+ ExtensionsProject.implement(Object, :define_method) do
124
+ class Object
125
+ #
126
+ # Defines a singleton method on the object. For example, the following are
127
+ # equivalent (assume <tt>o = Object.new</tt>):
128
+ #
129
+ # def o.add(x, y)
130
+ # x + y
131
+ # end
132
+ #
133
+ # o.define_method(:add) do |x, y|
134
+ # x + y
135
+ # end
136
+ #
137
+ # The difference is that with <tt>define_method</tt>, you can use variables
138
+ # local to the _current_ scope.
139
+ #
140
+ # x = 5
141
+ # o.define_method(:add_x) do |n|
142
+ # x + n
143
+ # end
144
+ # o.add_x(11) # -> 16
145
+ #
146
+ # You can't define such a method as <tt>add_x</tt> above with <tt>def
147
+ # o.add_x; x + n; end</tt>, as +def+ introduces a new scope.
148
+ #
149
+ # There are three ways to provide the body of the method: with a block (as
150
+ # in both examples above), or with a +Proc+ or +Method+ object. See the
151
+ # built-in method <tt>Module#define_method</tt> for details.
152
+ #
153
+ # (This method is exactly equivalent to calling <tt>Module#define_method</tt>
154
+ # in the scope of the singleton class of the object.)
155
+ #
156
+ def define_method(*args, &block)
157
+ singleton_class = class << self; self; end
158
+ singleton_class.module_eval do
159
+ define_method(*args, &block)
160
+ end
161
+ end
162
+ end
163
+ end
164
+