I have code like this:
class Foo
# (method definitions)
def make_hash
{
some_method: some_method,
some_other_method: some_other_method
}
end
end
How could I simplify, or DRY, make_hash? I want something like slice or Rails’ attributes.slice, but applying to methods of a regular class.
One way would be to create the hash with a default value block:
So, each time you request the hash’s key, it will dynamically load the instance_method without having to front load it all. The instance_method method returns objects, so you might want to
.to_sor.to_symto suit your needs.I’m intrigued by the question though and would be interested in knowing what your end goal is with this method.