Class: RRandom::Loader
- Inherits:
-
Object
- Object
- RRandom::Loader
- Defined in:
- lib/rrandom.rb
Instance Method Summary collapse
- #get(name) ⇒ Object
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
- #load_list(name) ⇒ Object
Constructor Details
#initialize ⇒ Loader
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.("rrandom/source/#{name}.txt", __dir__)).chomp.lines end |