Раздел «Язык Ruby».PhoneBookZhdanovSergey:

Реализация телефонной книжки

#Phone Book by Sergey Zhdanov, MIPT, 2007, group 672
#phone's canonical form - only digits for example: 89161234567
#name's capitalize form - capitalized word divided by space

#Define Class Phonebook

class Phonebook
    attr_reader :data
    
#Initalize Pb
    def initialize
        @data = Hash.new
      end
      
#Returns phone of contacts with recieved name or message about its absence
    def [](name)
      if @data[conv_name(name)]
        @data[conv_name(name)]
      else
          "contact ( #{conv_name(name)} ) not found"
      end
    end
    alias lookup []
    
#Creats new pb's contacts with recieved name and phone
    def []=(name, phone)
        if @data[conv_name(name)]
          p "contact ( #{conv_name(name)} ) updated"
        else
          p "contact ( #{conv_name(name)} ) added"
        end
        @data[conv_name(name)] = conv_ph(phone)
    end
    alias insert  []=    
    
#Returns names of contacts with recieved phone or message about its absence
    def lookup_name(phone)
        res = []
        @data.each { |name, ph|
            if (conv_ph(phone) == ph)
                res.push name
            end
        }
        if res.size != 0
          res.sort
        else
            "contact with phone:( #{conv_ph(phone)} ) not found"
        end
      end
      
#Finds all contacs with recieved key words
    def find(kws)
      res = []
      @data.each{ |name , phone|
        f = 0
        (kws.downcase.split(' ')).each{|kw| 
          if ((name.downcase).include? kw) == false
            f = 1
          end
        }
        if f == 0
          res.push [name,phone]
        end
      }
      if res.size !=0
        res.sort
      else
        "contacts with key words: ( #{kws} ) are not exist"
      end
    end
    
#Deletes contact with recieved name  or returns message about its absence
    def del_name(name)
      if @data[conv_name(name)]
        @data.delete(conv_name(name))
        "contact ( #{conv_name(name)} ) deleted"
      else
        "contact ( #{conv_name(name)} ) not found"
      end
    end
    
#Returns pb as array
    def to_a 
        @data.to_a.sort
      end
      
#Returns pb as array of all names
    def names 
        data.keys.sort
      end
      
#Returns pb as array of all phones
    def phones
        data.values.sort
      end
      
#Saves pb to file
    def save (filename)
        file = File.new(filename, "w+")
        data.each { |name, phone|
            file.puts "#{name} #{phone}"
        }
        p "file #{filename} saved"
        file.close
      end
      
#Loads pb from a file
    def load (filename)
        if File.exist?(file_name) == false
          p "file #{filename} doesn't exist"
        else
          p "Opening #{filename} ..."
          file = File.new(filename, "r")
          @data.clear
          file.readlines { |line|
            name, phone = line.chomp.split(" ")
            if name != nil && phone != nil
                @data[conv_name(name)] = conv_ph(phone)
            end
          }
        end
        file.close
    end
#Updates pb from a file
    def update (filename)
        if File.exist?(file_name) == false
          p "file #{filename} doesn't exist"
        else
          p "Opening #{filename} ..."
          file = File.new(filename, "r")
          file.readlines { |line|
            name, phone = line.chomp.split(" ")
            if name != nil && phone != nil
                @data[conv_name(name)] = conv_ph(phone)
            end
          }
        end
        file.close
    end
        
end

#Other Usable blocks:

#Converts phone to canonical form
    def conv_ph(phone)
      (phone.delete  "-() ").gsub("+7","8")
    end

#Convert name to capitalized form
  def conv_name(name)
    (name.split(' ').each{|str| str.capitalize!}).join(' ')
  end 

#Commands
pb = Phonebook.new
pb["asya ivanova"]         = "  +7 916 134-343-33"
pb["vasya pupkin"]         = "89161889897"
pb["petya sokolov"]       =  "+7324234234"
pb["sasha petrov"]         = "8(916)23424343"
pb["sasha nikolaev"]      = "8 (926) 234-34-23"
pb["pasha stromilov"]    = "8-901-1234534"
pb["asya ivanova"]         = "  +7 916 134-343-56"
p "---------------"
pb.save("c:/a.txt")
p "---------------"
p pb.lookup("peya")
p "---------------"
p pb.lookup("asya ivanova")
p "---------------"
pb.names.each { |name|
  p name
}
p "---------------"
pb.phones.each { |phone|
  p phone
}
p "---------------"
p pb.lookup_name("123")
p "---------------"
p pb.lookup_name("  +7-( 926 )--23434-23")
p "---------------"
pb.to_a.each { |name, phone|
  p name+" - "+phone
}
p "---------------"
p pb.del_name("petya sokolov")
p"-----------------"
pb.to_a.each { |name, phone|
  p name+" - "+phone
}
p "----------------"
pb.find("sh ov").each { |name, phone|
  p name+" - "+phone
}
p "----------------"
pb.find("asya").each { |name, phone|
  p name+" - "+phone
}}
Результат выполнения программы:
"contact ( Asya Ivanova ) added"
"contact ( Vasya Pupkin ) added"
"contact ( Petya Sokolov ) added"
"contact ( Sasha Petrov ) added"
"contact ( Sasha Nikolaev ) added"
"contact ( Pasha Stromilov ) added"
"contact ( Asya Ivanova ) updated"
"---------------"
"file c:/a.txt saved"
"---------------"
"contact ( Peya ) not found"
"---------------"
"891613434356"
"---------------"
"Asya Ivanova"
"Pasha Stromilov"
"Petya Sokolov"
"Sasha Nikolaev"
"Sasha Petrov"
"Vasya Pupkin"
"---------------"
"8324234234"
"89011234534"
"891613434356"
"89161889897"
"891623424343"
"89262343423"
"---------------"
"contact with phone:( 123 ) not found"
"---------------"
["Sasha Nikolaev"]
"---------------"
"Asya Ivanova - 891613434356"
"Pasha Stromilov - 89011234534"
"Petya Sokolov - 8324234234"
"Sasha Nikolaev - 89262343423"
"Sasha Petrov - 891623424343"
"Vasya Pupkin - 89161889897"
"---------------"
"contact ( Petya Sokolov ) deleted"
"-----------------"
"Asya Ivanova - 891613434356"
"Pasha Stromilov - 89011234534"
"Sasha Nikolaev - 89262343423"
"Sasha Petrov - 891623424343"
"Vasya Pupkin - 89161889897"
"----------------"
"Pasha Stromilov - 89011234534"
"Sasha Petrov - 891623424343"
"----------------"
"Asya Ivanova - 891613434356"
"Vasya Pupkin - 89161889897"