ruby - Return array with class instances -


i have simple, how return array class instances ? i'm trying return array, variable return empty array.

for example :

class library      def initialize         @@books = []     end      def         @@books     end      def add_book(arg = {})         @book = book.new(arg)         @@books << @book     end  end  class book      attr_accessor :name, :year, :author, :content      def initialize( arg = {})         @name = arg[:name]         @year = arg[:year]         @author = arg[:author]         @content = arg[:content]     end  end 

@@books library class variable. using method add_book put books @books, how can return array of these instances ? sorry bad english.

thanks in advance !

when call method new create new object, ruby runs initialize method. since initialize method sets @@books empty array, of course library.new.all return empty array.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

image - ClassNotFoundException when add a prebuilt apk into system.img in android -