Good effort on this! Here are my notes:
- Your runner code (which appears to be in your
parser.rb
file) does not seem to be utilizing any of yourpost
class code. It is not creating apost
object or printing anypost
info. - There is code in your
post
initialize
method that is most likely not doing what you think it is doing, right here. This is because of how youradd_comment
method is written (see next point). - In your
post
class you have a very interestingadd_comment
method, which is this:
def add_comment(comment)
symbol_id = ("id" + comment.id.to_s).to_sym
@comments[symbol_id] = comment
end
There 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_s
method on yourpost
andcomment
classes. - Nice work having your
post
initialize
method call yourparse_comments
method.
###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_s
includes yourpost
object attributes and the encapsulatedcomment
object attributes (and not just thecomment
object attributes).
Good effort! Any questions let me know.
-Phil