Class: Swearjar

Inherits:
Object
  • Object
show all
Defined in:
lib/swearjar.rb,
lib/swearjar/version.rb

Constant Summary collapse

VERSION =
'1.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Swearjar

Returns a new instance of Swearjar.



12
13
14
15
16
# File 'lib/swearjar.rb', line 12

def initialize(file = nil)
  @hash = {}
  @regexs = {}
  load_file(file) if file
end

Class Method Details

.defaultObject



4
5
6
# File 'lib/swearjar.rb', line 4

def self.default
  from_language('en')
end

.from_language(language) ⇒ Object



8
9
10
# File 'lib/swearjar.rb', line 8

def self.from_language(language)
  new(File.join(File.dirname(__FILE__), 'config', "#{language}.yml"))
end

Instance Method Details

#censor(string) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/swearjar.rb', line 37

def censor(string)
  censored_string = string.to_s.dup
  scan(string) do |test, word, position|
    next unless test
    replacement = block_given? ? yield(word) : word.gsub(/\S/, '*')
    censored_string[position, word.size] = replacement
  end
  censored_string
end

#profane?(string) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/swearjar.rb', line 18

def profane?(string)
  string = string.to_s
  scan(string) {|test| return true if test }
  false
end

#scorecard(string) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/swearjar.rb', line 24

def scorecard(string)
  string = string.to_s
  scorecard = {}
  scan(string) do |test|
    next unless test
    test.each do |type|
      scorecard[type] = 0 unless scorecard.key?(type)
      scorecard[type] += 1
    end
  end
  scorecard
end