Class: CW::Alphabet

Inherits:
Object
  • Object
show all
Defined in:
lib/cw/alphabet.rb

Overview

class Alphabet provides alphabet generation functionality

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Alphabet

initialize class Alphabet

Parameters:

options

An optional hash containg options:

:reverse - reverse alphabet :shuffle - shuffle alphabet :include - include only these letters of the alphabet :exclude - exclude these letters from the alphabet



18
19
20
# File 'lib/cw/alphabet.rb', line 18

def initialize(options = {})
  @options = options
end

Instance Method Details

#alphabetObject

alphabet() Returns alphabet as string

Parameters:

none



27
28
29
# File 'lib/cw/alphabet.rb', line 27

def alphabet
  'abcdefghijklmnopqrstuvwxyz'
end

#exclude_lettersObject

exclude_letters() exclude letters if :exclude option defined

Parameters:

none



68
69
70
71
72
# File 'lib/cw/alphabet.rb', line 68

def exclude_letters
  if @options[:exclude]
    @letters.tr!(@options[:exclude],'')
  end
end

#generateObject

generate() generate alphabet with options acted upon

Returns:

alphabet or filtered alphabet



79
80
81
82
83
84
85
86
# File 'lib/cw/alphabet.rb', line 79

def generate
  @letters = alphabet
  include_letters
  exclude_letters
  shuffle_alphabet_maybe
  reverse_alphabet_maybe
  @letters.split('').join(' ')
end

#include_lettersObject

include_letters() include letters if :include option defined

Parameters:

none



57
58
59
60
61
# File 'lib/cw/alphabet.rb', line 57

def include_letters
  if @options[:include]
    @letters = @options[:include]
  end
end

#reverse_alphabet_maybeObject

reverse_alphabet_maybe() reverse letters if reverse option set

Parameters:

none



36
37
38
# File 'lib/cw/alphabet.rb', line 36

def reverse_alphabet_maybe
  @letters.reverse! if @options[:reverse]
end

#shuffle_alphabet_maybeObject

shuffle_alphabet_maybe() shuffle alphabet if :shuffle option defined don’t shuffle if in test environment

Parameters:

none



46
47
48
49
50
# File 'lib/cw/alphabet.rb', line 46

def shuffle_alphabet_maybe
  unless(ENV["CW_ENV"] == "test")
    @letters = @letters.split('').shuffle.join if @options[:shuffle]
  end
end