Sha256: 12a0f291e32ec382398e9c69bdaacaa89c014e66a73861015f3938deb52250e9

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

 # The program takes an initial word or phrase from
 # the command line (or in the absence of a
 # parameter from the first line of standard
 # input).  In then reads successive words or
 # phrases from standard input and reports whether
 # they are angrams of the first word.
 #
 # Author::    Dave Thomas  (mailto:dave@x.y)
 # Copyright:: Copyright (c) 2002 The Pragmatic Programmers, LLC
 # License::   Distributes under the same terms as Ruby

 # This class holds the letters in the original
 # word or phrase. The is_anagram? method allows us
 # to test if subsequent words or phrases are
 # anagrams of the original.

 class Anagram

   # Remember the letters in the initial word
   def initialize(text)
     @initial_letters = letters_of(text)
   end

   # Test to see if a new word contains the same
   # letters as the original
   def is_anagram?(text)
     @initial_letters == letters_of(text)
   end

   # Determine the letters in a word or phrase
   #
   # * all letters are converted to lower case
   # * anything not a letter is stripped out
   # * the letters are converted into an array
   # * the array is sorted
   # * the letters are joined back into a string

   def letters_of(text)
     text.downcase.delete('^a-z').split('').sort.join
   end
 end


 tester = Anagram.new(ARGV.shift || gets)

 ARGF.each do |text|
   puts "Anagram! " if tester.is_anagram? text
 end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sqlpostgres-1.3.0 tools/rdoc/EXAMPLE.rb
sqlpostgres-1.2.6 tools/rdoc/EXAMPLE.rb
sqlpostgres-1.2.5 tools/rdoc/EXAMPLE.rb
sqlpostgres-1.2.4 tools/rdoc/EXAMPLE.rb