You are calling method on object
which looks for instance_level
method. So you need to define instance_level method_missing
method:
module Searchable
def method_missing(m, *args)
method = m.to_s
if method.start_with?("find_")
attr = method[5..-1]
puts attr
else
super
end
end
end
class Book
include Searchable
BOOKS = []
attr_accessor :author, :title, :year
def initialize(name = "Undefined", author = "Undefined", year = 1970)
@name = name
@author = author
@year = year
end
end
book = Book.new
book.find_hello #=> hello
When you use self
with method definition. It is defined as class level
method. In your case Book.find_hello
will output hello
.