I would like to define a method using string elements in an array, eg:
ARRAY = ["name1", "name2"]
ARRAY.each do |k|
def k?
self.attribute == k
#or whatever else in here
end
end
so that I could do the following:
article.name1? #would return true of article.attribute == "name1"
Is something like this possible?
Obviously this is just a simple example of one thing I might do with, but what I’m really getting at here is how to assign the method name based off a string.
You need
Module#define_method. Check this out: