blackwinter-ruby-nuggets 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/COPYING +676 -0
  2. data/ChangeLog +5 -0
  3. data/README +60 -0
  4. data/Rakefile +20 -0
  5. data/lib/nuggets/all.rb +34 -0
  6. data/lib/nuggets/array/combination.rb +86 -0
  7. data/lib/nuggets/array/flatten_once.rb +68 -0
  8. data/lib/nuggets/array/format.rb +124 -0
  9. data/lib/nuggets/array/in_order.rb +62 -0
  10. data/lib/nuggets/array/monotone.rb +101 -0
  11. data/lib/nuggets/array/only.rb +56 -0
  12. data/lib/nuggets/array/rand.rb +45 -0
  13. data/lib/nuggets/array/shuffle.rb +133 -0
  14. data/lib/nuggets/array/to_hash.rb +86 -0
  15. data/lib/nuggets/enumerable/agrep.rb +82 -0
  16. data/lib/nuggets/enumerable/all_any_extended.rb +99 -0
  17. data/lib/nuggets/enumerable/minmax.rb +119 -0
  18. data/lib/nuggets/env/user_encoding.rb +53 -0
  19. data/lib/nuggets/env/user_home.rb +54 -0
  20. data/lib/nuggets/file/which.rb +74 -0
  21. data/lib/nuggets/hash/at.rb +87 -0
  22. data/lib/nuggets/hash/in_order.rb +52 -0
  23. data/lib/nuggets/hash/insert.rb +65 -0
  24. data/lib/nuggets/hash/only.rb +68 -0
  25. data/lib/nuggets/integer/factorial.rb +74 -0
  26. data/lib/nuggets/integer/to_binary_s.rb +47 -0
  27. data/lib/nuggets/io/agrep.rb +43 -0
  28. data/lib/nuggets/io/modes.rb +133 -0
  29. data/lib/nuggets/numeric/between.rb +2 -0
  30. data/lib/nuggets/numeric/duration.rb +109 -0
  31. data/lib/nuggets/numeric/limit.rb +70 -0
  32. data/lib/nuggets/numeric/signum.rb +60 -0
  33. data/lib/nuggets/numeric/to_multiple.rb +68 -0
  34. data/lib/nuggets/object/blank.rb +119 -0
  35. data/lib/nuggets/object/boolean.rb +69 -0
  36. data/lib/nuggets/object/eigenclass.rb +2 -0
  37. data/lib/nuggets/object/ghost_class.rb +2 -0
  38. data/lib/nuggets/object/metaclass.rb +2 -0
  39. data/lib/nuggets/object/msend.rb +55 -0
  40. data/lib/nuggets/object/singleton_class.rb +150 -0
  41. data/lib/nuggets/object/uniclass.rb +2 -0
  42. data/lib/nuggets/object/virtual_class.rb +2 -0
  43. data/lib/nuggets/proc/bind.rb +68 -0
  44. data/lib/nuggets/string/capitalize_first.rb +63 -0
  45. data/lib/nuggets/string/case.rb +104 -0
  46. data/lib/nuggets/string/evaluate.rb +53 -0
  47. data/lib/nuggets/string/msub.rb +82 -0
  48. data/lib/nuggets/string/nsub.rb +80 -0
  49. data/lib/nuggets/string/sub_with_md.rb +131 -0
  50. data/lib/nuggets/string/word_wrap.rb +111 -0
  51. data/lib/nuggets/tempfile/open.rb +54 -0
  52. data/lib/nuggets/uri/content_type.rb +65 -0
  53. data/lib/nuggets/uri/exist.rb +63 -0
  54. data/lib/nuggets/util/added_methods/init.rb +3 -0
  55. data/lib/nuggets/util/added_methods.rb +407 -0
  56. data/lib/nuggets/util/ansicolor2css.rb +90 -0
  57. data/lib/nuggets/util/content_type.rb +104 -0
  58. data/lib/nuggets/util/dotted_decimal.rb +66 -0
  59. data/lib/nuggets/util/i18n.rb +143 -0
  60. data/lib/nuggets/version.rb +27 -0
  61. data/lib/nuggets.rb +73 -0
  62. metadata +124 -0
@@ -0,0 +1,131 @@
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
+ class String
29
+
30
+ alias_method :sub_without_md, :sub
31
+ alias_method :sub_without_md!, :sub!
32
+ alias_method :gsub_without_md, :gsub
33
+ alias_method :gsub_without_md!, :gsub!
34
+
35
+ # call-seq:
36
+ # str.sub_with_md(pattern) { |match_data| ... } => new_str
37
+ #
38
+ # Just like #sub, but passes the MatchData object instead of the current
39
+ # match string to the block.
40
+ def sub_with_md(pattern, replacement = nil)
41
+ replacement ?
42
+ sub_without_md(pattern, replacement) :
43
+ (_dup = dup).sub_with_md!(pattern) { |*a| yield(*a) } || _dup
44
+ end
45
+
46
+ # call-seq:
47
+ # str.sub_with_md!(pattern) { |match_data| ... } => str or nil
48
+ #
49
+ # Destructive version of #sub_with_md.
50
+ def sub_with_md!(pattern, replacement = nil)
51
+ replacement ?
52
+ sub_without_md!(pattern, replacement) :
53
+ sub_without_md!(pattern) { |match| yield $~ }
54
+ end
55
+
56
+ # call-seq:
57
+ # str.gsub_with_md(pattern) { |match_data| ... } => new_str
58
+ #
59
+ # Just like #gsub, but passes the MatchData object instead of the current
60
+ # match string to the block.
61
+ def gsub_with_md(pattern, replacement = nil)
62
+ replacement ?
63
+ gsub_without_md(pattern, replacement) :
64
+ (_dup = dup).gsub_with_md!(pattern) { |*a| yield(*a) } || _dup
65
+ end
66
+
67
+ # call-seq:
68
+ # str.gsub_with_md!(pattern) { |match_data| ... } => str or nil
69
+ #
70
+ # Destructive version of #gsub_with_md.
71
+ def gsub_with_md!(pattern, replacement = nil)
72
+ replacement ?
73
+ gsub_without_md!(pattern, replacement) :
74
+ gsub_without_md!(pattern) { |match| yield $~ }
75
+ end
76
+
77
+ # call-seq:
78
+ # gimme_match_data!
79
+ #
80
+ # Replaces the traditional substitution methods with their MatchData passing
81
+ # equivalents. USE WITH CAUTION!
82
+ def self.gimme_match_data!
83
+ alias_method :sub, :sub_with_md
84
+ alias_method :sub!, :sub_with_md!
85
+ alias_method :gsub, :gsub_with_md
86
+ alias_method :gsub!, :gsub_with_md!
87
+ end
88
+
89
+ end
90
+
91
+ if $0 == __FILE__
92
+ # stupid example, but it proves the point ;-)
93
+ s = 'Foo, Bar - Baz'
94
+ p s
95
+
96
+ p s.gsub(/\w(\w+)(\W*)/) { |m|
97
+ #p m
98
+ "#{$1.gsub(/[ao]/, 'X')}#{$2}"
99
+ }
100
+
101
+ p s.gsub_with_md(/\w(\w+)(\W*)/) { |md|
102
+ #p md[0]
103
+ "#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
104
+ }
105
+
106
+ p s.gsub_with_md(/\w(\w+)(\W*)/) { |md|
107
+ #p md[0]
108
+ "#{md[1].gsub_with_md(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
109
+ }
110
+
111
+ String.gimme_match_data!
112
+
113
+ p s.gsub(/\w(\w+)(\W*)/) { |m|
114
+ #p m
115
+ begin
116
+ "#{$1.gsub(/[ao]/, 'X')}#{$2}"
117
+ rescue NoMethodError => err
118
+ warn err
119
+ end
120
+ }
121
+
122
+ p s.gsub(/\w(\w+)(\W*)/) { |md|
123
+ #p md[0]
124
+ "#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
125
+ }
126
+
127
+ p s.gsub(/\w(\w+)(\W*)/) { |md|
128
+ #p md[0]
129
+ "#{md[1].gsub(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
130
+ }
131
+ 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 = 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
@@ -0,0 +1,54 @@
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
+ require 'tempfile'
29
+
30
+ class Tempfile
31
+
32
+ alias_method :_nuggets_original_open, :open
33
+
34
+ # If no block is given, this is a synonym for new().
35
+ #
36
+ # If a block is given, it will be passed tempfile as an argument,
37
+ # and the tempfile will automatically be closed when the block
38
+ # terminates. In this case, open() returns tempfile -- in contrast
39
+ # to the original implementation, which returns nil.
40
+ def self.open(*args)
41
+ tempfile = new(*args)
42
+
43
+ if block_given?
44
+ begin
45
+ yield tempfile
46
+ ensure
47
+ tempfile.close
48
+ end
49
+ end
50
+
51
+ tempfile
52
+ end
53
+
54
+ end
@@ -0,0 +1,65 @@
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
+ require 'socket'
29
+ require 'open-uri'
30
+
31
+ module URI
32
+
33
+ class << self
34
+
35
+ # call-seq:
36
+ # URI.content_type(uri) => aString or nil
37
+ #
38
+ # Return the content type of +uri+, or +nil+ if not found.
39
+ def content_type(uri)
40
+ open(uri.to_s).content_type
41
+ rescue OpenURI::HTTPError, SocketError, Errno::ENOENT, Errno::EHOSTUNREACH, NoMethodError
42
+ nil
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ if $0 == __FILE__
50
+ %w[
51
+ http://www.google.de
52
+ htp://www.google.de
53
+ www.google.de
54
+ http://blackwinter.de/misc/
55
+ http://blackwinter.de/misc/ww.png
56
+ http://blackwinter.de/misc/suicide_is_painless.mid
57
+ http://blackwinter.de/misc/expand_macros.pl.gz
58
+ http://blackwinter.de/misc/blanc60302523.nth
59
+ http://blackwinter.de/bla
60
+ http://blawinter.de
61
+ http://192.168.12.34/foo/bar
62
+ ].each { |u|
63
+ p [u, URI.content_type(u)]
64
+ }
65
+ end
@@ -0,0 +1,63 @@
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
+ require 'open-uri'
29
+
30
+ module URI
31
+
32
+ class << self
33
+
34
+ # call-seq:
35
+ # URI.exist?(uri) => true or false
36
+ #
37
+ # Return +true+ if the named URI exists.
38
+ def exist?(uri)
39
+ open(uri.to_s)
40
+ true
41
+ rescue OpenURI::HTTPError, SocketError, Errno::ENOENT
42
+ false
43
+ end
44
+ alias_method :exists?, :exist?
45
+
46
+ end
47
+
48
+ end
49
+
50
+ if $0 == __FILE__
51
+ %w[
52
+ http://www.google.de
53
+ htp://www.google.de
54
+ www.google.de
55
+ http://xuugle.de
56
+ http://blackwinter.de
57
+ http://blackwinter.de/bla
58
+ http://blackwinter.de/index.html
59
+ http://blackwinter.de/index.htm
60
+ ].each { |u|
61
+ p [u, URI.exist?(u)]
62
+ }
63
+ end
@@ -0,0 +1,3 @@
1
+ # just a short-cut
2
+ require 'nuggets/util/added_methods'
3
+ Util::AddedMethods.init