Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few remarks:</p> <ul> <li><p>Defining a method that only raises a NotImplemented error is somewhat redundant, since Ruby will do that anyway if the method does not exist. The code you wrote there is just as useful as simply putting a comment to say "You must implement a method called compare_to". In fact that is what the Enumerable module in Ruby's standard library does - in the documentation it specifically says that in order to use the functionality in Enumerable you must define an each() method.</p></li> <li><p>a compare_to method is also redundant, since that is precisely what the &lt;=> operator is for.</p></li> <li><p>Using an actual iterator object is a bit overkill in Ruby, since blocks tend to have a much more elegant and simple approach. Same goes for your visitor pattern - you don't need to use a visitor for "extensible and re-usable traversal operations" when you can just pass a block to a traverse method. For example you have many of them in Enumerable: each, each_with_index, map, inject, select, delete_if, partition, etc. All of these use a block in a different way to provide a different type of functionality, and other functionality can be added on in a fairly simple and consistent way (especially when you have open classes).</p></li> <li><p>Regarding interfaces, in Ruby (and pretty much any other dynamic language, like Python) people usually use interfaces that are <em>implicit</em>, which means that you don't actually define the interface in code. Instead you typically rely on documentation and proper testing suites to ensure that code works well together.</p></li> </ul> <p>I think that your code may be more coherent to someone coming from a Java world because it sticks to the "Java way" of doing things. However to other Ruby programmers your code would be confusing and difficult to work with since it doesn't really stick to the "Ruby way" of doing things. For example, an implementation of a select function using an iterator object:</p> <pre><code>it = my_list.iter results = [] while it.has_next? obj = it.next results &lt;&lt; obj if some_condition? end </code></pre> <p>is much less clear to a Ruby programmer than:</p> <pre><code>results = my_list.select do |obj| some_condition? end </code></pre> <p>If you would like to see an example of a data structures library in Ruby, you can see the algorithms gem here: <a href="http://rubydoc.info/gems/algorithms/0.3.0/frames" rel="nofollow">http://rubydoc.info/gems/algorithms/0.3.0/frames</a></p> <p>Also take a look at what is provided by default in the Enumerable module: <a href="http://www.ruby-doc.org/core/classes/Enumerable.html" rel="nofollow">http://www.ruby-doc.org/core/classes/Enumerable.html</a>. When you include Enumerable you receive all of these functions for free.</p> <p>I hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload