Sha256: 870ea3bd8771a5f26715334ba941be07eaa984b9f39af50caffb59125d378d21
Contents?: true
Size: 891 Bytes
Versions: 10
Compression:
Stored size: 891 Bytes
Contents
class Module # Create an attribute method for both getting # and setting an instance variable: # # attr_setter :a # # is equivalent to: # # def a(*args) # if args.size > 0 # @a = args[0] # self # else # @a # end # end # # CREDIT: Trans def attr_setter(*args) code, made = '', [] args.each do |a| code << %{ def #{a}(*args) args.size > 0 ? ( @#{a}=args[0] ; self ) : @#{a} end } made << "#{a}".to_sym end module_eval code made end # Alias an accessor. This create an alias for # both a reader and a writer. # # class AttrSetterExample # attr_setter :a # alias_setter :b, :a # end # # x = AttrSetterExample.new # x.b(1) # x.a #=> 1 # # CREDIT: Trans alias_method :alias_setter, :alias_method end
Version data entries
10 entries across 9 versions & 2 rubygems