Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Jukebox.rb:47:in `initialize': uninitialized constant SongList::WordIndex (NameError)
- from Jukebox.rb:123:in `new'
- from Jukebox.rb:123
- from Jukebox.rb:122:in `open'
- from Jukebox.rb:122
- ## Jukebox.rb [ruby]
- class Song
- @@plays = 0
- def initialize(name, artist, duration)
- @name = name
- @artist = artist
- @duration = duration
- @plays = 0
- end
- attr_reader :name, :artist, :duration
- attr_writer :duration
- def play
- @plays += 1
- @@plays += 1
- "This song: #{@plays} plays. Total #{@@plays}."
- end
- def to_s
- "Song: #{@name}--#{@artist} (#{@duration})"
- end
- def duration_in_minutes
- @duration / 60.0 # force floating point
- end
- def duration_in_minutes=(new_duration)
- @duration = (new_duration*60).to_i
- end
- end
- class KaraokeSong < Song
- def initialize(name, artist, duration, lyrics)
- super(name, artist, duration)
- @lyrics = lyrics
- end
- def to_s
- super + " [#{@lyrics}]"
- end
- end
- class SongList
- def initialize
- @songs = Array.new
- @index = WordIndex.new
- end
- def append(song)
- @songs.push(song)
- @index.add_to_index(song, song.name, song.artist)
- self
- end
- def lookup(word)
- @index.lookup(word)
- end
- def delete_first
- @songs.shift
- end
- def delete_last
- @songs.pop
- end
- def [](index)
- @songs[index]
- end
- def with_title(title)
- @songs.find {|song| title == song.name }
- end
- MAX_TIME = 5*60 # 5 minutes
- def SongList.is_too_long(song)
- return song.duration > MAX_TIME
- end
- end
- class MyLogger
- private_class_method :new
- @@logger = nil
- def MyLogger.create
- @@logger = new unless @@logger
- @@logger
- end
- end
- class Accounts
- def initialize(checking, savings)
- @checking = checking
- @savings = savings
- end
- private
- def debit(account, amount)
- account.balance -= amount
- end
- def credit(account, amount)
- account.balance += amount
- end
- public
- def transfer_to_savings(amount)
- debit(@checking, amount)
- credit(@savings, amount)
- end
- end
- class Account
- attr_reader :balance # accessor method 'balance'
- protected :balance
- def greater_balance_than(other)
- return @balance > other.balance
- end
- end
- File.open("songdata") do |song_file|
- songs = SongList.new
- song_file.each do |line|
- file, length, name, title = line.chomp.split(/\s*\|\s*/)
- name.squeeze!(" ")
- mins, secs = length.scan(/\d+/)
- songs.append(Song.new(title, name, mins.to_i*60+secs.to_i))
- end
- puts songs[1]
- end
- class WordIndex
- def initialize
- @index = {}
- end
- def add_to_index(obj, *phrases)
- phrases.each do |phrase|
- phrase.scan(/\w[-\w']+/) do |word| # extract each word
- word.downcase!
- @index[word] = [] if @index[word].nil?
- @index[word].push(obj)
- end
- end
- end
- def lookup(word)
- @index[word.downcase]
- end
- end
- songs = SongList.new
- song_file.each do |line|
- file, length, name, title = line.chomp.split(/\s*\|\s*/)
- name.squeeze!(" ")
- mins, secs = length.scan(/\d+/)
- songs.append(Song.new(title, name, mins.to_i*60+sec.to_i))
- end
Add Comment
Please, Sign In to add comment