I have a hash value containing a variable:
module Categories
module Services
PHOTOGRAPHY_VIDEO = "9115"
end
end
hash = {"CatId" => Categories::Services::PHOTOGRAPHY_VIDEO}
puts(hash["CatId"]) # This returns "9115" but how do I get the "Services" category?
I want to ask Ruby for the namespace of the variable ‘value’ of ‘CatId’ equal to ‘Services’. I want to do something like this:
def priced?
(hash["CatId"]).unknown_method == Services
end
How do I obtain its namespace?
I don’t think this is possible. Given a value such as ‘9115’ there’s no way (other than an exhaustive search, which would only be practical if there were a small number total) of knowing what constants have been set to a particular video.
If you need to get at this information, I’d rethink what data you store – the value for the CatId key could be something with more information in it (eg a struct containing both the id and a textual description)