Next 0.2 Small Ruby snippet (custom classes) Prev

Small Ruby snippet (custom classes)

class Song
    attr_reader :title, :album, :artist

    def initialize(filename)
        @artist, @album, @title = filename.split("/")[-3..-1]
        @title.chomp!(".mp3")
    end

    def play
        puts @title
    end
end

song = Song.new("/data/music/ABBA/The Best of/Chiquitita.mp3")
song.play

See more examples in the introduction lecture slides.

Next Ruby and Perl mini comparison Prev