qualitysmith_extensions 0.0.17 → 0.0.20

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.
@@ -25,8 +25,9 @@ class Module
25
25
  module_eval <<-End, __FILE__, __LINE__+1
26
26
  def #{method_name}(&block)
27
27
  old_guard_state, #{guard_variable} = #{guard_variable}, true
28
- yield
28
+ returning = yield
29
29
  #{guard_variable} = old_guard_state
30
+ returning
30
31
  end
31
32
  End
32
33
  end
@@ -41,8 +42,9 @@ class Module
41
42
  class << self
42
43
  def #{method_name}(&block)
43
44
  old_guard_state, #{guard_variable} = #{guard_variable}, true
44
- yield
45
+ returning = yield
45
46
  #{guard_variable} = old_guard_state
47
+ returning
46
48
  end
47
49
  end
48
50
  End
@@ -82,6 +84,9 @@ class GuardMethodTest < Test::Unit::TestCase
82
84
  end
83
85
  end
84
86
  end
87
+ def test_return_value
88
+ assert_equal 'special return value', A.new.guard_the_fruit! { 'special return value' }
89
+ end
85
90
  end
86
91
 
87
92
  class MGuardMethodTest < Test::Unit::TestCase
@@ -106,6 +111,9 @@ class MGuardMethodTest < Test::Unit::TestCase
106
111
  end
107
112
  end
108
113
  end
114
+ def test_return_value
115
+ assert_equal 'special return value', B.guard_the_fruit! { 'special return value' }
116
+ end
109
117
 
110
118
  end
111
119
  =end
@@ -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
+
@@ -2,19 +2,100 @@
2
2
  # Author:: Tyler Rick
3
3
  # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
4
  # License:: Ruby License
5
- # Submit to Facets?:: Yes! I can't believe Facets has Regexp#chomp, capitalize, downcase, etc., but not match/=~ !
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
6
  # Developer notes::
7
+ # * Rename to symbol/regexp.rb ?
7
8
  #++
8
9
 
10
+ require 'rubygems'
11
+ require 'facets/core/module/alias_method_chain'
12
+
13
+ unless defined?(Symbol::HasSymbolSupport)
14
+
9
15
  class Symbol
16
+ HasSymbolSupport = true
17
+
10
18
  def match(regexp)
11
19
  to_s.match(regexp)
12
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
+ #
13
28
  def =~(regexp)
14
29
  to_s =~ regexp
15
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
+
16
95
  end
17
96
 
97
+ end # unless defined?(Symbol::HasSymbolSupport)
98
+
18
99
  # _____ _
19
100
  # |_ _|__ ___| |_
20
101
  # | |/ _ \/ __| __|
@@ -23,6 +104,8 @@ end
23
104
  #
24
105
  =begin test
25
106
  require 'test/unit'
107
+ require 'rubygems'
108
+ require 'qualitysmith_extensions/object/singleton_send'
26
109
 
27
110
  class TheTest < Test::Unit::TestCase
28
111
  def test_equal_tilde
@@ -32,5 +115,43 @@ class TheTest < Test::Unit::TestCase
32
115
  assert :chopper.match(/hopper/).is_a?(MatchData)
33
116
  assert_equal 'hopper', :chopper.match(/hopper/).to_s
34
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
35
156
  end
36
157
  =end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: qualitysmith_extensions
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.17
7
- date: 2007-04-17 00:00:00 -07:00
6
+ version: 0.0.20
7
+ date: 2007-04-26 00:00:00 -07:00
8
8
  summary: A collection of reusable Ruby methods developed by QualitySmith.
9
9
  require_paths:
10
10
  - lib
@@ -96,6 +96,7 @@ files:
96
96
  - lib/qualitysmith_extensions/string/md5.rb
97
97
  - lib/qualitysmith_extensions/string/to_underscored_label.rb
98
98
  - lib/qualitysmith_extensions/string/all.rb
99
+ - lib/qualitysmith_extensions/string/each_char_with_index.rb
99
100
  - lib/qualitysmith_extensions/class/class_methods.rb
100
101
  - test/all.rb
101
102
  - Readme