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
Constructor Details
#initialize(sanitizer = FnameSanitizer) ⇒ Comm2FnameConverter
Creates a new Comm2FnameConverter, with no memory of any previous calls.
40 41 42 43 |
# File 'lib/batch_experiment.rb', line 40 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.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/batch_experiment.rb', line 55 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
67 68 69 |
# File 'lib/batch_experiment.rb', line 67 def initialize_clone(old) @num_times_seen = old.num_times_seen.clone end |