blackwinter-ruby-nuggets 0.4.5 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/Rakefile +7 -6
- data/lib/nuggets/all.rb +2 -0
- data/lib/nuggets/all_mixins.rb +34 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +9 -14
- data/lib/nuggets/enumerable/minmax.rb +5 -0
- data/lib/nuggets/file/which.rb +2 -71
- data/lib/nuggets/file/which_mixin.rb +60 -0
- data/lib/nuggets/integer/length.rb +5 -0
- data/lib/nuggets/integer/length_mixin.rb +50 -0
- data/lib/nuggets/object/blank.rb +10 -109
- data/lib/nuggets/object/blank_mixin.rb +100 -0
- data/lib/nuggets/object/boolean.rb +2 -66
- data/lib/nuggets/object/boolean_mixin.rb +62 -0
- data/lib/nuggets/object/msend.rb +2 -52
- data/lib/nuggets/object/msend_mixin.rb +44 -0
- data/lib/nuggets/object/silence.rb +5 -0
- data/lib/nuggets/object/silence_mixin.rb +45 -0
- data/lib/nuggets/object/singleton_class.rb +2 -149
- data/lib/nuggets/object/singleton_class_mixin.rb +96 -0
- data/lib/nuggets/proc/bind.rb +2 -65
- data/lib/nuggets/proc/bind_mixin.rb +52 -0
- data/lib/nuggets/tempfile/open.rb +21 -17
- data/lib/nuggets/uri/content_type.rb +2 -62
- data/lib/nuggets/uri/content_type_mixin.rb +47 -0
- data/lib/nuggets/uri/exist.rb +2 -60
- data/lib/nuggets/uri/exist_mixin.rb +48 -0
- data/lib/nuggets/version.rb +2 -2
- data/lib/nuggets.rb +2 -2
- metadata +17 -4
@@ -0,0 +1,62 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Object
|
30
|
+
module BooleanMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# object.boolean? => true or false
|
34
|
+
#
|
35
|
+
#
|
36
|
+
def boolean?
|
37
|
+
is_a?(::TrueClass) || is_a?(::FalseClass)
|
38
|
+
end
|
39
|
+
|
40
|
+
# call-seq:
|
41
|
+
# object.negate => true or false
|
42
|
+
#
|
43
|
+
#
|
44
|
+
def negate
|
45
|
+
!self
|
46
|
+
end
|
47
|
+
|
48
|
+
alias_method :false?, :negate
|
49
|
+
|
50
|
+
# call-seq:
|
51
|
+
# object.to_bool => true or false
|
52
|
+
#
|
53
|
+
#
|
54
|
+
def to_bool
|
55
|
+
!!self
|
56
|
+
end
|
57
|
+
|
58
|
+
alias_method :true?, :to_bool
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/nuggets/object/msend.rb
CHANGED
@@ -1,55 +1,5 @@
|
|
1
|
-
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU General Public License as published by the Free #
|
14
|
-
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
-
# any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
-
# more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU General Public License along #
|
23
|
-
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/object/msend_mixin'
|
27
2
|
|
28
3
|
class Object
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# object.msend(*messages) => anArray
|
32
|
-
#
|
33
|
-
# Sends _object_ multiple +messages+ and returns an array of the individual
|
34
|
-
# return values.
|
35
|
-
def msend(*messages)
|
36
|
-
hash = messages.last.is_a?(Hash) ? messages.pop : {}
|
37
|
-
(messages + hash.to_a).map { |msg| send *msg.is_a?(Array) ? msg : [msg] }
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
if $0 == __FILE__
|
43
|
-
o = 'foo bar'
|
44
|
-
p o
|
45
|
-
p o.msend(:length, :reverse)
|
46
|
-
|
47
|
-
o = 42
|
48
|
-
p o
|
49
|
-
p o.msend(:to_s, :* => 2)
|
50
|
-
p o.msend([:to_s, 2], '-@')
|
51
|
-
|
52
|
-
o = Time.now
|
53
|
-
p o
|
54
|
-
p o.msend(:year, :month, :day)
|
4
|
+
include Nuggets::Object::MSendMixin
|
55
5
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Object
|
30
|
+
module MSendMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# object.msend(*messages) => anArray
|
34
|
+
#
|
35
|
+
# Sends _object_ multiple +messages+ and returns an array of the individual
|
36
|
+
# return values.
|
37
|
+
def msend(*messages)
|
38
|
+
hash = messages.last.is_a?(::Hash) ? messages.pop : {}
|
39
|
+
(messages + hash.to_a).map { |msg| send(*msg.is_a?(::Array) ? msg : [msg]) }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Object
|
30
|
+
module SilenceMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# silence { ... }
|
34
|
+
#
|
35
|
+
# Silence warnings for block execution.
|
36
|
+
def silence
|
37
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
38
|
+
yield
|
39
|
+
ensure
|
40
|
+
$VERBOSE = verbose
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -1,152 +1,5 @@
|
|
1
|
-
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU General Public License as published by the Free #
|
14
|
-
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
-
# any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
-
# more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU General Public License along #
|
23
|
-
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/object/singleton_class_mixin'
|
27
2
|
|
28
3
|
class Object
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# object.singleton_class => aClass
|
32
|
-
#
|
33
|
-
# Returns the singleton (or virtual/eigen/meta) class associated with _object_.
|
34
|
-
def singleton_class
|
35
|
-
class << self; self; end
|
36
|
-
end
|
37
|
-
|
38
|
-
alias_method :virtual_class, :singleton_class
|
39
|
-
alias_method :ghost_class, :singleton_class
|
40
|
-
alias_method :eigenclass, :singleton_class
|
41
|
-
alias_method :metaclass, :singleton_class
|
42
|
-
alias_method :uniclass, :singleton_class
|
43
|
-
|
44
|
-
# call-seq:
|
45
|
-
# object.singleton_object => anObject
|
46
|
-
#
|
47
|
-
# Returns the object of which _object_ is the singleton_class.
|
48
|
-
# Raises a TypeError if _object_ is not a singleton class.
|
49
|
-
def singleton_object
|
50
|
-
[true, false, nil].each { |obj|
|
51
|
-
return obj if self.equal?(obj.singleton_class)
|
52
|
-
}
|
53
|
-
|
54
|
-
# raises TypeError if neither class nor module
|
55
|
-
ObjectSpace.each_object(self) { |obj|
|
56
|
-
return obj if self.equal?(obj.singleton_class)
|
57
|
-
}
|
58
|
-
|
59
|
-
# if we got here, it can't be a singleton class
|
60
|
-
# or its singleton object doesn't exist anymore
|
61
|
-
raise TypeError
|
62
|
-
rescue TypeError
|
63
|
-
raise TypeError, 'not a singleton class'
|
64
|
-
end
|
65
|
-
|
66
|
-
alias_method :virtual_object, :singleton_object
|
67
|
-
alias_method :ghost_object, :singleton_object
|
68
|
-
alias_method :eigenobject, :singleton_object
|
69
|
-
alias_method :metaobject, :singleton_object
|
70
|
-
alias_method :uniobject, :singleton_object
|
71
|
-
|
72
|
-
alias_method :singleton_instance, :singleton_object
|
73
|
-
|
74
|
-
# call-seq:
|
75
|
-
# object.singleton_class? => true or false
|
76
|
-
#
|
77
|
-
# Returns true if _object_ is a singleton_class
|
78
|
-
# (i.e., has a singleton_object), false otherwise.
|
79
|
-
def singleton_class?
|
80
|
-
singleton_object
|
81
|
-
true
|
82
|
-
rescue TypeError
|
83
|
-
false
|
84
|
-
end
|
85
|
-
|
86
|
-
alias_method :virtual_class?, :singleton_class?
|
87
|
-
alias_method :ghost_class?, :singleton_class?
|
88
|
-
alias_method :eigenclass?, :singleton_class?
|
89
|
-
alias_method :metaclass?, :singleton_class?
|
90
|
-
alias_method :uniclass?, :singleton_class?
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
if $0 == __FILE__
|
95
|
-
o = Object.new
|
96
|
-
p o
|
97
|
-
p o.singleton_class?
|
98
|
-
|
99
|
-
begin
|
100
|
-
p o.singleton_object
|
101
|
-
rescue TypeError => err
|
102
|
-
warn err
|
103
|
-
end
|
104
|
-
|
105
|
-
s = o.singleton_class
|
106
|
-
p s
|
107
|
-
p s.singleton_class?
|
108
|
-
p s.singleton_object
|
109
|
-
|
110
|
-
###
|
111
|
-
|
112
|
-
o = [1, 2]
|
113
|
-
p o
|
114
|
-
|
115
|
-
s = o.singleton_class
|
116
|
-
p s
|
117
|
-
p s.singleton_class?
|
118
|
-
p s.singleton_object
|
119
|
-
|
120
|
-
###
|
121
|
-
|
122
|
-
p Class.new.singleton_class?
|
123
|
-
p Class.singleton_class?
|
124
|
-
|
125
|
-
###
|
126
|
-
|
127
|
-
c = Class.new
|
128
|
-
o = c.new
|
129
|
-
p o
|
130
|
-
p c.singleton_class?
|
131
|
-
|
132
|
-
###
|
133
|
-
|
134
|
-
p nil.singleton_class
|
135
|
-
p NilClass.singleton_class?
|
136
|
-
p NilClass.singleton_object
|
137
|
-
|
138
|
-
###
|
139
|
-
|
140
|
-
class A # :nodoc:
|
141
|
-
end
|
142
|
-
class B < A # :nodoc:
|
143
|
-
end
|
144
|
-
|
145
|
-
a = A.singleton_class
|
146
|
-
b = B.singleton_class
|
147
|
-
|
148
|
-
p a
|
149
|
-
p a.singleton_object #=> A
|
150
|
-
p b
|
151
|
-
p b.singleton_object #=> B
|
4
|
+
include Nuggets::Object::SingletonClassMixin
|
152
5
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Object
|
30
|
+
module SingletonClassMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# object.singleton_class => aClass
|
34
|
+
#
|
35
|
+
# Returns the singleton (or virtual/eigen/meta) class associated with _object_.
|
36
|
+
def singleton_class
|
37
|
+
class << self; self; end
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :virtual_class, :singleton_class
|
41
|
+
alias_method :ghost_class, :singleton_class
|
42
|
+
alias_method :eigenclass, :singleton_class
|
43
|
+
alias_method :metaclass, :singleton_class
|
44
|
+
alias_method :uniclass, :singleton_class
|
45
|
+
|
46
|
+
# call-seq:
|
47
|
+
# object.singleton_object => anObject
|
48
|
+
#
|
49
|
+
# Returns the object of which _object_ is the singleton_class.
|
50
|
+
# Raises a TypeError if _object_ is not a singleton class.
|
51
|
+
def singleton_object
|
52
|
+
[true, false, nil].each { |obj|
|
53
|
+
return obj if self.equal?(obj.singleton_class)
|
54
|
+
}
|
55
|
+
|
56
|
+
# raises TypeError if neither class nor module
|
57
|
+
ObjectSpace.each_object(self) { |obj|
|
58
|
+
return obj if self.equal?(obj.singleton_class)
|
59
|
+
}
|
60
|
+
|
61
|
+
# if we got here, it can't be a singleton class
|
62
|
+
# or its singleton object doesn't exist anymore
|
63
|
+
raise TypeError
|
64
|
+
rescue TypeError
|
65
|
+
raise TypeError, 'not a singleton class'
|
66
|
+
end
|
67
|
+
|
68
|
+
alias_method :virtual_object, :singleton_object
|
69
|
+
alias_method :ghost_object, :singleton_object
|
70
|
+
alias_method :eigenobject, :singleton_object
|
71
|
+
alias_method :metaobject, :singleton_object
|
72
|
+
alias_method :uniobject, :singleton_object
|
73
|
+
|
74
|
+
alias_method :singleton_instance, :singleton_object
|
75
|
+
|
76
|
+
# call-seq:
|
77
|
+
# object.singleton_class? => true or false
|
78
|
+
#
|
79
|
+
# Returns true if _object_ is a singleton_class
|
80
|
+
# (i.e., has a singleton_object), false otherwise.
|
81
|
+
def singleton_class?
|
82
|
+
singleton_object
|
83
|
+
true
|
84
|
+
rescue TypeError
|
85
|
+
false
|
86
|
+
end
|
87
|
+
|
88
|
+
alias_method :virtual_class?, :singleton_class?
|
89
|
+
alias_method :ghost_class?, :singleton_class?
|
90
|
+
alias_method :eigenclass?, :singleton_class?
|
91
|
+
alias_method :metaclass?, :singleton_class?
|
92
|
+
alias_method :uniclass?, :singleton_class?
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/nuggets/proc/bind.rb
CHANGED
@@ -1,68 +1,5 @@
|
|
1
|
-
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU General Public License as published by the Free #
|
14
|
-
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
-
# any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
-
# more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU General Public License along #
|
23
|
-
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/proc/bind_mixin'
|
27
2
|
|
28
3
|
class Proc
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# proc.bind(object) => aMethod
|
32
|
-
#
|
33
|
-
# Straight from Rails' ActiveSupport -- effectively binds _proc_ to +object+.
|
34
|
-
def bind(object)
|
35
|
-
block, time = self, Time.now
|
36
|
-
|
37
|
-
(class << object; self; end).class_eval {
|
38
|
-
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
39
|
-
define_method(method_name, &block)
|
40
|
-
|
41
|
-
method = instance_method(method_name)
|
42
|
-
remove_method(method_name)
|
43
|
-
|
44
|
-
method
|
45
|
-
}.bind(object)
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
if $0 == __FILE__
|
51
|
-
l = lambda { bla }
|
52
|
-
|
53
|
-
begin
|
54
|
-
l.call
|
55
|
-
rescue NameError => err
|
56
|
-
puts "#{err} (#{err.class})"
|
57
|
-
end
|
58
|
-
|
59
|
-
module Bla # :nodoc:
|
60
|
-
|
61
|
-
def self.bla
|
62
|
-
puts 'blub'
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
l.bind(Bla).call
|
4
|
+
include Nuggets::Proc::BindMixin
|
68
5
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Proc
|
30
|
+
module BindMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# proc.bind(object) => aMethod
|
34
|
+
#
|
35
|
+
# Straight from Rails' ActiveSupport -- effectively binds _proc_ to +object+.
|
36
|
+
def bind(object)
|
37
|
+
block, time = self, ::Time.now
|
38
|
+
|
39
|
+
(class << object; self; end).class_eval {
|
40
|
+
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
41
|
+
define_method(method_name, &block)
|
42
|
+
|
43
|
+
method = instance_method(method_name)
|
44
|
+
remove_method(method_name)
|
45
|
+
|
46
|
+
method
|
47
|
+
}.bind(object)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -29,26 +29,30 @@ require 'tempfile'
|
|
29
29
|
|
30
30
|
class Tempfile
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
32
|
+
class << self
|
33
|
+
|
34
|
+
alias_method :_nuggets_original_open, :open
|
35
|
+
|
36
|
+
# If no block is given, this is a synonym for new().
|
37
|
+
#
|
38
|
+
# If a block is given, it will be passed tempfile as an argument,
|
39
|
+
# and the tempfile will automatically be closed when the block
|
40
|
+
# terminates. In this case, open() returns tempfile -- in contrast
|
41
|
+
# to the original implementation, which returns nil.
|
42
|
+
def open(*args)
|
43
|
+
tempfile = new(*args)
|
44
|
+
|
45
|
+
if block_given?
|
46
|
+
begin
|
47
|
+
yield tempfile
|
48
|
+
ensure
|
49
|
+
tempfile.close
|
50
|
+
end
|
48
51
|
end
|
52
|
+
|
53
|
+
tempfile
|
49
54
|
end
|
50
55
|
|
51
|
-
tempfile
|
52
56
|
end
|
53
57
|
|
54
58
|
end
|