Guest User

Untitled

a guest
May 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. Jukebox.rb:47:in `initialize': uninitialized constant SongList::WordIndex (NameError)
  2. from Jukebox.rb:123:in `new'
  3. from Jukebox.rb:123
  4. from Jukebox.rb:122:in `open'
  5. from Jukebox.rb:122
  6.  
  7. ## Jukebox.rb [ruby]
  8. class Song
  9. @@plays = 0
  10. def initialize(name, artist, duration)
  11. @name = name
  12. @artist = artist
  13. @duration = duration
  14. @plays = 0
  15. end
  16.  
  17. attr_reader :name, :artist, :duration
  18. attr_writer :duration
  19.  
  20. def play
  21. @plays += 1
  22. @@plays += 1
  23. "This song: #{@plays} plays. Total #{@@plays}."
  24. end
  25.  
  26. def to_s
  27. "Song: #{@name}--#{@artist} (#{@duration})"
  28. end
  29.  
  30. def duration_in_minutes
  31. @duration / 60.0 # force floating point
  32. end
  33.  
  34. def duration_in_minutes=(new_duration)
  35. @duration = (new_duration*60).to_i
  36. end
  37.  
  38. end
  39.  
  40. class KaraokeSong < Song
  41. def initialize(name, artist, duration, lyrics)
  42. super(name, artist, duration)
  43. @lyrics = lyrics
  44. end
  45.  
  46. def to_s
  47. super + " [#{@lyrics}]"
  48. end
  49. end
  50.  
  51. class SongList
  52. def initialize
  53. @songs = Array.new
  54. @index = WordIndex.new
  55. end
  56.  
  57. def append(song)
  58. @songs.push(song)
  59. @index.add_to_index(song, song.name, song.artist)
  60. self
  61. end
  62.  
  63. def lookup(word)
  64. @index.lookup(word)
  65. end
  66.  
  67. def delete_first
  68. @songs.shift
  69. end
  70.  
  71. def delete_last
  72. @songs.pop
  73. end
  74.  
  75. def [](index)
  76. @songs[index]
  77. end
  78.  
  79. def with_title(title)
  80. @songs.find {|song| title == song.name }
  81. end
  82.  
  83. MAX_TIME = 5*60 # 5 minutes
  84.  
  85. def SongList.is_too_long(song)
  86. return song.duration > MAX_TIME
  87. end
  88. end
  89.  
  90. class MyLogger
  91. private_class_method :new
  92. @@logger = nil
  93.  
  94. def MyLogger.create
  95. @@logger = new unless @@logger
  96. @@logger
  97. end
  98. end
  99.  
  100. class Accounts
  101. def initialize(checking, savings)
  102. @checking = checking
  103. @savings = savings
  104. end
  105.  
  106. private
  107. def debit(account, amount)
  108. account.balance -= amount
  109. end
  110. def credit(account, amount)
  111. account.balance += amount
  112. end
  113.  
  114. public
  115. def transfer_to_savings(amount)
  116. debit(@checking, amount)
  117. credit(@savings, amount)
  118. end
  119. end
  120.  
  121. class Account
  122. attr_reader :balance # accessor method 'balance'
  123. protected :balance
  124. def greater_balance_than(other)
  125. return @balance > other.balance
  126. end
  127. end
  128.  
  129. File.open("songdata") do |song_file|
  130. songs = SongList.new
  131. song_file.each do |line|
  132. file, length, name, title = line.chomp.split(/\s*\|\s*/)
  133. name.squeeze!(" ")
  134. mins, secs = length.scan(/\d+/)
  135. songs.append(Song.new(title, name, mins.to_i*60+secs.to_i))
  136. end
  137. puts songs[1]
  138. end
  139.  
  140. class WordIndex
  141. def initialize
  142. @index = {}
  143. end
  144.  
  145. def add_to_index(obj, *phrases)
  146. phrases.each do |phrase|
  147. phrase.scan(/\w[-\w']+/) do |word| # extract each word
  148. word.downcase!
  149. @index[word] = [] if @index[word].nil?
  150. @index[word].push(obj)
  151. end
  152. end
  153. end
  154.  
  155. def lookup(word)
  156. @index[word.downcase]
  157. end
  158. end
  159.  
  160. songs = SongList.new
  161. song_file.each do |line|
  162. file, length, name, title = line.chomp.split(/\s*\|\s*/)
  163. name.squeeze!(" ")
  164. mins, secs = length.scan(/\d+/)
  165. songs.append(Song.new(title, name, mins.to_i*60+sec.to_i))
  166. end
Add Comment
Please, Sign In to add comment