Class: Fae::Language

Inherits:
Object
  • Object
show all
Defined in:
lib/fae/language.rb

Overview

A language described by any number of characters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(characters) ⇒ Language

Creates a new language instance.

Parameters:

  • characters (Array)

    an array of characters



10
11
12
13
# File 'lib/fae/language.rb', line 10

def initialize(characters)
  @characters = []
  @characters = characters.uniq
end

Instance Attribute Details

#charactersObject

Returns the value of attribute characters.



5
6
7
# File 'lib/fae/language.rb', line 5

def characters
  @characters
end

Instance Method Details

#add_character(char) ⇒ Object

Adds a character to the language.

Parameters:

  • char (String)

    the character to add



31
32
33
# File 'lib/fae/language.rb', line 31

def add_character(char)
  @characters << char
end

#string_is_valid(string) ⇒ Object

Checks if a string is valid for this language.

Parameters:

  • string (String)

    the string to check



18
19
20
21
22
23
24
25
26
# File 'lib/fae/language.rb', line 18

def string_is_valid(string)
  # Use lookahead to check for valid string
  regex = "^(?=.*\\D)[#{@characters.join('|')}]+$"

  if (string.match /#{regex}/)
    return true
  end
  return false
end