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, valid_block = nil) ⇒ 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, valid_block=nil)
  @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



26
27
28
# File 'lib/fae/language.rb', line 26

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
# File 'lib/fae/language.rb', line 18

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