This is really good stuff. I am going to get into nitty gritty to give you some items you could tweak:
- Your
Parsablemodule is statically referencing two instance variables that are being created outside this module:@nokogiri_documentand@file. That is a no-no. You want your module to be written in such a way that it needs to know nothing about the rest of your code. Thus, a better way would be to pass the nokogiri document and the file to the module as arguments. As in other words, yourParsablemodule should be written in such a way that, if the names of these two instance variables were changed, there would be no need to change any of the code in yourParsablemodule. - The
map_with_indexon line 5 in yourParsablemodule is sweet. Nice work iterating over the DOM like this and delivering an array ofcommentobjects to yourpostinitialize method. - Heads up: Your
to_smethod on yourpostclass only prints the state of thecommentobjects encapusulated in yourpostobject; it doesn't print the rest of the state of yourpostobject. You should add that to theto_smethod so it prints thetitle,url, etc. of yourpostobject. - It is really cool that you utilized colors!
- Also the big sign that your code is tight is that your
runner.rbfile is so short. Nice work.
##Challenge for you:##
Refactor your code in such a way that these two conditions are met:
- This runner code would work:
post = Post.new({file:'html-samples/hacker-news-post.html'})
puts post- The only instance variables referenced in your code are from the same class they are being referenced in. For example, in your
postinitializemethod, this line would be cool because it is referencing an instance variable that is also created by yourpostclass (the@html_nodeinstance variable):
@body_node = @html_node.children.lastbut this line would need to be refactored because it is currently referencing a variable created outside of your post class (the @nokogiri_document instance variable:
@html_node = @nokogiri_document.children.lastYou can do it!
Any questions let me know.
-Phil