quality_extensions 1.1.6 → 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.
Files changed (133) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/License +403 -0
  4. data/Rakefile +2 -0
  5. data/Readme.rdoc +2 -3
  6. data/VERSION +1 -0
  7. data/doc_include/ReleaseNotes-0.0.10 +1 -0
  8. data/doc_include/ReleaseNotes-0.0.12 +1 -0
  9. data/doc_include/ReleaseNotes-0.0.13 +2 -0
  10. data/doc_include/ReleaseNotes-0.0.14 +1 -0
  11. data/doc_include/ReleaseNotes-0.0.15 +2 -0
  12. data/doc_include/ReleaseNotes-0.0.16 +1 -0
  13. data/doc_include/ReleaseNotes-0.0.17 +1 -0
  14. data/doc_include/ReleaseNotes-0.0.18 +1 -0
  15. data/doc_include/ReleaseNotes-0.0.19 +1 -0
  16. data/doc_include/ReleaseNotes-0.0.20 +1 -0
  17. data/doc_include/ReleaseNotes-0.0.21 +7 -0
  18. data/doc_include/ReleaseNotes-0.0.22 +2 -0
  19. data/doc_include/ReleaseNotes-0.0.23 +1 -0
  20. data/doc_include/ReleaseNotes-0.0.24 +3 -0
  21. data/doc_include/ReleaseNotes-0.0.25 +3 -0
  22. data/doc_include/ReleaseNotes-0.0.26 +2 -0
  23. data/doc_include/ReleaseNotes-0.0.27 +1 -0
  24. data/doc_include/ReleaseNotes-0.0.28 +1 -0
  25. data/doc_include/ReleaseNotes-0.0.29 +3 -0
  26. data/doc_include/ReleaseNotes-0.0.3 +1 -0
  27. data/doc_include/ReleaseNotes-0.0.30 +4 -0
  28. data/doc_include/ReleaseNotes-0.0.31 +1 -0
  29. data/doc_include/ReleaseNotes-0.0.32 +1 -0
  30. data/doc_include/ReleaseNotes-0.0.33 +1 -0
  31. data/doc_include/ReleaseNotes-0.0.34 +2 -0
  32. data/doc_include/ReleaseNotes-0.0.35 +1 -0
  33. data/doc_include/ReleaseNotes-0.0.36 +1 -0
  34. data/doc_include/ReleaseNotes-0.0.37 +1 -0
  35. data/doc_include/ReleaseNotes-0.0.38 +1 -0
  36. data/doc_include/ReleaseNotes-0.0.39 +1 -0
  37. data/doc_include/ReleaseNotes-0.0.4 +2 -0
  38. data/doc_include/ReleaseNotes-0.0.40 +1 -0
  39. data/doc_include/ReleaseNotes-0.0.41 +1 -0
  40. data/doc_include/ReleaseNotes-0.0.42 +2 -0
  41. data/doc_include/ReleaseNotes-0.0.43 +2 -0
  42. data/doc_include/ReleaseNotes-0.0.44 +3 -0
  43. data/doc_include/ReleaseNotes-0.0.45 +1 -0
  44. data/doc_include/ReleaseNotes-0.0.46 +1 -0
  45. data/doc_include/ReleaseNotes-0.0.47 +1 -0
  46. data/doc_include/ReleaseNotes-0.0.48 +2 -0
  47. data/doc_include/ReleaseNotes-0.0.49 +2 -0
  48. data/doc_include/ReleaseNotes-0.0.5 +1 -0
  49. data/doc_include/ReleaseNotes-0.0.50 +1 -0
  50. data/doc_include/ReleaseNotes-0.0.51 +1 -0
  51. data/doc_include/ReleaseNotes-0.0.52 +1 -0
  52. data/doc_include/ReleaseNotes-0.0.53 +3 -0
  53. data/doc_include/ReleaseNotes-0.0.54 +1 -0
  54. data/doc_include/ReleaseNotes-0.0.55 +2 -0
  55. data/doc_include/ReleaseNotes-0.0.56 +1 -0
  56. data/doc_include/ReleaseNotes-0.0.57 +3 -0
  57. data/doc_include/ReleaseNotes-0.0.58 +1 -0
  58. data/doc_include/ReleaseNotes-0.0.59 +1 -0
  59. data/doc_include/ReleaseNotes-0.0.6 +3 -0
  60. data/doc_include/ReleaseNotes-0.0.60 +3 -0
  61. data/doc_include/ReleaseNotes-0.0.61 +1 -0
  62. data/doc_include/ReleaseNotes-0.0.62 +1 -0
  63. data/doc_include/ReleaseNotes-0.0.63 +1 -0
  64. data/doc_include/ReleaseNotes-0.0.64 +1 -0
  65. data/doc_include/ReleaseNotes-0.0.7 +1 -0
  66. data/doc_include/ReleaseNotes-0.0.8 +2 -0
  67. data/doc_include/ReleaseNotes-0.0.9 +1 -0
  68. data/doc_include/ReleaseNotes-0.1.1 +1 -0
  69. data/doc_include/ReleaseNotes-0.1.2 +1 -0
  70. data/doc_include/ReleaseNotes-0.1.3 +1 -0
  71. data/doc_include/ReleaseNotes-0.1.4 +1 -0
  72. data/doc_include/ReleaseNotes-0.1.5 +0 -0
  73. data/doc_include/ReleaseNotes-1.0.0 +1 -0
  74. data/doc_include/ReleaseNotes-1.0.1 +1 -0
  75. data/doc_include/ReleaseNotes-1.0.2 +1 -0
  76. data/doc_include/ReleaseNotes-1.0.3 +1 -0
  77. data/doc_include/ReleaseNotes-1.1.0 +1 -0
  78. data/doc_include/ReleaseNotes-1.1.1 +1 -0
  79. data/doc_include/ReleaseNotes-1.1.6 +1 -0
  80. data/doc_include/assert_equal_with_difference_highlighting-there_he_is.png +0 -0
  81. data/doc_include/assert_equal_with_difference_highlighting-wheres_waldo.png +0 -0
  82. data/escape/Rakefile +62 -0
  83. data/escape/Readme +16 -0
  84. data/escape/doc_include/ReleaseNotes-0.0.1 +1 -0
  85. data/escape/doc_include/ReleaseNotes-0.0.3 +1 -0
  86. data/escape/doc_include/ReleaseNotes-0.0.4 +1 -0
  87. data/escape/doc_include/ReleaseNotes-0.0.5 +1 -0
  88. data/escape/doc_include/template/qualitysmith.rb +631 -0
  89. data/escape/lib/escape.rb +302 -0
  90. data/escape/pkg/escape-0.0.5.tgz +0 -0
  91. data/escape/pkg/escape-0.0.5.zip +0 -0
  92. data/escape/pkg/escape-0.0.5/Readme +16 -0
  93. data/escape/pkg/escape-0.0.5/doc_include/template/qualitysmith.rb +631 -0
  94. data/escape/pkg/escape-0.0.5/lib/escape.rb +302 -0
  95. data/lib/quality_extensions.rb +3 -1
  96. data/lib/quality_extensions/array/average.rb +45 -0
  97. data/lib/quality_extensions/array/expand_ranges.rb +2 -2
  98. data/lib/quality_extensions/array/shell_escape.rb +2 -2
  99. data/lib/quality_extensions/color/gradiate.rb +5 -5
  100. data/lib/quality_extensions/enumerable/every.rb +2 -2
  101. data/lib/quality_extensions/enumerable/grep_with_index.rb +51 -0
  102. data/lib/quality_extensions/enumerable/grepv.rb +1 -0
  103. data/lib/quality_extensions/enumerable/map_with_index.rb +2 -0
  104. data/lib/quality_extensions/enumerable/max_by_value.rb +72 -0
  105. data/lib/quality_extensions/enumerable/select_while.rb +7 -2
  106. data/lib/quality_extensions/enumerable/select_with_index.rb +2 -2
  107. data/lib/quality_extensions/float/truncate.rb +2 -1
  108. data/lib/quality_extensions/hash/except.rb +2 -2
  109. data/lib/quality_extensions/hash/to_query_string.rb +6 -6
  110. data/lib/quality_extensions/helpers/numbers.rb +259 -0
  111. data/lib/quality_extensions/kernel/die.rb +2 -2
  112. data/lib/quality_extensions/kernel/filter_output.rb +1 -0
  113. data/lib/quality_extensions/kernel/require_all.rb +7 -7
  114. data/lib/quality_extensions/module/guard_method.rb +3 -3
  115. data/lib/quality_extensions/object/non.rb +10 -4
  116. data/lib/quality_extensions/pathname.rb +51 -3
  117. data/lib/quality_extensions/regexp/named_captures.rb +7 -6
  118. data/lib/quality_extensions/regexp/to_plain_s.rb +116 -0
  119. data/lib/quality_extensions/safe_method.rb +100 -0
  120. data/lib/quality_extensions/safe_nil.rb +87 -11
  121. data/lib/quality_extensions/string/chomped_lines.rb +0 -0
  122. data/lib/quality_extensions/string/integer_eh.rb +2 -2
  123. data/lib/quality_extensions/string/prefix.rb +120 -0
  124. data/lib/quality_extensions/string/prefix_lines.rb +4 -69
  125. data/lib/quality_extensions/string/safe_numeric_conversion.rb +5 -5
  126. data/lib/quality_extensions/string/shell_escape.rb +3 -3
  127. data/lib/quality_extensions/string/to_proc.rb +119 -0
  128. data/lib/quality_extensions/template.rb +2 -2
  129. data/lib/quality_extensions/test/difference_highlighting.rb +2 -2
  130. data/lib/quality_extensions/version.rb +3 -0
  131. data/quality_extensions.gemspec +21 -0
  132. metadata +249 -145
  133. data/lib/Xfind_bug_test.rb +0 -28
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Credits::
2
+ # Credits::
3
3
  # * Tom Locke (http://hobocentral.net/blog/2007/08/25/quiz/) -- original version
4
4
  # * coderrr (http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/) -- some optimizations
5
5
  # * Tyler Rick -- packaged it, added some documention, and added some tests
@@ -12,22 +12,30 @@
12
12
  $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
13
13
  require 'singleton'
14
14
  require 'rubygems'
15
+ #require 'facets/functor'
16
+ gem 'builder'
15
17
  require 'builder/blankslate'
16
18
  #require 'facets/basicobject'
17
19
  #puts Object.methods.include?(:blank_slate_method_added) # not there?
18
20
 
19
21
 
20
22
  class Object
23
+ # If you are not sure if the receiver is nil?, you can prefix a method call / operation with _? to be safe. If the receiver is not nil, it will behave as if you'd called your method on the receiver directly.
24
+ # If on the other hand the receiver is nil (see NilClass#_?), calling _? will cause an instance of SafeNil to be the *real* receiver, which will returns nil when you actually call whatever real method you are calling.
21
25
  def _?
22
26
  self
23
27
  end
28
+
29
+ #def unless_blank?
30
+ # self
31
+ #end
24
32
  end
25
33
 
26
34
  class NilClass
27
- # The _? method just returns the object itself for all objects except for nil. For nil, _? will return a special version of nil (actually, an instance of
35
+ # The _? method just returns the object itself for all objects except for nil. For nil, _? will return a special version of nil (actually, an instance of
28
36
  # SafeNil) that lets you call undefined methods.
29
37
  #
30
- # If you call an undefined method on nil._?, you will get back nil -- rather than raising an exception, which is what would happen if you called the same
38
+ # If you call an undefined method on nil._?, you will get back nil -- rather than raising an exception, which is what would happen if you called the same
31
39
  # method on a _plain_ old nil!
32
40
  #
33
41
  # _? gives us a much conciser alternative to this common pattern:
@@ -49,23 +57,27 @@ class NilClass
49
57
  end
50
58
  end
51
59
 
60
+ # http://railstips.org/blog/archives/2009/08/07/patterns-are-not-scary-method-missing-proxy/
61
+ class BasicObject #:nodoc:
62
+ instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
63
+ end unless defined?(BasicObject)
52
64
 
53
65
  # Extending BasicObject because it provides us with a clean slate. It is similar to Object except it has almost all the standard methods stripped away so that
54
66
  # we will hit method_missing for almost all method routing.
55
- class SafeNil < BlankSlate
67
+ #
68
+ # See NilClass#_?/Object#_?
69
+ class SafeNil < BasicObject
56
70
  include ::Singleton # I assume this is because it's faster than instantiating a new object each time.
57
71
 
58
- #undef inspect # Use nil's version of inspect... although I wonder whether it would be better to have it return "SafeNil" so you know it's different than a normal nil.
59
- def inspect
60
- 'SafeNil'
61
- end
62
-
72
+ # See NilClass#_?/Object#_?
63
73
  def method_missing(method, *args, &block)
64
74
  return nil unless nil.respond_to?(method) # A much faster alternative to 'rescue nil' that can be used most of the time to speed things up.
65
75
  nil.send(method, *args, &block) rescue nil
66
76
  end
67
77
 
78
+ # See NilClass#_?/Object#_?
68
79
  def nil?; true; end
80
+ def blank?; true; end
69
81
  end
70
82
 
71
83
 
@@ -81,7 +93,7 @@ end
81
93
  =begin test
82
94
  require 'test/unit'
83
95
 
84
- class TheTest < Test::Unit::TestCase
96
+ class SafeNilTest < Test::Unit::TestCase
85
97
  def test_simple__nil
86
98
  hash = {}
87
99
  assert_equal nil, hash[:a]._?.length
@@ -91,6 +103,12 @@ class TheTest < Test::Unit::TestCase
91
103
  assert_equal 3, hash[:a]._?.length
92
104
  end
93
105
 
106
+ def test_to_s
107
+ assert_equal '', nil._?.to_s
108
+ assert_equal( {}.to_s, {}._?.to_s ) # this is '' in 1.8, '{}' in 1.9
109
+ assert_equal [].to_s, []._?.to_s
110
+ end
111
+
94
112
  def test_chaining
95
113
  hash = {}
96
114
  assert_equal nil, hash[:a]._?.foo
@@ -98,19 +116,77 @@ class TheTest < Test::Unit::TestCase
98
116
  assert_equal nil, hash[:a]._?[:b]._?[:c]._?.some_method
99
117
  end
100
118
 
119
+ # As far as I know, this is impossible to get to pass, since by design an object in Ruby is *always* truthy unless it is false or nil
120
+ #def test_or_ing
121
+ # assert_equal 'else', nil._? || 'else'
122
+ #end
123
+
101
124
  def test_inspect
102
- assert_equal 'SafeNil', nil._?.inspect
125
+ assert_equal 'nil', nil._?.inspect
126
+ end
127
+
128
+ def test_does_not_permanently_modify_nil_class
129
+ assert_raise(NoMethodError) { nil.foo }
130
+ nil._?
131
+ assert_raise(NoMethodError) { nil.foo }
103
132
  end
104
133
 
105
134
  def test_nil?
106
135
  assert SafeNil.instance.nil?
107
136
  end
108
137
 
138
+ def test_blank?
139
+ require 'facets/blank'
140
+ assert nil.blank?
141
+ assert SafeNil.instance.blank?
142
+ assert ({1=>'a', 2=>'b'})[3]._?.blank?
143
+ end
144
+ end
145
+
146
+ class UnlessBlankTest < Test::Unit::TestCase
147
+ def test_simple__blank
148
+ hash = {}
149
+ assert_equal nil, hash[:a]._?.length
150
+ end
151
+ def test_simple__normal_objects
152
+ hash = {:a => 'abc'}
153
+ assert_equal 3, hash[:a]._?.length
154
+ end
155
+
156
+ def test_to_s
157
+ assert_equal '', nil._?.to_s
158
+ assert_equal( {}.to_s, {}._?.to_s ) # this is '' in 1.8, '{}' in 1.9
159
+ assert_equal [].to_s, []._?.to_s
160
+ end
161
+
162
+ def test_chaining
163
+ hash = {}
164
+ assert_equal nil, hash[:a]._?.foo
165
+ assert_equal nil, hash[:a]._?[:b]._?[:c]
166
+ assert_equal nil, hash[:a]._?[:b]._?[:c]._?.some_method
167
+ end
168
+
169
+ def test_inspect
170
+ assert_equal 'nil', nil._?.inspect
171
+ end
172
+
109
173
  def test_does_not_permanently_modify_nil_class
110
174
  assert_raise(NoMethodError) { nil.foo }
111
175
  nil._?
112
176
  assert_raise(NoMethodError) { nil.foo }
113
177
  end
178
+
179
+ def test_nil?
180
+ assert SafeNil.instance.nil?
181
+ end
182
+
183
+ def test_blank?
184
+ require 'facets/blank'
185
+ assert nil.blank?
186
+ assert SafeNil.instance.blank?
187
+ assert ({1=>'a', 2=>'b'})[3]._?.blank?
188
+ end
189
+
114
190
  end
115
191
  =end
116
192
 
@@ -1,3 +1,3 @@
1
1
  # Alias for:
2
- require 'facets/kernel/require_local'
3
- require_local 'numeric_eh'
2
+ require 'facets/kernel/require_relative'
3
+ require_relative 'numeric_eh'
@@ -0,0 +1,120 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2009, Tyler Rick
4
+ # License:: Ruby License
5
+ # Submit to Facets?::
6
+ # Developer notes::
7
+ # Changes::
8
+ #++
9
+
10
+ class String
11
+ def prefix(prefix)
12
+ sub(/^/, prefix)
13
+ end
14
+
15
+ def unprefix(prefix)
16
+ sub(/^#{Regexp.escape(prefix)}/, "")
17
+ end
18
+
19
+ def suffix(suffix)
20
+ sub(/$/, suffix)
21
+ end
22
+
23
+ def unsuffix(suffix)
24
+ sub(/#{Regexp.escape(suffix)}$/, "")
25
+ end
26
+
27
+ #----------------------------------------------------------------------------------------------------
28
+
29
+ def prefix_lines(prefix)
30
+ gsub(/^/, prefix)
31
+ end
32
+
33
+ def unprefix_lines(prefix)
34
+ gsub(/^#{Regexp.escape(prefix)}/, "")
35
+ end
36
+
37
+ def suffix_lines(suffix)
38
+ gsub(/$/, suffix)
39
+ end
40
+
41
+ def unsuffix_lines(suffix)
42
+ gsub(/#{Regexp.escape(suffix)}$/, "")
43
+ end
44
+
45
+ end
46
+
47
+
48
+ # _____ _
49
+ # |_ _|__ ___| |_
50
+ # | |/ _ \/ __| __|
51
+ # | | __/\__ \ |_
52
+ # |_|\___||___/\__|
53
+ #
54
+ =begin test
55
+ require 'spec/autorun'
56
+
57
+ describe "String#prefix_lines" do
58
+ it "lets you indent lines, similar to Facets' String#indent(n)" do
59
+ "abc". prefix_lines(' ').should == ' abc'
60
+ " abc".prefix_lines(' '). should == ' abc'
61
+
62
+ ("abc\n" +
63
+ "xyz" ).prefix_lines(' ').should ==
64
+ (" abc\n" +
65
+ " xyz")
66
+ end
67
+
68
+ it "lets you comment lines" do
69
+ "abc". prefix_lines('# ').should == '# abc'
70
+ " abc".prefix_lines('# ').should == '# abc'
71
+
72
+ ("line 1\n" +
73
+ "line 2" ).prefix_lines('# ').should ==
74
+ ("# line 1\n" +
75
+ "# line 2")
76
+ end
77
+
78
+ it "lets you uncomment lines" do
79
+ "# abc". unprefix_lines('# ').should == 'abc'
80
+ "# abc".unprefix_lines('# ').should == ' abc'
81
+
82
+ ("# line 1\n" +
83
+ "# line 2" ).unprefix_lines('# ').should ==
84
+ ("line 1\n" +
85
+ "line 2")
86
+ end
87
+ end
88
+
89
+ describe "String#suffix_lines" do
90
+ it "lets you indent lines, similar to Facets' String#indent(n)" do
91
+ "abc". suffix_lines(' ').should == 'abc '
92
+ "abc ".suffix_lines(' '). should == 'abc '
93
+
94
+ ("abc\n" +
95
+ "xyz" ).suffix_lines(' ').should ==
96
+ ("abc \n" +
97
+ "xyz ")
98
+ end
99
+
100
+ it "lets you add something to the end of each line" do
101
+ "abc". suffix_lines(' \\').should == 'abc \\'
102
+ "abc ".suffix_lines(' \\').should == 'abc \\'
103
+
104
+ ("line 1\n" +
105
+ "line 2" ).suffix_lines(' \\').should ==
106
+ ("line 1 \\\n" +
107
+ "line 2 \\")
108
+ end
109
+
110
+ it "lets you uncomment lines" do
111
+ "abc \\". unsuffix_lines(' \\').should == 'abc'
112
+ "abc \\".unsuffix_lines(' \\').should == 'abc '
113
+
114
+ ("line 1 \\\n" +
115
+ "line 2 \\" ).unsuffix_lines(' \\').should ==
116
+ ("line 1\n" +
117
+ "line 2")
118
+ end
119
+ end
120
+ =end
@@ -1,69 +1,4 @@
1
- #--
2
- # Author:: Tyler Rick
3
- # Copyright:: Copyright (c) 2009, Tyler Rick
4
- # License:: Ruby License
5
- # Submit to Facets?::
6
- # Developer notes::
7
- # Changes::
8
- #++
9
-
10
- class String
11
-
12
- def prefix_lines(prefix)
13
- gsub(/^/, prefix)
14
- end
15
-
16
- def unprefix_lines(prefix)
17
- gsub(/^#{Regexp.escape(prefix)}/, "")
18
- end
19
-
20
- # TODO:
21
- #def suffix_lines(suffix)
22
- #def unsuffix_lines(suffix)
23
-
24
- end
25
-
26
-
27
- # _____ _
28
- # |_ _|__ ___| |_
29
- # | |/ _ \/ __| __|
30
- # | | __/\__ \ |_
31
- # |_|\___||___/\__|
32
- #
33
- =begin test
34
- require 'spec'
35
-
36
- describe "String#prefix_lines" do
37
-
38
- it "lets you indent lines, similar to Facets' String#indent(n)" do
39
- "abc". prefix_lines(' ').should == ' abc'
40
- " abc".prefix_lines(' '). should == ' abc'
41
-
42
- ("abc\n" +
43
- "xyz" ).prefix_lines(' ').should ==
44
- (" abc\n" +
45
- " xyz")
46
- end
47
-
48
- it "lets you comment lines" do
49
- "abc". prefix_lines('# ').should == '# abc'
50
- " abc".prefix_lines('# ').should == '# abc'
51
-
52
- ("line 1\n" +
53
- "line 2" ).prefix_lines('# ').should ==
54
- ("# line 1\n" +
55
- "# line 2")
56
- end
57
-
58
- it "lets you uncomment lines" do
59
- "# abc". unprefix_lines('# ').should == 'abc'
60
- "# abc".unprefix_lines('# ').should == ' abc'
61
-
62
- ("# line 1\n" +
63
- "# line 2" ).unprefix_lines('# ').should ==
64
- ("line 1\n" +
65
- "line 2")
66
- end
67
-
68
- end
69
- =end
1
+ gem 'facets'
2
+ require 'facets/kernel/require_relative'
3
+ # Alias for:
4
+ require_relative 'prefix'
@@ -1,7 +1,7 @@
1
1
  #--
2
2
  # Author:: Tyler Rick
3
3
  # Copyright:: Copyright (c) 2001, Leo [ slonika AT yahoo DOT com ], Tyler Rick
4
- # Credits::
4
+ # Credits::
5
5
  # - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/14518
6
6
  # License:: Ruby License
7
7
  # Submit to Facets?::
@@ -24,8 +24,8 @@
24
24
  class NumericError < RuntimeError
25
25
  end
26
26
 
27
- require 'facets/kernel/require_local'
28
- require_local 'numeric_eh'
27
+ require 'facets/kernel/require_relative'
28
+ require_relative 'numeric_eh'
29
29
 
30
30
 
31
31
  class String
@@ -33,9 +33,9 @@ class String
33
33
  alias __std__to_f to_f if ! method_defined? :__std__to_f
34
34
  alias __std__hex hex if ! method_defined? :__std__hex
35
35
  alias __std__oct oct if ! method_defined? :__std__oct
36
-
36
+
37
37
  def to_i()
38
- case self
38
+ case self
39
39
  when /^[-+]?0\d/ then __std__oct
40
40
  when /^[-+]?0x[a-f\d]/i then __std__hex
41
41
  when /^[-+]?\d/ then __std__to_i
@@ -9,7 +9,7 @@ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
9
9
  require 'rubygems'
10
10
  require 'escape' # http://www.a-k-r.org/escape/
11
11
  require 'facets/symbol/to_proc'
12
- require 'facets/kernel/require_local'
12
+ require 'facets/kernel/require_relative'
13
13
 
14
14
  class String
15
15
  def shell_escape
@@ -34,11 +34,11 @@ class TheTest < Test::Unit::TestCase
34
34
  assert_equal input, output
35
35
  end
36
36
  def test_using_echo_1
37
- assert_that_echo_gives_back_what_we_put_in(
37
+ assert_that_echo_gives_back_what_we_put_in(
38
38
  %q{!&'"`$0 |()<>} )
39
39
  end
40
40
  def test_using_echo_2
41
- assert_that_echo_gives_back_what_we_put_in(
41
+ assert_that_echo_gives_back_what_we_put_in(
42
42
  %q{'an arg that's got "quotes"} )
43
43
  end
44
44
 
@@ -0,0 +1,119 @@
1
+ # String#to_proc
2
+ #
3
+ # See http://weblog.raganwald.com/2007/10/stringtoproc.html ( Subscribe in a reader)
4
+ #
5
+ # Ported from the String Lambdas in Oliver Steele's Functional Javascript
6
+ # http://osteele.com/sources/javascript/functional/
7
+ #
8
+ # This work is licensed under the MIT License:
9
+ #
10
+ # (c) 2007 Reginald Braithwaite
11
+ # Portions Copyright (c) 2006 Oliver Steele
12
+ #
13
+ # Permission is hereby granted, free of charge, to any person obtaining
14
+ # a copy of this software and associated documentation files (the
15
+ # "Software"), to deal in the Software without restriction, including
16
+ # without limitation the rights to use, copy, modify, merge, publish,
17
+ # distribute, sublicense, and/or sell copies of the Software, and to
18
+ # permit persons to whom the Software is furnished to do so, subject to
19
+ # the following conditions:
20
+ #
21
+ # The above copyright notice and this permission notice shall be
22
+ # included in all copies or substantial portions of the Software.
23
+ #
24
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+ class String
33
+ unless ''.respond_to?(:to_proc)
34
+ def to_proc &block
35
+ params = []
36
+ expr = self
37
+ sections = expr.split(/\s*->\s*/m)
38
+ if sections.length > 1 then
39
+ eval sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block && block.binding
40
+ elsif expr.match(/\b_\b/)
41
+ eval "Proc.new { |_| #{expr} }", block && block.binding
42
+ else
43
+ leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
44
+ rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
45
+ if leftSection || rightSection then
46
+ if (leftSection) then
47
+ params.push('$left')
48
+ expr = '$left' + expr
49
+ end
50
+ if (rightSection) then
51
+ params.push('$right')
52
+ expr = expr + '$right'
53
+ end
54
+ else
55
+ self.gsub(
56
+ /(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
57
+ ).scan(
58
+ /([a-z_$][a-z_$\d]*)/i
59
+ ) do |v|
60
+ params.push(v) unless params.include?(v)
61
+ end
62
+ end
63
+ eval "Proc.new { |#{params.join(', ')}| #{expr} }", block && block.binding
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+
70
+ # _____ _
71
+ # |_ _|__ ___| |_
72
+ # | |/ _ \/ __| __|
73
+ # | | __/\__ \ |_
74
+ # |_|\___||___/\__|
75
+ #
76
+ =begin test
77
+ require 'spec/autorun'
78
+
79
+ describe "String to Proc" do
80
+
81
+ before(:all) do
82
+ @one2five = 1..5
83
+ end
84
+
85
+ it "should handle simple arrow notation" do
86
+ @one2five.map(&'x -> x + 1').should eql(@one2five.map { |x| x + 1 })
87
+ @one2five.map(&'x -> x*x').should eql(@one2five.map { |x| x*x })
88
+ @one2five.inject(&'x y -> x*y').should eql(@one2five.inject { |x,y| x*y })
89
+ 'x y -> x**y'.to_proc()[2,3].should eql(lambda { |x,y| x**y }[2,3])
90
+ 'y x -> x**y'.to_proc()[2,3].should eql(lambda { |y,x| x**y }[2,3])
91
+ end
92
+
93
+ it "should handle chained arrows" do
94
+ 'x -> y -> x**y'.to_proc()[2][3].should eql(lambda { |x| lambda { |y| x**y } }[2][3])
95
+ 'x -> y z -> y**(z-x)'.to_proc()[1][2,3].should eql(lambda { |x| lambda { |y,z| y**(z-x) } }[1][2,3])
96
+ end
97
+
98
+ it "should handle the default parameter" do
99
+ @one2five.map(&'2**_/2').should eql(@one2five.map { |x| 2**x/2 })
100
+ @one2five.select(&'_%2==0').should eql(@one2five.select { |x| x%2==0 })
101
+ end
102
+
103
+ it "should handle point-free notation" do
104
+ @one2five.inject(&'*').should eql(@one2five.inject { |mem, var| mem * var })
105
+ @one2five.select(&'>2').should eql(@one2five.select { |x| x>2 })
106
+ @one2five.select(&'2<').should eql(@one2five.select { |x| 2<x })
107
+ @one2five.map(&'2*').should eql(@one2five.map { |x| 2*x })
108
+ (-3..3).map(&'.abs').should eql((-3..3).map { |x| x.abs })
109
+ end
110
+
111
+ it "should handle implied parameters as best it can" do
112
+ @one2five.inject(&'x*y').should eql(@one2five.inject(&'*'))
113
+ 'x**y'.to_proc()[2,3].should eql(8)
114
+ 'y**x'.to_proc()[2,3].should eql(8)
115
+ end
116
+
117
+ end
118
+ =end
119
+