Is there any other ways to make it another way than with closure and define_method?
Say i have this:
def test
result=[1,2,3]
metadata=['foo', 'bar'] # for simplicity, could be fetched from database
result.define_singleton_method :headers, lambda { metadata }
result
end
I’m curious, are there other ways to embed, make static or well, “copy” metadata variable into method in Ruby?
I find it kind of iffy to be defining methods like this (probably you should have an object that looks like an array rather than making the array look like your object), but this will work as well.
You could also do something like this (it’s a little different in that it creates a setter as well).