# -*- coding: utf-8 -*-

Forrás: 
Dave Thomas
Programming Ruby, 2nd ed.

class Song
  def initialize(name, artist, duration)
    @name = name
    @artist = artist
    @duration = duration
  end
end


song = Song.new("Rehab", "Amy W", 260)
song.to_s -> "#<Song:0x1c7ec4>"

class Song
  def to_s
    "Song: #@name--#@artist (#@duration)"
  end
end

song.to_s -> "Song: Rehab--Amy W (260)"

class KaraokeSong < Song
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end
end

song = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")
song.to_s -> "Song: My Way--Sinatra (225)"

class KaraokeSong
  def to_s
    super + " [#@lyrics]"
  end
end

song.to_s -> "KS: My Way--Sinatra (225) [And now, the...]"



class Song
  def name
    @name
  end
  def artist
    @artist
  end
  def duration
    @duration
  end
end

vagy

class Song
  attr_reader :name, :artist, :duration
end


song = Song.new("Rehab", "Amy W", 260)
song.artist     -> "Amy W"
song.name       -> "Rehab"
song.duration   -> 260

class Song
  def duration=(new_duration)
    @duration = new_duration
  end
end

vagy:
class Song
  attr_writer :duration
end


song = Song.new("Rehab", "Amy W", 260)
song.duration   -> 260
song.duration = 257   # set attribute with updated value
song.duration   -> 257

class Song
  @@plays = 0   # class variable
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
    @plays    = 0
  end
  def play
    @plays += 1
    @@plays += 1
    "This song: #@plays plays. Total #@@plays plays."
  end
end


s1 = Song.new("Song1", "Artist1", 234) # test songs..
s2 = Song.new("Song2", "Artist2", 345)
s1.play   -> "This song: 1 plays. Total   1 plays."
s2.play   -> "This song: 1 plays. Total   2 plays."
s1.play   -> "This song: 2 plays. Total   3 plays."
s1.play   -> "This song: 3 plays. Total   4 plays."

class SongList
  MAX_TIME = 5*60           #  5 minutes
  def SongList.is_too_long(song)      # class method
    return song.duration > MAX_TIME
  end
end

SongList.is_too_long(s1)   -> false
SongList.is_too_long(s2)   -> true

