Class: Ronin::Support::Text::Typo::Generator
- Inherits:
- 
      Object
      
        - Object
- Ronin::Support::Text::Typo::Generator
 
- Defined in:
- lib/ronin/support/text/typo/generator.rb
Overview
Geneerates one or more typos based on a series of substitution rules.
Instance Attribute Summary collapse
- 
  
    
      #rules  ⇒ Array<(Regexp, String)> 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The typo substitution rules. 
Class Method Summary collapse
- 
  
    
      .[](*rules)  ⇒ Generator 
    
    
  
  
  
  
  
  
  
  
  
    Creates a new generator. 
Instance Method Summary collapse
- 
  
    
      #+(other)  ⇒ Generator 
    
    
  
  
  
  
  
  
  
  
  
    Combines the typo generator's rules with another typo generator's rules. 
- 
  
    
      #each_substitution(word) {|typo_word| ... } ⇒ Enumerator 
    
    
  
  
  
  
  
  
  
  
  
    Enumerates over every possible typo substition for the given word. 
- 
  
    
      #initialize(rules)  ⇒ Generator 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Initializes the typo substitution rules. 
- 
  
    
      #substitute(word)  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    Performs a random typo substitution of the given word. 
- 
  
    
      #to_a  ⇒ Array<(Regexp, String)> 
    
    
  
  
  
  
  
  
  
  
  
    Converts the generator into the Array of substitution rules. 
Constructor Details
#initialize(rules) ⇒ Generator
Initializes the typo substitution rules.
| 46 47 48 | # File 'lib/ronin/support/text/typo/generator.rb', line 46 def initialize(rules) @rules = rules end | 
Instance Attribute Details
Class Method Details
.[](*rules) ⇒ Generator
Creates a new generator.
| 67 68 69 | # File 'lib/ronin/support/text/typo/generator.rb', line 67 def self.[](*rules) new(rules) end | 
Instance Method Details
#+(other) ⇒ Generator
Combines the typo generator's rules with another typo generator's rules.
| 81 82 83 | # File 'lib/ronin/support/text/typo/generator.rb', line 81 def +(other) Generator.new(@rules + other.rules) end | 
#each_substitution(word) {|typo_word| ... } ⇒ Enumerator
Enumerates over every possible typo substition for the given word.
| 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | # File 'lib/ronin/support/text/typo/generator.rb', line 127 def each_substitution(word) return enum_for(__method__,word) unless block_given? @rules.each do |regexp,replace| offset = 0 while (match = word.match(regexp,offset)) start, stop = match.offset(0) new_string = word.dup new_string[start...stop] = replace yield new_string offset = stop end end return nil end | 
#substitute(word) ⇒ String
Performs a random typo substitution of the given word.
| 97 98 99 100 101 102 103 104 105 106 107 108 109 | # File 'lib/ronin/support/text/typo/generator.rb', line 97 def substitute(word) matching_rules = @rules.select do |regexp,replace| word =~ regexp end if matching_rules.empty? raise(NoTypoPossible,"no possible typo substitution found in word: #{word.inspect}") end regexp, replace = matching_rules.sample return word.sub(regexp,replace) end |