ruby-nuggets 0.0.1.126

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.
@@ -0,0 +1,46 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ class Object
29
+
30
+ # call-seq:
31
+ # object.singleton_class => aClass
32
+ #
33
+ # Returns the singleton class associated with _object_.
34
+ def singleton_class
35
+ class << self; self; end
36
+ end
37
+ alias_method :eigenclass, :singleton_class
38
+ alias_method :metaclass, :singleton_class
39
+
40
+ end
41
+
42
+ if $0 == __FILE__
43
+ o = Object.new
44
+ p o
45
+ p o.singleton_class
46
+ end
@@ -0,0 +1,104 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ class String
29
+
30
+ module Case
31
+
32
+ LOWER = :lower
33
+ UPPER = :upper
34
+ MIXED = :mixed
35
+
36
+ end
37
+
38
+ # call-seq:
39
+ # str.case => aSymbol
40
+ #
41
+ # Returns a symbol indicating the case of _str_.
42
+ def case
43
+ self == downcase ? Case::LOWER :
44
+ self == upcase ? Case::UPPER :
45
+ Case::MIXED
46
+ end
47
+
48
+ # call-seq:
49
+ # str.lower_case? => true or false
50
+ #
51
+ # Tell whether _str_ is all lower case.
52
+ def lower_case?
53
+ self.case == Case::LOWER
54
+ end
55
+ alias_method :downcase?, :lower_case?
56
+
57
+ # call-seq:
58
+ # str.upper_case? => true or false
59
+ #
60
+ # Tell whether _str_ is all upper case.
61
+ def upper_case?
62
+ self.case == Case::UPPER
63
+ end
64
+ alias_method :upcase?, :upper_case?
65
+
66
+ # call-seq:
67
+ # str.mixed_case? => true or false
68
+ #
69
+ # Tell whether _str_ is mixed case.
70
+ def mixed_case?
71
+ self.case == Case::MIXED
72
+ end
73
+
74
+ # call-seq:
75
+ # str.capitalized? => true or false
76
+ #
77
+ # Tell whether _str_ is capitalized.
78
+ def capitalized?
79
+ self == capitalize
80
+ end
81
+
82
+ end
83
+
84
+ if $0 == __FILE__
85
+ s = 'Some string'
86
+ puts s
87
+ p s.case
88
+ puts s.downcase?
89
+ puts s.upcase?
90
+ puts s.mixed_case?
91
+ puts s.capitalized?
92
+
93
+ s = 'some string'
94
+ puts s
95
+ p s.case
96
+ puts s.downcase?
97
+ puts s.mixed_case?
98
+
99
+ s = 'SOME STRING'
100
+ puts s
101
+ p s.case
102
+ puts s.upcase?
103
+ puts s.mixed_case?
104
+ end
@@ -0,0 +1,53 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ class String
29
+
30
+ # call-seq:
31
+ # str.evaluate(binding = TOPLEVEL_BINDING) => new_str
32
+ #
33
+ # Basically turns Kernel#eval into an instance method of String -- inspired
34
+ # by Ruby Cookbook example 1.3. This allows to pre-populate strings with
35
+ # substitution expressions ("#{...}") that can get evaluated in a different
36
+ # environment (= +binding+) at a later point.
37
+ def evaluate(binding = TOPLEVEL_BINDING)
38
+ eval(%Q{"#{self}"}, binding)
39
+ end
40
+
41
+ end
42
+
43
+ if $0 == __FILE__
44
+ s = 'bl#{a}blub'
45
+ p s
46
+
47
+ def foo(bar) # :nodoc:
48
+ a = 'ub'
49
+ bar.evaluate(binding)
50
+ end
51
+
52
+ p foo(s)
53
+ end
@@ -0,0 +1,82 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ require File.join(File.dirname(__FILE__), 'evaluate')
29
+
30
+ class String
31
+
32
+ # call-seq:
33
+ # str.msub(*substitutions) => new_str
34
+ #
35
+ # Performs _multiple_ substitutions on _str_ with order being taken into
36
+ # account (thus results of previous substitutions won't be subject to later
37
+ # ones) -- inspired by Ruby Cookbook example 1.18.
38
+ #
39
+ # The +substitutions+ parameter can be an array or a list of <tt>[pattern,
40
+ # substitution]</tt> pairs, or, simply, a hash. Note that, when using a hash,
41
+ # the ordering of how substitutions are processed might differ from what you
42
+ # intended -- instead use an array when order matters. +pattern+ can be a
43
+ # string or a regexp, +substitution+ may contain string expressions (cf.
44
+ # #evaluate).
45
+ def msub(*substitutions)
46
+ dup.msub!(*substitutions) || dup
47
+ end
48
+
49
+ # call-seq:
50
+ # str.msub!(*substitutions) => str or nil
51
+ #
52
+ # Destructive version of #msub.
53
+ def msub!(*substitutions)
54
+ substitutions = *substitutions # Handle hashes and arrays alike
55
+ keys, subs, cache = [], [], {}
56
+
57
+ substitutions.each { |key, value|
58
+ key = Regexp.new(Regexp.escape(key)) unless key.is_a?(Regexp)
59
+
60
+ keys << key
61
+ subs << [key, value]
62
+ }
63
+
64
+ gsub!(Regexp.union(*keys)) { |match|
65
+ cache[match] ||= subs.find { |key,|
66
+ key =~ match
67
+ }.last.evaluate(binding)
68
+ }
69
+ end
70
+
71
+ end
72
+
73
+ if $0 == __FILE__
74
+ s = 'Foo, Bar - Baz'
75
+ p s
76
+
77
+ p s.gsub(/a/, 'o').gsub(/o/, 'a')
78
+ p s.msub('a' => 'o', 'o' => 'a')
79
+
80
+ s.msub!('a' => 'o','o' => 'a')
81
+ p s
82
+ end
@@ -0,0 +1,82 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ class String
29
+
30
+ # call-seq:
31
+ # str.nsub(pattern, replacement, count) => new_str
32
+ # str.nsub(pattern, count) { |match| ... } => new_str
33
+ #
34
+ # Returns a copy of _str_ with the _first_ +count+ occurrences of pattern
35
+ # replaced with either +replacement+ or the value of the block.
36
+ def nsub(*args, &block)
37
+ dup.nsub!(*args, &block) || dup
38
+ end
39
+
40
+ # call-seq:
41
+ # str.nsub!(pattern, replacement, count) => str or nil
42
+ # str.nsub!(pattern, count) { |match| ... } => str or nil
43
+ #
44
+ # Performs the substitutions of #nsub in place, returning _str_, or +nil+ if
45
+ # no substitutions were performed.
46
+ def nsub!(*args, &block)
47
+ case args.size
48
+ when 2
49
+ pattern = args.shift
50
+
51
+ # Only +count+ given; require block
52
+ count = *args
53
+ raise(ArgumentError, 'no block given') unless block_given?
54
+ when 3
55
+ pattern = args.shift
56
+
57
+ # Both +replacement+ and +count+ given;
58
+ # ignore block (just like String#gsub does)
59
+ replacement, count = *args
60
+ block = lambda { replacement }
61
+ else
62
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
63
+ end
64
+
65
+ i = 0
66
+ gsub!(pattern) { |match|
67
+ (i += 1) <= count ? block[match] : match
68
+ }
69
+ end
70
+
71
+ end
72
+
73
+ if $0 == __FILE__
74
+ s = 'a b c d e f g h i'
75
+ puts s
76
+
77
+ puts s.nsub(' ', '', 6)
78
+ puts s.nsub(' ', 6) { '' }
79
+
80
+ s.nsub!(' ', '', 6)
81
+ puts s
82
+ end
@@ -0,0 +1,111 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007 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
+ require 'enumerator'
29
+
30
+ class String
31
+
32
+ # call-seq:
33
+ # str.word_wrap(line_width) => new_str
34
+ #
35
+ # Word wrap a string not exceeding +line_width+. Based on the Ruby Facets
36
+ # implementation, but preserves paragraphs. Thus
37
+ # <tt>str == str.word_wrap(str.split("\n").map { |l| l.length }.max)</tt>.
38
+ def word_wrap(line_width = 80, as_array = false)
39
+ wrapped = []
40
+
41
+ split(/(\n+)/).to_enum(:each_slice, 2).each { |paragraph, linebreaks|
42
+ wrapped << paragraph.word_wrap_paragraph!(line_width) << linebreaks
43
+ }
44
+
45
+ wrapped = wrapped.join
46
+
47
+ as_array ? wrapped.split("\n") : wrapped
48
+ end
49
+
50
+ # call-seq:
51
+ # str.word_wrap!(line_width) => str
52
+ #
53
+ # As with #word_wrap, but modifies the string in place.
54
+ def word_wrap!(line_width = 80)
55
+ replace word_wrap(line_width)
56
+ end
57
+
58
+ # call-seq:
59
+ # str.word_wrap_paragraph(line_width) => new_str
60
+ #
61
+ # Similar to #word_wrap, but assumes a single paragraph.
62
+ def word_wrap_paragraph(line_width = 80)
63
+ dup.word_wrap_paragraph!(line_width) || dup
64
+ end
65
+
66
+ # call-seq:
67
+ # str.word_wrap_paragraph!(line_width) => str
68
+ #
69
+ # Destructive version of #word_wrap_paragraph.
70
+ def word_wrap_paragraph!(line_width = 80)
71
+ gsub!(/(.{1,#{line_width}})(?:\s+|$)/, "\\1\n")
72
+ sub!(/\n$/, '')
73
+
74
+ self
75
+ end
76
+
77
+ end
78
+
79
+ if $0 == __FILE__
80
+ s = <<EOT
81
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam nulla arcu,
82
+ ullamcorper non, vulputate eget, elementum quis, sapien. Quisque consequat
83
+ porta enim. Phasellus porta libero et turpis. Ut felis.
84
+
85
+ Phasellus eget est a enim rutrum accumsan. Integer nec purus. Maecenas
86
+ facilisis urna sed arcu. Suspendisse potenti.
87
+
88
+
89
+ Vestibulum lacus metus, faucibus sit amet, mattis non, mollis sed, pede. Aenean
90
+ vitae sem nec sem euismod sollicitudin. Cras rhoncus.
91
+
92
+
93
+
94
+ Phasellus condimentum, ante a cursus dictum, lectus ipsum convallis magna, sed
95
+ tincidunt massa eros vitae ante. Suspendisse nec sem.
96
+ In hac habitasse platea dictumst. Fusce purus leo, ullamcorper sit amet, luctus
97
+ in, mollis mollis, enim. In adipiscing erat.
98
+ EOT
99
+ puts s
100
+
101
+ puts '=' * 80
102
+
103
+ puts s.word_wrap(60)
104
+ puts '=' * 80
105
+ puts s.word_wrap(79)
106
+
107
+ puts '=' * 80
108
+
109
+ s.word_wrap!(60)
110
+ puts s
111
+ end