fork download
  1. class Book
  2. attr_reader :id, :title
  3. def initialize(id, title)
  4. @id = id
  5. @title = title
  6. end
  7. end
  8.  
  9. bookShelf = []
  10.  
  11. bookShelf <<
  12. Book.new(1, "English") <<
  13. Book.new(2, "Math") <<
  14. Book.new(3, "History") <<
  15. Book.new(4, "Physics") <<
  16. Book.new(5, "Programming") <<
  17. Book.new(6, "Science")
  18.  
  19. print "------- Order -------\n"
  20.  
  21. bookShelf.each{ | book | print "id: #{book.id} title: #{book.title}\n" }
  22.  
  23. print "------- Reverse Order -------\n"
  24.  
  25. bookShelf.reverse_each{ | book | print "id: #{book.id} title: #{book.title}\n" }
Success #stdin #stdout 0.05s 9656KB
stdin
Standard input is empty
stdout
------- Order -------
id: 1 title: English
id: 2 title: Math
id: 3 title: History
id: 4 title: Physics
id: 5 title: Programming
id: 6 title: Science
------- Reverse Order -------
id: 6 title: Science
id: 5 title: Programming
id: 4 title: Physics
id: 3 title: History
id: 2 title: Math
id: 1 title: English