I'm studying Ruby and try to implement method_missing
, but it's not working. For example, I want to print method name after find_
but ruby raises "undefined method 'find_hello'" when i call it on Book instance.
TEST_05.RB
module Searchable
def self.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