Class: RRandom::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/rrandom.rb

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



6
7
8
# File 'lib/rrandom.rb', line 6

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#get(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rrandom.rb', line 10

def get(name)
  ivar_name = :"@_list_#{name}"

  return instance_variable_get(ivar_name) if instance_variable_get(ivar_name)

  @mutex.synchronize do
    # do it again, in case of race condition
    return instance_variable_get(ivar_name) if instance_variable_get(ivar_name)

    list = load_list(name)
    instance_variable_set(ivar_name, list)
    list
  end
end

#load_list(name) ⇒ Object



25
26
27
# File 'lib/rrandom.rb', line 25

def load_list(name)
  File.read(File.expand_path("rrandom/source/#{name}.txt", __dir__)).chomp.lines
end