Class: Wordplay

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

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Wordplay

Public: creates a class instance word - a String



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

def initialize(word)
  @word = word.downcase # always work with lowercase
end

Instance Method Details

#ordered_lettersObject

Public: orders the letters of a word in alphabetical order.

Returns a String



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

def ordered_letters
  @word.chars.sort.join
end

#ordered_letters?Boolean

Public: determines if the component letters of the provided word are in alphabetical order. E.g.: “fox” has its letters in order, “car” does not.

Returns a Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/wordplay.rb', line 13

def ordered_letters?
  # we could just run `@word == ordered_letters` but this is faster
  1.upto(@word.length - 1) do |i|
    return false unless @word[i] >= @word[i - 1]
  end
  true
end

#palindrome?Boolean

Public: determines if the provided string is a palindome Only non-accented ASCII letters are accepted for now.

Returns a Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/wordplay.rb', line 51

def palindrome?
  formatted_word = @word.gsub(/[^a-z]/, "")
  formatted_word == formatted_word.reverse
end

#reverse_ordered_lettersObject

Public: orders the letters of a word in reverse alphabetical order.

Returns a String



43
44
45
# File 'lib/wordplay.rb', line 43

def reverse_ordered_letters
  ordered_letters.reverse
end

#reverse_ordered_letters?Boolean

Public: determines if the component letters of the provided word are in reverse alphabetical order. E.g.: “pig” has its letters in reverse order, “car” does not.

Returns a Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/wordplay.rb', line 26

def reverse_ordered_letters?
  1.upto(@word.length - 1) do |i|
    return false unless @word[i] <= @word[i - 1]
  end
  true
end