Sha256: c367d44e3b3354012463dd4a45dff5fb9a64f63751091c0a77d9e4aeea18d2a8

Contents?: true

Size: 544 Bytes

Versions: 10

Compression:

Stored size: 544 Bytes

Contents

module MissileEmitter
  module Attributes
    MissileEmitter do |_, field, default, *, &block|
      attribute = "@#{field}" # 构造实例变量名称,如:name => @name
      # 动态定义实例⽅法
      define_method field do
        if instance_variable_defined?(attribute)
          instance_variable_get attribute
        else
          #返回声明的默认值,同样考虑传递Proc的情况
          block ? instance_eval(&block) : default
        end
      end
      # 写值⽅法
      attr_writer field
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
missile_emitter-0.4.10 lib/missile_emitter/attributes.rb
missile_emitter-0.4.01 lib/missile_emitter/attributes.rb
missile_emitter-0.3.20 lib/missile_emitter/attributes.rb
missile_emitter-0.3.19 lib/missile_emitter/attributes.rb
missile_emitter-0.3.18 lib/missile_emitter/attributes.rb
missile_emitter-0.3.17 lib/missile_emitter/attributes.rb
missile_emitter-0.3.16 lib/missile_emitter/attributes.rb
missile_emitter-0.3.14 lib/missile_emitter/attributes.rb
missile_emitter-0.3.13 lib/missile_emitter/attributes.rb
missile_emitter-0.3.12 lib/missile_emitter/attributes.rb