[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Displaying Hash Problems

Morton Goldberg

10/18/2006 4:54:00 AM


On Oct 17, 2006, at 11:58 PM, Mark Sargent wrote:

> Hi All,
>
> just getting back into learning Ruby and thought I'd play with
> classes and
> hashes today. Below code produces the following ouput,
>
>
> mark@marklaptop:~/study/ruby$ ruby songs.rb
> Enter your name
> Mark
> Enter song type
> Rock
> Enter song title
> I Was Made For Loving You
> Enter song artist
> Kiss
>
> TitlenewSong.get_song_titleArtistnewSong.get_song_artistTypenewSong.ge
> t_song_type
>
>
> CODE:
>
> mark@marklaptop:~/study/ruby$ cat songs.rb
> ##define song class
> class Songs
> def set_song_type(type)
> @song_type = type
> end
>
> def set_song_title(title)
> @song_title = title
> end
>
> def set_song_artist(artist)
> @song_artist = artist
> end
>
> def get_song_title
> return @song_title
> end
>
> def get_song_type
> return @song_type
> end
>
> def get_song_artist
> return @song_artist
> end
> end
>
> ##create user name for array name
> puts "Enter your name"
> aName = gets()
>
> ##create new song from class, Songs
> newSong = Songs.new
>
> puts "Enter song type"
> newSong.set_song_type(gets())
>
> puts "Enter song title"
> newSong.set_song_title(gets())
>
> puts "Enter song artist"
> newSong.set_song_artist(gets())
>
> #create new song array
> aName = Hash.new
> aName['Title'] = 'newSong.get_song_title'
> aName['Type'] = 'newSong.get_song_type'
> aName['Artist'] = 'newSong.get_song_artist'
>
> ##display array contant
> puts
> puts(aName)
>
>
> --
>
> I'm figuring it has something to do with how I assigned the values
> to the
> keys, but, cannot for the life of me see what. Any suggestions for
> me for
> where to look? Cheers.

Also consider:

#create new song array
aName = Hash.new ### This clobbers the previous value of aName
aName['Title'] = newSong.get_song_title
aName['Type'] = newSong.get_song_type
aName['Artist'] = newSong.get_song_artist

p aName

Regards, Morton



1 Answer

Garance A Drosehn

10/19/2006 12:25:00 AM

0

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