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