attr_reader :a # Generates a reader/get function:
#def a
# @a
#end
attr_writer :a # Generates a writer/set function
#def a= (value)
# @a = value
#end
attr_accessor :a # Generates Both get/set functions
Creating Custom Accessors
We need to define a function in the main Module class:
class Module
def custom_accessor(*symbols)
symbols.each do |symbol|
class_eval "
# Here we would insert the code that we want to generate.
"
end
end
end
And this is it. Now we can use our custom accessor like this:
custom_accessor :accessor
No comments:
Post a Comment