Sha256: 508487106226f3f6418f0c09c2b8f1e38a933ed31cb75d7970b47892705a1685

Contents?: true

Size: 782 Bytes

Versions: 1

Compression:

Stored size: 782 Bytes

Contents

# Class Attribute Example
#
require 'sync_attr'

# Sample class with lazy initialized Synchronized Class Attributes
def Person
  include SyncAttr

  # Thread safe Class Attribute reader for name
  # Sets :name only when it is first called
  # Ideal for when name is loaded after startup from a database or config file
  sync_cattr_reader :name do
    "Joe Bloggs"
  end

  # Thread safe Class Attribute reader and writer for age
  # Sets :age only when it is first called
  sync_cattr_accessor :age do
    21
  end
end

person = Person.new
puts "The person is #{person.name} with age #{person.age}"

person.age = 22
puts "The person is #{person.name} now has age #{person.age}"

person.age = Proc.new {|age| age += 1 }
puts "The person is #{person.name} now has age #{person.age}"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sync_attr-0.0.1 examples/class_attribute.rb