Sha256: 7c815f4e1949fc7253907e16c39f63a3a8562190fc4e738490816958d772c8b7

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# Class Attribute Example
#
require 'sync_attr'

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

  # Thread safe Instance 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_attr_reader :name do
    "Joe Bloggs"
  end

  # Thread safe Instance Attribute reader and writer for age
  # Sets :age only when it is first called
  sync_attr_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}"

# Changes to person above do not affect any changes to second_person
# Also, the initial value that is lazy loaded into name is unaffected by person above
second_person = Person.new
puts "The second person is #{second_person.name} with age #{second_person.age}"

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sync_attr-1.0.0 examples/instance_attribute.rb
sync_attr-0.1.1 examples/instance_attribute.rb
sync_attr-0.1.0 examples/instance_attribute.rb