Good effort on this! Here are my notes:
- Your runner code (which appears to be in your
parser.rbfile) does not seem to be utilizing any of yourpostclass code. It is not creating apostobject or printing anypostinfo. - There is code in your
postinitializemethod that is most likely not doing what you think it is doing, right here. This is because of how youradd_commentmethod is written (see next point). - In your
postclass you have a very interestingadd_commentmethod, which is this:
def add_comment(comment)
symbol_id = ("id" + comment.id.to_s).to_sym
@comments[symbol_id] = comment
endThere is a problem with this method: the first time this method is run, @comments will contain one key-value pair representing the added comment object, like this: @comments={:id=>#<Comment:0x007f8e9c9b9ec0 @id=nil, @username="No Username", @timestamp="No Time", @content="">} But then, when add_comments is run again, the entire hash gets overwritten by a new key-value pair, so that after the second time it runs, @comments then contains only this new key-value pair, like this: @comments={:id=>#<Comment:0x007f8e9d0d8b20 @id=nil, @username="No Username", @timestamp="No Time", @content="">} Thus each time your add_comments method runs, it is overwriting everything in your @comments hash.
Tip: You overthought your add_comments method. Here is what your add_comments method should look like:
def add_comments(comment)
@comments << comment
end- Nice work on having a
to_smethod on yourpostandcommentclasses. - Nice work having your
postinitializemethod call yourparse_commentsmethod.
###If you have time to refactor, see if you can meet these constraints:###
- Refactor your code in such a way that your runner code is simply:
post = Post.new('html-samples/hacker-news-post.html')
puts post.to_s(Note: This would require you to refactor more than your runner code!)
- Be sure that the console output from
puts post.to_sincludes yourpostobject attributes and the encapsulatedcommentobject attributes (and not just thecommentobject attributes).
Good effort! Any questions let me know.
-Phil