ruby - How can I tell if a method is read-write, read-only, or write-only, via introspection? -
i coming java, , want know if can 'set' instance variable object using introspection.
for example, if have following class declaration, 2 instance variables, first_attribute
, second_attribute
:
class someclass attr_accessor :first_attribute attr_reader :second_attribute def initialize() # ... end end
i want able instance methods, presumably calling someclass.instance_methods
, know of instance methods read/write vs. read-only.
in java can by:
propertydescriptor[] properties = propertyutils.getpropertydescriptors(someclass); (prop : properties) { if (prop.getwritemethod() != null) { // can set one! } }
how do in ruby?
there's not built-in java property stuff, can pretty this:
self.class.instance_methods.grep(/\w=$/)
which return names of setter methods on class.
Comments
Post a Comment