ruby - How do I check to see if an object respond_to? MyObject#attr_reader :my_property but not setter -


i writing tests ruby gem...

how check see if object respond_to? myobject#attr_reader :my_property not myobject#attr_writer :my_property or myobject#my_property(value) # <= setter?

plenty of examples checking but. ideas?

attr_reader :my_property syntactic sugar for

def my_property   @my_property end 

likewise, attr_writer :my_property shorthand for

def my_property=(val)   @my_property = val end 

note method defined attr_writer ends , equals sign; means obj.my_property = foo interpreted obj.my_property=(foo); how setters defined in ruby, while getters methods take no parameters , return value.

so, original question. if obj.respond_to? :my_property returns true, means getter set. (note getters ordinary methods, can't tell whether method getter or unrelated method) likewise if obj.respond_to? :my_property= returns true, means setter method set.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -