quality_extensions 0.1.1
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.
- data/Readme +54 -0
- data/lib/qualitysmith_extensions/all.rb +4 -0
- data/lib/qualitysmith_extensions/array/all.rb +2 -0
- data/lib/qualitysmith_extensions/array/average.rb +44 -0
- data/lib/qualitysmith_extensions/array/classify.rb +97 -0
- data/lib/qualitysmith_extensions/array/expand_ranges.rb +52 -0
- data/lib/qualitysmith_extensions/array/group_by.rb +134 -0
- data/lib/qualitysmith_extensions/array/sequence.rb +66 -0
- data/lib/qualitysmith_extensions/array/shell_escape.rb +36 -0
- data/lib/qualitysmith_extensions/array/to_a_recursive.rb +41 -0
- data/lib/qualitysmith_extensions/array/to_query_string.rb +96 -0
- data/lib/qualitysmith_extensions/collection_extensions_for_cgi.rb +2 -0
- data/lib/qualitysmith_extensions/colored/toggleability.rb +62 -0
- data/lib/qualitysmith_extensions/console/command.facets.1.8.51.rb +749 -0
- data/lib/qualitysmith_extensions/console/command.facets.1.8.54.rb +748 -0
- data/lib/qualitysmith_extensions/console/command.rb +944 -0
- data/lib/qualitysmith_extensions/date/all.rb +2 -0
- data/lib/qualitysmith_extensions/date/deprecated.rb +40 -0
- data/lib/qualitysmith_extensions/date/iso8601.rb +31 -0
- data/lib/qualitysmith_extensions/date/month_ranges.rb +122 -0
- data/lib/qualitysmith_extensions/dir/each_child.rb +58 -0
- data/lib/qualitysmith_extensions/enumerable/enum.rb +69 -0
- data/lib/qualitysmith_extensions/enumerable/select_until.rb +4 -0
- data/lib/qualitysmith_extensions/enumerable/select_while.rb +109 -0
- data/lib/qualitysmith_extensions/exception/inspect_with_backtrace.rb +65 -0
- data/lib/qualitysmith_extensions/file/exact_match_regexp.rb +34 -0
- data/lib/qualitysmith_extensions/file_test/binary_file.rb +110 -0
- data/lib/qualitysmith_extensions/find/select.rb +68 -0
- data/lib/qualitysmith_extensions/global_variable_set.rb +153 -0
- data/lib/qualitysmith_extensions/hash/all.rb +2 -0
- data/lib/qualitysmith_extensions/hash/to_date.rb +34 -0
- data/lib/qualitysmith_extensions/hash/to_query_string.rb +121 -0
- data/lib/qualitysmith_extensions/kernel/all.rb +2 -0
- data/lib/qualitysmith_extensions/kernel/autoreload.rb +128 -0
- data/lib/qualitysmith_extensions/kernel/backtrace.rb +71 -0
- data/lib/qualitysmith_extensions/kernel/capture_output.rb +115 -0
- data/lib/qualitysmith_extensions/kernel/die.rb +49 -0
- data/lib/qualitysmith_extensions/kernel/example_printer.rb +81 -0
- data/lib/qualitysmith_extensions/kernel/filter_output.rb +108 -0
- data/lib/qualitysmith_extensions/kernel/remove_const.rb +178 -0
- data/lib/qualitysmith_extensions/kernel/remove_module.rb +127 -0
- data/lib/qualitysmith_extensions/kernel/require_all.rb +186 -0
- data/lib/qualitysmith_extensions/kernel/require_local_all.rb +4 -0
- data/lib/qualitysmith_extensions/kernel/require_once.rb +18 -0
- data/lib/qualitysmith_extensions/kernel/simulate_input.rb +52 -0
- data/lib/qualitysmith_extensions/kernel/trap_chain.rb +61 -0
- data/lib/qualitysmith_extensions/kernel/windows_platform.rb +46 -0
- data/lib/qualitysmith_extensions/module/alias_method.rb +6 -0
- data/lib/qualitysmith_extensions/module/alias_method_chain.rb +165 -0
- data/lib/qualitysmith_extensions/module/ancestry_of_instance_method.rb +43 -0
- data/lib/qualitysmith_extensions/module/attribute_accessors.rb +49 -0
- data/lib/qualitysmith_extensions/module/basename.rb +76 -0
- data/lib/qualitysmith_extensions/module/bool_attr_accessor.rb +497 -0
- data/lib/qualitysmith_extensions/module/class_methods.rb +87 -0
- data/lib/qualitysmith_extensions/module/create.rb +315 -0
- data/lib/qualitysmith_extensions/module/create_setter.rb +9 -0
- data/lib/qualitysmith_extensions/module/dirname.rb +4 -0
- data/lib/qualitysmith_extensions/module/guard_method.rb +312 -0
- data/lib/qualitysmith_extensions/module/includable_once.rb +10 -0
- data/lib/qualitysmith_extensions/module/join.rb +66 -0
- data/lib/qualitysmith_extensions/module/malias_method_chain.rb +92 -0
- data/lib/qualitysmith_extensions/module/module_methods.rb +4 -0
- data/lib/qualitysmith_extensions/module/namespace.rb +112 -0
- data/lib/qualitysmith_extensions/module/parents.rb +61 -0
- data/lib/qualitysmith_extensions/module/remove_const.rb +117 -0
- data/lib/qualitysmith_extensions/module/split.rb +55 -0
- data/lib/qualitysmith_extensions/month.rb +66 -0
- data/lib/qualitysmith_extensions/mutex/if_available.rb +75 -0
- data/lib/qualitysmith_extensions/object/ancestry_of_method.rb +257 -0
- data/lib/qualitysmith_extensions/object/default.rb +69 -0
- data/lib/qualitysmith_extensions/object/if_else.rb +157 -0
- data/lib/qualitysmith_extensions/object/ignore_access.rb +84 -0
- data/lib/qualitysmith_extensions/object/mcall.rb +92 -0
- data/lib/qualitysmith_extensions/object/methods.rb +63 -0
- data/lib/qualitysmith_extensions/object/send_if.rb +151 -0
- data/lib/qualitysmith_extensions/object/send_if_not_nil.rb +35 -0
- data/lib/qualitysmith_extensions/object/singleton_send.rb +129 -0
- data/lib/qualitysmith_extensions/regexp/join.rb +111 -0
- data/lib/qualitysmith_extensions/string/all.rb +2 -0
- data/lib/qualitysmith_extensions/string/constantize.rb +4 -0
- data/lib/qualitysmith_extensions/string/digits_only.rb +27 -0
- data/lib/qualitysmith_extensions/string/each_char_with_index.rb +41 -0
- data/lib/qualitysmith_extensions/string/md5.rb +29 -0
- data/lib/qualitysmith_extensions/string/shell_escape.rb +43 -0
- data/lib/qualitysmith_extensions/string/to_underscored_label.rb +37 -0
- data/lib/qualitysmith_extensions/string/with_knowledge_of_color.rb +64 -0
- data/lib/qualitysmith_extensions/symbol/constantize.rb +69 -0
- data/lib/qualitysmith_extensions/symbol/match.rb +157 -0
- data/lib/qualitysmith_extensions/template.rb +33 -0
- data/lib/qualitysmith_extensions/test/all.rb +2 -0
- data/lib/qualitysmith_extensions/test/assert_anything.rb +93 -0
- data/lib/qualitysmith_extensions/test/assert_changed.rb +66 -0
- data/lib/qualitysmith_extensions/test/assert_exception.rb +66 -0
- data/lib/qualitysmith_extensions/test/assert_includes.rb +36 -0
- data/lib/qualitysmith_extensions/test/assert_user_error.rb +37 -0
- data/lib/qualitysmith_extensions/test/difference_highlighting.rb +323 -0
- data/lib/qualitysmith_extensions/time/all.rb +2 -0
- data/lib/qualitysmith_extensions/time/deprecated.rb +31 -0
- data/test/all.rb +16 -0
- metadata +148 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Yes! A great compliment to each_char!
|
|
6
|
+
# Developer notes::
|
|
7
|
+
#++
|
|
8
|
+
|
|
9
|
+
require 'rubygems'
|
|
10
|
+
require 'qualitysmith_extensions/enumerable/enum'
|
|
11
|
+
|
|
12
|
+
class String
|
|
13
|
+
def each_char_with_index
|
|
14
|
+
i = 0
|
|
15
|
+
split(//).each do |c|
|
|
16
|
+
yield i, c
|
|
17
|
+
i += 1
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# _____ _
|
|
25
|
+
# |_ _|__ ___| |_
|
|
26
|
+
# | |/ _ \/ __| __|
|
|
27
|
+
# | | __/\__ \ |_
|
|
28
|
+
# |_|\___||___/\__|
|
|
29
|
+
#
|
|
30
|
+
=begin test
|
|
31
|
+
require 'test/unit'
|
|
32
|
+
|
|
33
|
+
class TheTest < Test::Unit::TestCase
|
|
34
|
+
def test_1
|
|
35
|
+
assert_equal [[0, "a"], [1, "b"], [2, "c"]],
|
|
36
|
+
'abc'.enum(:each_char_with_index).to_a
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
=end
|
|
40
|
+
|
|
41
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Yes
|
|
6
|
+
#++
|
|
7
|
+
|
|
8
|
+
require "digest/md5"
|
|
9
|
+
|
|
10
|
+
class String
|
|
11
|
+
# Because it's so much more natural to have this as a method of the string rather than having to call <tt>Digest::MD5.hexdigest(string)</tt>.
|
|
12
|
+
def md5
|
|
13
|
+
return Digest::MD5.hexdigest(self)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# _____ _
|
|
18
|
+
# |_ _|__ ___| |_
|
|
19
|
+
# | |/ _ \/ __| __|
|
|
20
|
+
# | | __/\__ \ |_
|
|
21
|
+
# |_|\___||___/\__|
|
|
22
|
+
#
|
|
23
|
+
=begin test
|
|
24
|
+
class TheTest < Test::Unit::TestCase
|
|
25
|
+
def test_md5
|
|
26
|
+
assert_equal "abc".md5, Digest::MD5.hexdigest("abc")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
=end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Yes
|
|
6
|
+
#++
|
|
7
|
+
|
|
8
|
+
require 'rubygems'
|
|
9
|
+
require 'escape' # http://www.a-k-r.org/escape/
|
|
10
|
+
require 'facets/core/symbol/to_proc' unless Symbol.method_defined?(:to_proc)
|
|
11
|
+
require 'facets/core/kernel/require_local'
|
|
12
|
+
|
|
13
|
+
class String
|
|
14
|
+
def shell_escape
|
|
15
|
+
Escape.shell_command([self])
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# _____ _
|
|
20
|
+
# |_ _|__ ___| |_
|
|
21
|
+
# | |/ _ \/ __| __|
|
|
22
|
+
# | | __/\__ \ |_
|
|
23
|
+
# |_|\___||___/\__|
|
|
24
|
+
#
|
|
25
|
+
=begin test
|
|
26
|
+
require 'test/unit'
|
|
27
|
+
class TheTest < Test::Unit::TestCase
|
|
28
|
+
def assert_that_echo_gives_back_what_we_put_in(input)
|
|
29
|
+
input = %q{!&'"`$0 |()<>}
|
|
30
|
+
output = `echo -n #{input.shell_escape}`
|
|
31
|
+
assert_equal input, output
|
|
32
|
+
end
|
|
33
|
+
def test_1
|
|
34
|
+
assert_that_echo_gives_back_what_we_put_in(
|
|
35
|
+
%q{!&'"`$0 |()<>} )
|
|
36
|
+
end
|
|
37
|
+
def test_2
|
|
38
|
+
assert_that_echo_gives_back_what_we_put_in(
|
|
39
|
+
%q{'an arg that's got "quotes"} )
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
=end
|
|
43
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Probably not.
|
|
6
|
+
# Developer notes:
|
|
7
|
+
# * Can we use a more general method instead (like humanize or methodize)? Does this really have a use distinct from all the other inflection methods out there?
|
|
8
|
+
#++
|
|
9
|
+
|
|
10
|
+
class String
|
|
11
|
+
# Strips out most non-alphanumeric characters and leaves you with a lowercased, underscored string that can safely be used as a class_name
|
|
12
|
+
def to_underscored_label
|
|
13
|
+
self.
|
|
14
|
+
downcase.
|
|
15
|
+
gsub(/-+/, "_").gsub(/ +/, "_"). # spaces and -'s-> underscores
|
|
16
|
+
gsub(/[^a-z0-9_]/, ""). # keep only alphanumeric and _ characters
|
|
17
|
+
gsub(/_+$/, ""). # We don't want any _ characters at the end
|
|
18
|
+
gsub(/^_+/, "") # ... or the beginning
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# _____ _
|
|
23
|
+
# |_ _|__ ___| |_
|
|
24
|
+
# | |/ _ \/ __| __|
|
|
25
|
+
# | | __/\__ \ |_
|
|
26
|
+
# |_|\___||___/\__|
|
|
27
|
+
#
|
|
28
|
+
=begin test
|
|
29
|
+
class TheTest < Test::Unit::TestCase
|
|
30
|
+
def test_to_underscored_label
|
|
31
|
+
assert_equal "discount_amount", "Discount Amount".to_underscored_label
|
|
32
|
+
assert_equal "more_spaces", "More SPACES".to_underscored_label
|
|
33
|
+
assert_equal "other_123_types_of_characters", "Other-123 Types? Of!!! Characters".to_underscored_label
|
|
34
|
+
assert_equal "weird_characters_on_the_end", "weird characters on the end *#*#**".to_underscored_label
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
=end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Maybe
|
|
6
|
+
#++
|
|
7
|
+
|
|
8
|
+
module WithKnowledgeOfColor
|
|
9
|
+
Color_regexp = /\e\[[^m]+m/
|
|
10
|
+
|
|
11
|
+
def strip_color
|
|
12
|
+
gsub(Color_regexp, '')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# This version of +length+ takes into account the fact that the ANSI color codes themselves don't take up any space to display on the screen.
|
|
16
|
+
# So this returns the number of characters wide the string is when it is actually printed to the screen.
|
|
17
|
+
def length_without_color
|
|
18
|
+
strip_color.length
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def nonprinting_characters_used_for_color
|
|
22
|
+
self.scan(Color_regexp).join
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def ljust_without_color(width)
|
|
26
|
+
ljust(width + nonprinting_characters_used_for_color.length)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class String
|
|
31
|
+
include WithKnowledgeOfColor
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# _____ _
|
|
35
|
+
# |_ _|__ ___| |_
|
|
36
|
+
# | |/ _ \/ __| __|
|
|
37
|
+
# | | __/\__ \ |_
|
|
38
|
+
# |_|\___||___/\__|
|
|
39
|
+
#
|
|
40
|
+
=begin test
|
|
41
|
+
require 'rubygems'
|
|
42
|
+
gem 'colored'
|
|
43
|
+
require 'colored'
|
|
44
|
+
|
|
45
|
+
class TheTest < Test::Unit::TestCase
|
|
46
|
+
def test_strip_color
|
|
47
|
+
assert "abc" != "abc".blue
|
|
48
|
+
assert_equal "abc", "abc".blue.strip_color
|
|
49
|
+
end
|
|
50
|
+
def test_length_without_color
|
|
51
|
+
assert_equal 12, "abc".blue.length
|
|
52
|
+
assert_equal 3, "abc".blue.length_without_color
|
|
53
|
+
end
|
|
54
|
+
def test_nonprinting_characters_used_for_color
|
|
55
|
+
assert_equal "\e[34m\e[0m", 'abc'.blue.nonprinting_characters_used_for_color
|
|
56
|
+
end
|
|
57
|
+
def test_ljust_without_color
|
|
58
|
+
assert_equal "abc ", 'abc'. ljust( 5)
|
|
59
|
+
assert_equal "abc ", 'abc'.blue.ljust_without_color(5).strip_color
|
|
60
|
+
assert_equal "\e[34mabc\e[0m ", 'abc'.blue.ljust_without_color(5)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end
|
|
64
|
+
=end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Yes!
|
|
6
|
+
# Developer notes::
|
|
7
|
+
# Changes::
|
|
8
|
+
#++
|
|
9
|
+
|
|
10
|
+
require 'rubygems'
|
|
11
|
+
require 'facets/core/kernel/constant'
|
|
12
|
+
|
|
13
|
+
class Symbol
|
|
14
|
+
# Tries to find a declared constant with the name specified in self.
|
|
15
|
+
#
|
|
16
|
+
# :Foo.constantize => Foo
|
|
17
|
+
#
|
|
18
|
+
# Unlike ActiveSupport, we don't do this check (because Kernel.module "can handle module hierarchy"):
|
|
19
|
+
# vendor/rails/activesupport/lib/active_support/inflector.rb
|
|
20
|
+
# unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
|
|
21
|
+
# raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
|
|
22
|
+
# end
|
|
23
|
+
def constantize
|
|
24
|
+
Kernel.constant(self)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
class String
|
|
28
|
+
# Tries to find a declared constant with the name specified in self.
|
|
29
|
+
#
|
|
30
|
+
# 'Foo'.constantize => Foo
|
|
31
|
+
#
|
|
32
|
+
# Unlike ActiveSupport, we don't do this check (because Kernel.module "can handle module hierarchy"):
|
|
33
|
+
# vendor/rails/activesupport/lib/active_support/inflector.rb
|
|
34
|
+
# unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
|
|
35
|
+
# raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
|
|
36
|
+
# end
|
|
37
|
+
def constantize
|
|
38
|
+
Kernel.constant(self)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# _____ _
|
|
43
|
+
# |_ _|__ ___| |_
|
|
44
|
+
# | |/ _ \/ __| __|
|
|
45
|
+
# | | __/\__ \ |_
|
|
46
|
+
# |_|\___||___/\__|
|
|
47
|
+
#
|
|
48
|
+
=begin test
|
|
49
|
+
require 'test/unit'
|
|
50
|
+
|
|
51
|
+
module OuterModule; end
|
|
52
|
+
module OuterModule::InnerModule; end
|
|
53
|
+
|
|
54
|
+
class SymbolTest < Test::Unit::TestCase
|
|
55
|
+
module InnerModule; end
|
|
56
|
+
def test_1
|
|
57
|
+
assert_equal OuterModule, :OuterModule.constantize
|
|
58
|
+
assert_equal OuterModule::InnerModule, :'OuterModule::InnerModule'.constantize
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
class StringTest < Test::Unit::TestCase
|
|
62
|
+
module InnerModule; end
|
|
63
|
+
def test_1
|
|
64
|
+
assert_equal OuterModule, 'OuterModule'.constantize
|
|
65
|
+
assert_equal OuterModule::InnerModule, 'OuterModule::InnerModule'.constantize
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
=end
|
|
69
|
+
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Author:: Tyler Rick
|
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
|
4
|
+
# License:: Ruby License
|
|
5
|
+
# Submit to Facets?:: Maybe, if we can get the bugs worked out. I can't believe Facets has Regexp#chomp, capitalize, downcase, etc., but not match/=~ !
|
|
6
|
+
# Developer notes::
|
|
7
|
+
# * Rename to symbol/regexp.rb ?
|
|
8
|
+
#++
|
|
9
|
+
|
|
10
|
+
require 'rubygems'
|
|
11
|
+
require 'facets/core/module/alias_method_chain'
|
|
12
|
+
|
|
13
|
+
unless defined?(Symbol::HasSymbolSupport)
|
|
14
|
+
|
|
15
|
+
class Symbol
|
|
16
|
+
HasSymbolSupport = true
|
|
17
|
+
|
|
18
|
+
def match(regexp)
|
|
19
|
+
to_s.match(regexp)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Warning: Due to what I think is a bug in Ruby, $1, Regexp.last_match do not yield accurate results when queried after
|
|
23
|
+
# returning from a method call that does a regexp match!
|
|
24
|
+
# If you need access to that data, you're better off just doing :symbol.to_s =~ /regexp/ yourself.
|
|
25
|
+
# If all you need is a true/false (matches or doesn't match) result, then you can use this.
|
|
26
|
+
# :cat =~ /c.t/
|
|
27
|
+
#
|
|
28
|
+
def =~(regexp)
|
|
29
|
+
to_s =~ regexp
|
|
30
|
+
end
|
|
31
|
+
# Seems to be equivalent to this, if anyone cares:
|
|
32
|
+
# m = self.match(regexp)
|
|
33
|
+
# m ? m.begin(0) : nil
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# I would just do something like this, but it causes the $1-not-set-after-method-call bug to infect anything that uses Regexp#===, which would be really bad!!
|
|
38
|
+
#class Regexp
|
|
39
|
+
# alias_method :orig_eee, :===
|
|
40
|
+
# def === (other)
|
|
41
|
+
# orig_eee(other)
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
#end
|
|
45
|
+
# I can't even run the tests in this file after making this (trivial) wrapper for ===! If I try, I get this error:
|
|
46
|
+
#/usr/lib/ruby/1.8/optparse.rb:1099:in `make_switch': undefined method `downcase' for nil:NilClass (NoMethodError)
|
|
47
|
+
# from /usr/lib/ruby/1.8/optparse.rb:1032:in `each'
|
|
48
|
+
# from /usr/lib/ruby/1.8/optparse.rb:1032:in `make_switch'
|
|
49
|
+
# from /usr/lib/ruby/1.8/optparse.rb:1140:in `define'
|
|
50
|
+
# from /usr/lib/ruby/1.8/optparse.rb:1149:in `on'
|
|
51
|
+
# from /usr/lib/ruby/1.8/test/unit/autorunner.rb:106:in `options'
|
|
52
|
+
# from /usr/lib/ruby/1.8/optparse.rb:755:in `initialize'
|
|
53
|
+
# from /usr/lib/ruby/1.8/test/unit/autorunner.rb:101:in `new'
|
|
54
|
+
# from /usr/lib/ruby/1.8/test/unit/autorunner.rb:101:in `options'
|
|
55
|
+
# from /usr/lib/ruby/1.8/test/unit/autorunner.rb:88:in `process_args'
|
|
56
|
+
# from /usr/lib/ruby/1.8/test/unit/autorunner.rb:10:in `run'
|
|
57
|
+
# from /usr/lib/ruby/1.8/test/unit.rb:278
|
|
58
|
+
# from -:46
|
|
59
|
+
|
|
60
|
+
class Regexp
|
|
61
|
+
def eee_with_support_for_symbols (other)
|
|
62
|
+
case other
|
|
63
|
+
when Symbol
|
|
64
|
+
__send__ :===, other.to_s
|
|
65
|
+
else
|
|
66
|
+
__send__ :===, other
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
module WithSupportForSymbols
|
|
71
|
+
def self.extended(base)
|
|
72
|
+
base.class.class_eval do
|
|
73
|
+
alias_method :eee_without_support_for_symbols, :===
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
def === (other)
|
|
77
|
+
case other
|
|
78
|
+
when Symbol
|
|
79
|
+
eee_without_support_for_symbols(other.to_s)
|
|
80
|
+
else
|
|
81
|
+
eee_without_support_for_symbols(other)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
module Enumerable
|
|
88
|
+
def grep_with_regexp_support_for_symbols(pattern, &block)
|
|
89
|
+
map { |element|
|
|
90
|
+
element.is_a?(Symbol) ? element.to_s : element
|
|
91
|
+
}.grep_without_regexp_support_for_symbols(pattern, &block)
|
|
92
|
+
end
|
|
93
|
+
alias_method_chain :grep, :regexp_support_for_symbols
|
|
94
|
+
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end # unless defined?(Symbol::HasSymbolSupport)
|
|
98
|
+
|
|
99
|
+
# _____ _
|
|
100
|
+
# |_ _|__ ___| |_
|
|
101
|
+
# | |/ _ \/ __| __|
|
|
102
|
+
# | | __/\__ \ |_
|
|
103
|
+
# |_|\___||___/\__|
|
|
104
|
+
#
|
|
105
|
+
=begin test
|
|
106
|
+
require 'test/unit'
|
|
107
|
+
require 'rubygems'
|
|
108
|
+
require 'qualitysmith_extensions/object/singleton_send'
|
|
109
|
+
|
|
110
|
+
class TheTest < Test::Unit::TestCase
|
|
111
|
+
def test_equal_tilde
|
|
112
|
+
assert_equal 1, :chopper =~ /hopper/
|
|
113
|
+
end
|
|
114
|
+
def test_match
|
|
115
|
+
assert :chopper.match(/hopper/).is_a?(MatchData)
|
|
116
|
+
assert_equal 'hopper', :chopper.match(/hopper/).to_s
|
|
117
|
+
end
|
|
118
|
+
def test_triple_equal
|
|
119
|
+
assert_equal true, /hopper/ === 'chopper'
|
|
120
|
+
assert_equal false, /hopper/ === :chopper # Doesn't work!
|
|
121
|
+
|
|
122
|
+
assert_equal true, /hopper/.eee_with_support_for_symbols(:chopper)
|
|
123
|
+
assert_equal true, /hopper/.singleton_send(Regexp::WithSupportForSymbols, :===, :chopper)
|
|
124
|
+
regexp = /chopper/
|
|
125
|
+
regexp.extend Regexp::WithSupportForSymbols
|
|
126
|
+
assert_equal true, regexp === :chopper
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Due to what I think is a BUG in Ruby, the details of the last_match appear to be reset after returning from a method.
|
|
130
|
+
# In other words, if you print out $1 from within Symbol#=~, it has the value you'd expected, but as soon as we get back from
|
|
131
|
+
# Symbol#=~, it's gone!
|
|
132
|
+
# See http://svn.tylerrick.com/public/ruby/examples/regexp-variables_reset_after_return_from_method.rb
|
|
133
|
+
def test_the_setting_of_match_variables
|
|
134
|
+
# This doesn't work, unfortunately.
|
|
135
|
+
assert_equal 0, :cat =~ /c(.)t/
|
|
136
|
+
assert_equal nil, $1
|
|
137
|
+
assert_equal nil, Regexp.last_match(1)
|
|
138
|
+
|
|
139
|
+
# Nor this
|
|
140
|
+
assert :cat.match(/c(.)t/)
|
|
141
|
+
assert_equal nil, $1
|
|
142
|
+
assert_equal nil, Regexp.last_match(1)
|
|
143
|
+
|
|
144
|
+
# But if we do the to_s conversion *here* (as opposed to from within Symbol#=~), it works! Yuck!
|
|
145
|
+
assert_equal 0, :cat.to_s =~ /c(.)t/
|
|
146
|
+
assert_equal 'a', $1
|
|
147
|
+
assert_equal 'a', Regexp.last_match(1)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def test_grep
|
|
151
|
+
assert_equal ['a'], ['a', 'b', 'c'].grep(/a/)
|
|
152
|
+
|
|
153
|
+
# This only works because of our grep_with_regexp_support_for_symbols() monkey patch.
|
|
154
|
+
assert_equal ['a'], [:a, :b, :c].grep(/a/)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
=end
|