Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. # This is an actual blog script, built by Ruby. Gotta use class variables, class methods, loops, make your own methods, make your own objects, etc. etc. etc. This ain't no WordPress, by the way.
  2.  
  3. class Blog
  4.  
  5. @@all_posts = []
  6. @@num_posts = 0
  7.  
  8. def self.all
  9. @@all_posts
  10. end
  11.  
  12. def self.add(item)
  13. @@all_posts << item
  14. @@num_posts += 1
  15. end
  16.  
  17. def self.postit
  18. @@all_posts.each do |post|
  19. puts "Title:\n #{post.title}"
  20. puts "Content:\n #{post.content}"
  21. puts "Date Published:\n #{post.date_created}"
  22. end
  23. end
  24.  
  25. end
  26.  
  27. class Post < Blog
  28. def self.build
  29. post = new
  30. puts "Give your new blog post a title:"
  31. post.title = gets.chomp
  32. puts "Start writing your content:"
  33. post.content = gets.chomp
  34. post.date_created = Time.now
  35. post.save
  36. puts "Write another post? Y or N?"
  37. create if gets.chomp.downcase == 'y'
  38. end
  39.  
  40. def title
  41. @title
  42. end
  43.  
  44. def title=(title)
  45. @title = title
  46. end
  47.  
  48. def date_created
  49. @date_created
  50. end
  51.  
  52. def date_created=(date_created)
  53. @date_created = date_created
  54. end
  55.  
  56. def content
  57. @content
  58. end
  59.  
  60. def content=(content)
  61. @content = content
  62. end
  63.  
  64. def save
  65. Post.add(self)
  66. end
  67.  
  68. end
  69.  
  70. Post.build
  71. all_posts = Post.all
  72. puts all_posts.inspect
  73. Post.postit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement