Class: BatchExperiment::Comm2FnameConverter
- Inherits:
-
Object
- Object
- BatchExperiment::Comm2FnameConverter
- Defined in:
- lib/batch_experiment.rb
Overview
Converts a command to a filename using a given sanitizer, gives different names to different calls with the same arguments. Example: if a call with “sleep 1” yields “sleep_1”, the second call with the same argument yields “sleep_1.2”, and so on. Note that this is done by remembering previous calls, the object don’t inspect the filesystem to check if that name was or wasn’t used.
Instance Method Summary collapse
-
#call(comm) ⇒ String
Takes a command, creates a fname for it, if this fname was already seen before, returns the fname + “.N”, where N is the number of times fname was already seen.
-
#initialize(sanitizer = FnameSanitizer) ⇒ Comm2FnameConverter
constructor
Creates a new Comm2FnameConverter, with no memory of any previous calls.
-
#initialize_clone(old) ⇒ Object
Used to guarantee that a clone of Comm2FnameConverter will not share relevant state with the original.
Constructor Details
#initialize(sanitizer = FnameSanitizer) ⇒ Comm2FnameConverter
Creates a new Comm2FnameConverter, with no memory of any previous calls.
46 47 48 49 |
# File 'lib/batch_experiment.rb', line 46 def initialize(sanitizer = FnameSanitizer) @num_times_seen = {} @sanitizer = sanitizer end |
Instance Method Details
#call(comm) ⇒ String
Note that different arguments can be reduced to the same sanitized filename and, if this happens, they will NOT overwrite each other. Example: ‘echo “abc”’ -> ‘echo_abc’; ‘echo abc’ -> ‘echo_abc.2’.
Takes a command, creates a fname for it, if this fname was already seen before, returns the fname + “.N”, where N is the number of times fname was already seen.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/batch_experiment.rb', line 61 def call(comm) fname = @sanitizer.call(comm) if @num_times_seen.include? fname @num_times_seen[fname] += 1 fname << ".#{@num_times_seen[fname]}" else @num_times_seen[fname] = 1 end fname.clone end |
#initialize_clone(old) ⇒ Object
Used to guarantee that a clone of Comm2FnameConverter will not share relevant state with the original. So calls to #call on a clone don’t affect the state of original (and vice versa).
76 77 78 |
# File 'lib/batch_experiment.rb', line 76 def initialize_clone(old) @num_times_seen = old.num_times_seen.clone end |