From: Steven Davidovitz Date: 2006-11-16T15:12:47+09:00 Subject: Re: basic mistake? On Thu, 16 Nov 2006 14:21:21 +0900 Hal Fulton wrote: > Nia wrote: > > -------------------------------------------------------- > > class BookStore > > @bookstore=Array.new > > > > def initialize(*books) > > i=0 > > books.each do |x| > > #@bookstore.push(x) > > puts "(#{i})" + x.inspect > > i+=1 > > end > > end > > end > > > > class Book > > def initialize(name,cost) > > @book_name=name > > @book_cost=cost > > end > > > > def detail > > puts "Book: #{@book_name} ($#{@book_cost})" > > end > > end > > > > b1=Book.new('Harry Potter','9.95') > > b2=Book.new('The Raven','8.65') > > store = BookStore.new(b1,b2) > > -------------------------------------------------------------- > > this bit of code seems to work fine, but when i try to assign an object to my bookstore > > array, i'm given an error(i commented it out since it wasn't working). wierd part is that this > > seem to work fine outside of a class. am i missing something?, like a declaration? this isn't > > the first time i've encounter this problem. please help me solve it. > > > > At least two mistakes here... move the @bookstore line > down into the initialize method. And take off the # > from that line -- that makes it a comment. > > Hal > > > You can also consolidate part of the initialize method like so: def initialize(*books) @bookstore=Array.new books.each_with_index do |x,i| @bookstore.push(x) puts "(#{i})" + x.inspect end end