Your code works! Good job! Here are some notes:
- Line 3 in your
comment.rb
file sets two attribute readers:attr_reader :comment_user_ID, :comment_text
. These attribute names are redundant since they are attributes on yourcomment
class. Removing the word 'comment' from the attribute names and simply naming themattr_reader :user_ID, :text
would suffice. - Lines 12-14 in your
post
class can be refactored. Instead of this:
def comments
@collection_of_comments
end
you 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
display
method in yourpost
class that controls what it printed to the console. This does not belong in yourpost
class code, but should rather be in your runner code. (Yourrunner.rb
file 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 yourpost
class in order to access yourpost
object from the runner. Another hint: utilizeattr_reader
on yourpost
class as you did on yourcomment
class. - To make your runner code much easier to read and follow (which is very important!), what if you created your
comment
objects first, then put them all into an array, then create yourpost
object and pass the comments array to your Post.new as an argument. As in other words, have all of yourcomment
creating code first, resulting in a comments array, then have all of yourpost
creating 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.rb
file, and one line in yourrunner.rb
file)
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!