我有以下必须通过的测试:
def test_can_find_by_arbitrary_fields
assert @library.respond_to? :find_by_artist
assert !@library.respond_to?(:find_by_bitrate)
@library.add_song({ :artist => 'Green Day',
:name => 'American Idiot',
:bitrate => 192 })
assert @library.respond_to?(:find_by_bitrate)
end
而且我不确定我该怎么做。
我试着做:
def respond_to?(method)
if self.public_methods.include? method
true
elsif (method == :find_by_bitrate)
define_method :find_by_bitrate, ->(default = nrb) { @songs.select |a| a[:bitrate] == nrb }
false
else
false
end
但它说“define_method 未定义”。有什么方法可以定义find_by_bitrate
方法吗?