Skip to content

Instantly share code, notes, and snippets.

@adriculous
Last active July 27, 2016 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriculous/8cc9928e56f5a7631b7022ef757ba6f1 to your computer and use it in GitHub Desktop.
Save adriculous/8cc9928e56f5a7631b7022ef757ba6f1 to your computer and use it in GitHub Desktop.
(Hella) simple blog script built in Ruby
# 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.
class Blog
@@all_posts = []
@@num_posts = 0
def self.all
@@all_posts
end
def self.add(item)
@@all_posts << item
@@num_posts += 1
end
def self.postit
@@all_posts.each do |post|
puts "Title:\n #{post.title}"
puts "Content:\n #{post.content}"
puts "Date Published:\n #{post.date_created}"
end
end
end
class Post < Blog
def self.build
post = new
puts "Give your new blog post a title:"
post.title = gets.chomp
puts "Start writing your content:"
post.content = gets.chomp
post.date_created = Time.now
post.save
puts "Write another post? Y or N?"
build if gets.chomp.downcase == 'y'
end
def title
@title
end
def title=(title)
@title = title
end
def date_created
@date_created
end
def date_created=(date_created)
@date_created = date_created
end
def content
@content
end
def content=(content)
@content = content
end
def save
Post.add(self)
end
end
Post.build
all_posts = Post.all
puts all_posts.inspect
Post.postit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment