Class: Word
- Inherits:
-
Object
- Object
- Word
- Defined in:
- lib/word_play.rb
Overview
word_play.rb
This gem file contains the ‘Word’ class definition that tests if a given pair of words is an Anagram and if a given word is a palindrome!
Word class. The Word class consists of methods that could be performed on strings, any number of methods can be added. This file consists of two methods named is_anagram? and is_palindrome?.
Summary
This gem can be used to perform two types of tests on string:
-
Use is_anagram? to check if two strigs are Anagram of each other
-
Use is_palindrome? to check if a string is a Palindrome
Example
word1 = Word.new("scream") # Initialize the class Word
word2 = Word.new("creams") # Initialize the class Word
is_anagram?(word1,word2) # Pass 2 words to check if they are Anagram
is_palindrome("Madam") # Check if a word is palindrome
Anagram definition:
A word or phraseis said to be an Anagram if rearranging the exact letters would
produce another word. Example: scream and creams
Palindrome definition
A word or phrase is said to be a Palindrome, if we obtain the same word when read from the reverse. Example: “A nut for a jar of tuna”
Reference
Contact
- Author
-
Anupama Iyengar ([email protected])
- Website
-
”
- Date
-
Sunday November 17, 2013
Class Method Summary collapse
-
.is_anagram?(text1, text2) ⇒ Boolean
Return _true_ if the two entered strings are Anagram, else return _false_.
-
.is_palindrome?(text) ⇒ Boolean
Return _true_ if the entered string is a *Palindrome*, else return _false_.
-
.process(text) ⇒ Object
In order to check for *Anagram* convert all the letters into lowercase, split them into letters, sort them and join them to form a string!.
Class Method Details
.is_anagram?(text1, text2) ⇒ Boolean
Return _true_ if the two entered strings are Anagram, else return _false_
56 57 58 59 60 61 62 63 64 |
# File 'lib/word_play.rb', line 56 def is_anagram?(text1,text2) if (process(text1) == process(text2)) puts " The 2 words are anagram of each other" return true else puts " The 2 words are not anagrams" return false end end |
.is_palindrome?(text) ⇒ Boolean
Return _true_ if the entered string is a *Palindrome*, else return _false_
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/word_play.rb', line 68 def is_palindrome?(text) if (text.downcase == text.downcase.reverse) puts " The word is a palindrome" return true else puts " The words is not a palindrome" return false end end |
.process(text) ⇒ Object
In order to check for *Anagram* convert all the letters into lowercase, split them into letters, sort them and join them to form a string!
50 51 52 |
# File 'lib/word_play.rb', line 50 def process(text) text.downcase.split('').sort.join end |