Your code works! Good job! Here are some notes:
- Line 3 in your
comment.rbfile sets two attribute readers:attr_reader :comment_user_ID, :comment_text. These attribute names are redundant since they are attributes on yourcommentclass. Removing the word 'comment' from the attribute names and simply naming themattr_reader :user_ID, :textwould suffice. - Lines 12-14 in your
postclass can be refactored. Instead of this:
def comments
@collection_of_comments
endyou could eliminate the need to write this method by naming your collection @comments and create an attr_reader for it: attr_reader :comments
- Heads up: You have a
displaymethod in yourpostclass that controls what it printed to the console. This does not belong in yourpostclass code, but should rather be in your runner code. (Yourrunner.rbfile is currently functioning as your controller. Soon you will learn the MVC pattern and will need to keep these functions separate.) Hint: In order to move your display code to your runner file, you would need to refactor yourpostclass in order to access yourpostobject from the runner. Another hint: utilizeattr_readeron yourpostclass as you did on yourcommentclass. - To make your runner code much easier to read and follow (which is very important!), what if you created your
commentobjects first, then put them all into an array, then create yourpostobject and pass the comments array to your Post.new as an argument. As in other words, have all of yourcommentcreating code first, resulting in a comments array, then have all of yourpostcreating code (with the comments array being one of the args passed to Post.new), then print the post to console. That would be much easier to follow. - Be sure to delete any unused code (at the end of your
post.rbfile, and one line in yourrunner.rbfile)
Good work! You figured out how to traverse the DOM, scrape the html doc, and puts all of the desired info to the console. Well done!