Displaying Hash Problems

Hi All,

just getting back into learning Ruby and thought I’d play with classes
and
hashes today. Below code produces the following ouput,

[email protected]:~/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.get_song_type

CODE:

[email protected]:~/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.

Mark S.

“Tell me and I’ll forget, show me and I may not remember, involve me,
and
I’ll understand.”

Native American Proverb

Hi All,

I now get this,

[email protected]:~/study/ruby$ ruby songs.rb
Enter your name
Mark
Enter song type
Rock
Enter song title
Wire
Enter song artist
U2

Title
Artist
Type
newSong.get_song_title
newSong.get_song_artist
newSong.get_song_type
[email protected]:~/study/ruby$

for this code change,

##display array contant
puts
puts(aName.keys+aName.values)

I know I’m close. Cheers.

Mark S.

“Tell me and I’ll forget, show me and I may not remember, involve me,
and
I’ll understand.”

Native American Proverb

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

Enter song type
CODE:
end
return @song_type

newSong.set_song_artist(gets())

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.

puts does funny things to arrays. try p instead.

p aName

Regards, Morton

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

Enter song type
CODE:
end
return @song_type

newSong.set_song_artist(gets())

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

On Oct 18, 2006, at 12:43 AM, Morton G. wrote:

puts does funny things to arrays. try p instead.

p aName

Oops, that should be: puts does funny things to arrays and hashes.

Regards, Morton

On 10/18/06, Morton G. [email protected] wrote:

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

Thanx for that. I’ve corrected that, but the below code still won’t work
correctly,

CODE OUTPUT:

[email protected]:~/study/ruby$ ruby songs.rb
Enter your name
Mark
Enter song type
Rock
Enter song title
Wire
Enter song artist
U2

[“Name”, “Title”, “Artist”, “Type”, “aName”, “newSong.get_song_title”, "
newSong.get_song_artist", “newSong.get_song_type”]

I wish to dislay like this,

Name value
Title value
Artist value
Type value

Cheers.

Mark S…

CODE:

##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
songList = Hash.new
songList[‘Name’] = ‘aName’
songList[‘Title’] = ‘newSong.get_song_title’
songList[‘Type’] = ‘newSong.get_song_type’
songList[‘Artist’] = ‘newSong.get_song_artist’

##display array contant
puts
p(songList.keys+songList.values)

“Tell me and I’ll forget, show me and I may not remember, involve me,
and
I’ll understand.”

Native American Proverb

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs