Class: CWG::Alphabet

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

Overview

class Alphabet provides alphabet generation functionality

Instance Method Summary (collapse)

Constructor Details

- (Alphabet) initialize(options = {})

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 '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 18

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

Instance Method Details

- (Object) alphabet

return string containing alphabet



24
25
26
# File '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 24

def alphabet
  'abcdefghijklmnopqrstuvwxyz'
end

- (Object) exclude_letters

exclude letters passed in as string if :exclude defined



50
51
52
53
54
# File '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 50

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

- (Object) generate

generate alphabet with options acted upon

Returns:

alphabet or filtered alphabet



60
61
62
63
64
65
66
67
# File '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 60

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

- (Object) include_letters

include letters passed in as string if :include defined



42
43
44
45
46
# File '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 42

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

- (Object) reverse_alphabet_maybe

reverse alphabet if :reverse option defined



30
31
32
# File '/Users/martyn/cw/cw_clone/lib/cw/alphabet.rb', line 30

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

- (Object) shuffle_alphabet_maybe

shuffle alphabet if :shuffle option defined



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

def shuffle_alphabet_maybe
  @letters = @letters.split('').shuffle.join if @options[:shuffle]
end