Garance A Drosehn
10/19/2006 12:25:00 AM
First, in this section:
> #create new song array
> songList = Hash.new
> songList['Name'] = 'aName'
> songList['Title'] = 'newSong.get_song_title'
> songList['Type'] = 'newSong.get_song_type'
> songList['Artist'] = 'newSong.get_song_artist'
There is no reason to have single-quotes around the right-hand side.
The single-quotes mean you want literal-strings, not the values returned
by those method calls. So, change that to:
#create new song array
songList = Hash.new
songList['Name'] = aName
songList['Title'] = newSong.get_song_title
songList['Type'] = newSong.get_song_type
songList['Artist'] = newSong.get_song_artist
Second, once you do that, you'll perhaps notice that all of the values
that you read in include a new-line character ("\n") at the end of them.
My guess is that you don't want those to be there, so change that earlier
section to add some calls to "chomp":
##create user name for array name
puts "Enter your name"
aName = gets().chomp
##create new song from class, Songs
newSong = Songs.new
puts "Enter song type"
newSong.set_song_type(gets().chomp)
puts "Enter song title"
newSong.set_song_title(gets().chomp)
puts "Enter song artist"
newSong.set_song_artist(gets().chomp)
Third the call to 'p' in the final section is not going to do what you seem
to think it will do. 'p' is more of a debugging aid, and not a pretty-print
command. You give 'p' one or more objects, and it prints the result it
gets from calling the "inspect" method on each one of those objects.
> ##display array contant
> puts
> p(songList.keys+songList.values)
In this command, you're creating *one* array, where the value of that
array is the list of keys for songList, followed by the list of all values
for that songList. You're giving it one object, and it is printing out
that object. That object is a single array, so it prints out the single
array on a single line.
I am not sure what you want there, but if it were me, I woud use 'printf'
instead of 'p' for this. You might want to try an ending of:
puts
p(songList)
puts
songList.each_key { |skey|
printf "%-8s %s\n", skey + ":", songList[skey]
}
puts
That will print out your value twice. I expect the second version is
pretty lose to what you're hoping for. An example run:
(117) /tmp/song_list.rb
Enter your name
gad
Enter song type
Progressive Rock
Enter song title
Arriving Somewhere but not Here
Enter song artist
Porcupine Tree
{"Name"=>"gad", "Title"=>"Arriving Somewhere but not Here",
"Artist"=>"Porcupine Tree", "Type"=>"Progressive Rock"}
Name: gad
Title: Arriving Somewhere but not Here
Artist: Porcupine Tree
Type: Progressive Rock
You could also change the line:
printf "%-8s %s\n", skey + ":", songList[skey]
to
printf "%-8s %s\n", skey + ":", songList[skey].inspect
to get an idea of what the 'p' command is doing for you.
--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA