# Displaying Hash Problems

**URL:** <https://www.ruby-forum.com/t/displaying-hash-problems/72579>\
**Category:** Ruby\
**Created:** [October 18, 2006, 5:59am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579 "2006-10-18T05:59:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 18, 2006, 5:59am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/1 "2006-10-18T05:59:26Z")

</div>

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.get\_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.

Mark S.

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

Native American Proverb

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 18, 2006, 6:23am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/2 "2006-10-18T06:23:27Z")

</div>

Hi All,

I now get this,

mark@marklaptop:~/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  
mark@marklaptop:~/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

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 18, 2006, 6:45am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/3 "2006-10-18T06:45:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 18, 2006, 6:55am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/4 "2006-10-18T06:55:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 18, 2006, 6:59am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/5 "2006-10-18T06:59:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 19, 2006, 12:25am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/6 "2006-10-19T00:25:42Z")

</div>

On 10/18/06, Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

mark@marklaptop:~/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

---

<div class="post-metadata">

**Author:** ![coolboarderguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@coolboarderguy](https://www.ruby-forum.com/u/coolboarderguy)\
**Post date:** [October 19, 2006, 2:25am UTC](https://www.ruby-forum.com/t/displaying-hash-problems/72579/7 "2006-10-19T02:25:57Z")

</div>

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.
