Witam ma pewnej problem z kodem z książki tak więc
[quote]class Song
def initialize(name,artist,duration)
def name @name
end
def artist @artist
end
def duration @duration
end
end
def to_s
“Utwór : #@name–#@artysta (#@duration)”
end
end
class WordIndex
def initialize @index = {}
end
def add_to_index(obj, *ph)
ph.each do |phr|
phr.scan(/\w[-\w’]+/) do |word|
word.downcase @index[word] = [] if @index[word].nil? @index[word].push(obj)
end
end
end
def lookup(word) @index[word.downcase]
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 delete_first @songs.shift
end
def delete_last @songs.pop
end
def @songs[index]
end
def lookup(word) @index.lookup(word)
end
end
song = SongList.new
File.open(“ddd.txt”) 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.lookup[“h”]
end[/quote]
plik ddd.txt wygląda tak
[quote]ceder@ceder:~/Desktop/ruby$ ruby ruby1.rb
ruby1.rb:25:in add_to_index': private methodscan’ called for nil:NilClass (NoMethodError)
from ruby1.rb:24:in each' from ruby1.rb:24:inadd_to_index’
from ruby1.rb:44:in append' from ruby1.rb:69 from ruby1.rb:65:ineach’
from ruby1.rb:65
from ruby1.rb:63:in `open’
from ruby1.rb:63[/quote]
i na koniec pytanie co w tym kodzie jest nie tak ,że kod nie chcę się wykonać
[code]class Song
attr_accessor :name, :artist, :duration
def initialize(name,artist,duration) @name =name @artist = artist
@duration=duration
end
def to_s
“Utwór : #@name–#@artist (#@duration)”
end
end
class WordIndex
def initialize @index = {}
end
def add_to_index(obj, *ph)
ph.each do |phr|
phr.scan(/\w[-\w’]+/) do |word|
word.downcase @index[word] = [] if @index[word].nil? @index[word].push(obj)
end
end
end
def lookup @index.inspect
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 delete_first @songs.shift
end
def delete_last @songs.pop
end
def @songs[index]
end
def lookup @index.lookup
end
end
song = SongList.new
song_file = File.open(“ddd.txt”)
song_file.each do |line|
file , length , name , title = line.chomp.split(/\s*|\s*/)
name.squeeze!()
mins ,secs = length.scan(/\d+/)
song.append(Song.new(title,name,mins.to_i * 60 + secs.to_i))
end
puts song.lookup[/code]
nie do konca to to chciales, ale powinno Ci pomoc
IRB! i lecisz po kolej jak byk pisze ze w 25 linii wywolujesz prywatna metode scan dla pustego obiektu. Pusty?? czemu powinno paść pytanie, więc lecisz w gore i sprawdzasz czemu masz pusto.
jak mowilem odpal irb i lec po kolei,
a czemu klepiesz z ksiazki dokladnie i nie dziala?
Autor książki pisze ,że używał ruby w wersji 1.8.2 (2004-12-30) a ja ma ruby 1.8.7 mam nadzieje ,że jakiś wielkich zmian nie ma ? a co do kodu okazało się ,że nie inicjalizowałem zmiennych obiektu klasy Song, poprawiony kod
[quote=Kod]class Song
attr_reader :name, :artist, :duration
def initialize(name,artist,duration) @name =name @artist = artist
@duration=duration
end
def to_s
“Utwór : #@name–#@artist (#@duration)”
end
end
class WordIndex
def initialize @index = {}
end
def add_to_index(obj, *ph)
ph.each do |phr|
phr.scan(/\w[-\w’]+/) do |word|
word.downcase @index[word] = [] if @index[word].nil? @index[word].push(obj)
end
end
end
def lookup(word) @index[word.downcase]
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 delete_first @songs.shift
end
def delete_last @songs.pop
end
def @songs[index]
end
def lookup(word) @index.lookup(word)
end
end
File.open(“ddd.txt”) 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