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

Version Path
facets-glimmer-3.2.0 lib/core/facets/module/attr_setter.rb
facets-3.1.0 lib/core/facets/module/attr_setter.rb
facets-3.0.0 lib/core/facets/module/attr_setter.rb
facets-2.9.3 lib/core/facets/module/attr_setter.rb
facets-2.9.2 src/core/facets/module/attr_setter.rb
facets-2.9.2 lib/core/facets/module/attr_setter.rb
facets-2.9.1 lib/core/facets/module/attr_setter.rb
facets-2.9.0 lib/core/facets/module/attr_setter.rb
facets-2.9.0.pre.2 lib/core/facets/module/attr_setter.rb
facets-2.9.0.pre.1 lib/core/facets/module/attr_setter.rb