attr_reader creates these accessor methods for you.
class Song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
def name
@name
end
def artist
@artist
end
def duration
@duration
end
end
song = Song.new("A", "B", 2)
puts song.artist
puts song.name
puts song.duration
class Song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
attr_reader :name, :artist, :duration
end
song = Song.new("A", "B", 6)
puts song.artist
puts song.name
puts song.duration
Related examples in the same category