The distinctive Perl camel is (c) O'Reilly
Perl Workshop Home Page
Home of the Bioinformatics Perl Workshop perl workshop > courses > introduction to ruby (5.1.1.5) > Introduction to Ruby (.1/5) > address_book_with_save.rb (.c1)

course 5.1.1.5

Level: beginner
5.1.1.5.1
Ruby syntax; introduction to strings, arrays and hashes

legend

course code

cat.course.level.sessions.session

e.g. 1.0.1.8

categories

0 | introduction and orientation

1 | perl fundamentals

2 | shell and prompt tools

3 | web development

4 | CPAN Modules

5 | Ruby

levels

level: all all ( 0 )

level: beginner beginner ( 1 )

level: intermediate intermediate ( 2 )

level: advanced advanced ( 3 )

[ consider using for instead of foreach ]

lecture code viewer

downloads

Code
Introduction to Ruby
Introduction to Ruby
Edward Ocampo-Gooding
#!/usr/bin/env ruby -w require "yaml" # An address book def prompt(addresses) menu = " 1. Look for someone 2. Enter some data 3. Show all entries 4. Getouttahere (quit) " loop do puts menu # Chomp is used all over the place in order to get rid of newline characters (\n) input = gets.chomp next if input == nil case input when '1' look_for_someone(addresses) when '2' enter_data(addresses) when '3' show_all(addresses) when '4' puts "Writing stuff down..." begin File.open("addresses.yaml", "w") { |f| YAML.dump(addresses, f) } puts "Done! See you later!" rescue puts "Uh oh... there's been a problem, so you might have lost some data." end exit else puts "Please select a command from the menu" end end end def look_for_someone(addresses) puts "Who or what would you like to look for?" query = gets.chomp # Look for it directly (fastest, constant time lookup) if addresses.has_key?(query) result = addresses[query] else # We couldn't find a direct lookup, so just hunt through everything else found_entries = addresses.select do |key, value| key.downcase.include?(query.downcase) || value.downcase.include?(query.downcase) end if found_entries.empty? puts "Sorry, I couldn't find anything that matches your request." else # We got something. result = "" found_entries.each do |key, value| result << "#{key}\t#{value}\n" end puts result end end end def enter_data(addresses) puts "I'm gonna need a name, and some other address-like stuff, ok? It'll happen in two steps. Be cool." puts "\nPlease enter a name (and hit when you're done.)" name = gets.chomp puts "\nGreat. Now enter some address-data all on one line. (Hit when you're done.)" address = gets.chomp if addresses.has_key?(name) puts "\nWhoa whoa whoa... We've already got that guy in here. Are you sure you want to overwrite that entry? Enter 'yes' or 'no':" if gets.include?('y') puts "Alright, I wrote over the entry." addresses[name] = address else puts "Ok, I didn't do anything." end else # A fresh new entry puts "Entry added:" puts "#{name}\t#{address}" addresses[name] = address end end def show_all(addresses) addresses.each do |key, value| puts "#{key}\t#{value}" end end begin addresses = YAML.load_file("addresses.yaml") rescue puts "No address file found. This must be your first time." addresses = {} end puts "Hi, this is an address book. What would you like to do?" prompt(addresses)

1 | Introduction to Ruby | 5.1.1.5.1

5.1.1.5.1.c1 | address_book_with_save.rb | Edward Ocampo-Gooding | code
5.1.1.5.1.a1 | Introduction to Ruby | Edward Ocampo-Gooding | pdf
5.1.1.5.1.a2 | Introduction to Ruby | Edward Ocampo-Gooding | pdf