Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a lot of info missing to properly answer this. The test implies that <code>find_by_artist</code> is always defined even when <code>@library</code> is empty, but that there are dynamic methods available on other attributes (eg: bitrate) that are valid only when library contains a record with such a method.</p> <p>One should not redefine <code>respond_to?</code> in any case. There is an explicit hook method for answering respond_to? for dynamic methods: <a href="http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to_missing-3F" rel="nofollow"><code>Object#respond_to_missing?</code></a>.</p> <p>So a simple way to make your test pass is to be sure the @library object has a concrete method <code>#find_by_artist</code> and a respond to hook that checks whether any of it's elements a have the requested attribute. If I assume @library is a collection object <code>Library</code> which keeps an enumeration of songs in <code>@songs</code></p> <pre><code>class Library def find_by_artist artist @songs.select { |song| song['artist'] == artist } end def method_missing meth, arg m = /^find_by_(.+)$/.match meth.to_s return super unless attr = m &amp;&amp; m[1] @songs.select { |song| song[attr] == arg } end def respond_to_missing? meth, include_private m = /^find_by_(.+)$/.match meth.to_s return super unless attr = m &amp;&amp; m[1] @songs.any? { |song| song.has_key? attr } end end </code></pre> <p>This has a performance problem in that respond_to? now incurs a search of all the songs. One could optimize by keeping a set of the union of all attributes contained in @songs and updating it in methods which add/update/delete elements in the collection.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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