ruby-nuggets 0.1.8.235 → 0.1.9.237

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.1.8
5
+ This documentation refers to ruby-nuggets version 0.1.9
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -26,16 +26,18 @@
26
26
  #++
27
27
 
28
28
  require 'rubygems'
29
- require 'text'
29
+ require 'amatch'
30
30
 
31
31
  module Enumerable
32
32
 
33
33
  # call-seq:
34
34
  # enum.agrep(pattern[, distance]) => anArray
35
+ # enum.agrep(pattern[, distance]) { |obj| ... } => anArray
35
36
  #
36
- # Returns an array of every element in _enum_ for which the Levenshtein
37
- # distance between +pattern+ and +element+ is at most +distance+. Hence,
38
- # errors in the match are allowed, similar to what the UNIX tool agrep does.
37
+ # Returns an array of every element in _enum_ for which +pattern+ approximately
38
+ # matches +element+ (see Amatch::Levenshtein#search). If the optional +block+
39
+ # is supplied, each matching element is passed to it, and the block‘s result
40
+ # is stored in the output array.
39
41
  #
40
42
  # LIMITATIONS:
41
43
  #
@@ -43,13 +45,14 @@ module Enumerable
43
45
  # to their respective source. (Equivalent to <tt>agrep -k</tt>)
44
46
  # - Only works with string elements in _enum_. (Calls +to_s+ on each element)
45
47
  # - The cost for individual error types (substitution, insertion, deletion)
46
- # cannot be adjusted.
47
- def agrep(pattern, distance = 0, cost = 1)
48
+ # cannot be adjusted.
49
+ def agrep(pattern, distance = 0, &block)
48
50
  pattern = pattern.source if pattern.is_a?(Regexp)
49
51
 
50
- select { |obj|
51
- Text::Levenshtein.distance(pattern, obj.to_s) * cost <= distance
52
- }
52
+ amatch = Amatch::Levenshtein.new(pattern)
53
+ matches = select { |obj| amatch.search(obj.to_s) <= distance }
54
+
55
+ block ? matches.map(&block) : matches
53
56
  end
54
57
 
55
58
  end
@@ -65,5 +68,5 @@ if $0 == __FILE__
65
68
  p e.grep(/qu.x/)
66
69
  p e.agrep(/qu.x/)
67
70
 
68
- p [123, 124, 1233].agrep(/123/, 1)
71
+ #p [123, 124, 1233].agrep(/123/, 1)
69
72
  end
@@ -0,0 +1,43 @@
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 File.join(File.dirname(__FILE__), '..', 'enumerable', 'agrep')
29
+
30
+ class IO
31
+
32
+ # call-seq:
33
+ # IO.agrep(fd, pattern[, distance]) => anArray
34
+ #
35
+ def self.agrep(fd, pattern, distance)
36
+ open(fd) { |io| io.agrep(pattern, distance) }
37
+ end
38
+
39
+ end
40
+
41
+ if $0 == __FILE__
42
+ puts File.agrep(__FILE__, /calls/, 2)
43
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 1
7
- TINY = 8
7
+ TINY = 9
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8.235
4
+ version: 0.1.9.237
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -26,6 +26,7 @@ extra_rdoc_files:
26
26
  files:
27
27
  - lib/nuggets/all.rb
28
28
  - lib/nuggets/version.rb
29
+ - lib/nuggets/io/agrep.rb
29
30
  - lib/nuggets/file/which.rb
30
31
  - lib/nuggets/integer/factorial.rb
31
32
  - lib/nuggets/integer/to_binary_s.rb