Module: Rust

Defined in:
lib/rust-core.rb

Defined Under Namespace

Modules: Descriptive, EffectSize, RBindings, StatisticalTests Classes: CSV, DataFrame, Matrix, RustDatatype

Constant Summary collapse

CLIENT_MUTEX =
Mutex.new
R_MUTEX =
Mutex.new
R_ENGINE =
RinRuby.new(echo: false)
@@in_client_mutex =
false

Class Method Summary collapse

Class Method Details

.[](variable, type = RustDatatype) ⇒ Object



35
36
37
# File 'lib/rust-core.rb', line 35

def self.[](variable, type=RustDatatype)
    return type.pull_variable(variable)
end

.[]=(variable, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rust-core.rb', line 24

def self.[]=(variable, value)
    if value.is_a?(RustDatatype)
        value.load_in_r_as(variable.to_s)
    elsif value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(Array)
        R_ENGINE.assign(variable, value)
    else
        raise "Given #{variable.class}, expected RustDatatype, String, Numeric, or Array"
    end
    
end

._eval(r_command, return_warnings = false) ⇒ Object



64
65
66
# File 'lib/rust-core.rb', line 64

def self._eval(r_command, return_warnings = false)
    self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.eval(cmd) }
end

._eval_big(r_command, return_warnings = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rust-core.rb', line 39

def self._eval_big(r_command, return_warnings = false)
    r_command = r_command.join("\n") if r_command.is_a?(Array)
    
    self._rexec(r_command, return_warnings) do |cmd|
        result = true
        instructions = cmd.lines
        
        while instructions.size > 0
            current_command = ""
            
            while (instructions.size > 0) && (current_command.length + instructions.first.length < 10000)
                current_command << instructions.shift
            end
            
            result &= R_ENGINE.eval(current_command)
        end
        
        result
    end
end

._pull(r_command, return_warnings = false) ⇒ Object



60
61
62
# File 'lib/rust-core.rb', line 60

def self._pull(r_command, return_warnings = false)
    self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.pull(cmd) }
end

._rexec(r_command, return_warnings = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rust-core.rb', line 68

def self._rexec(r_command, return_warnings = false)
    R_MUTEX.synchronize do
        assert("This command must be executed in an exclusive block") { @@in_client_mutex }
        
        result = nil
        begin
            $stdout = StringIO.new
            if return_warnings
                R_ENGINE.echo(true, true)
            else
                R_ENGINE.echo(false, false)
            end
            result = yield(r_command)
        ensure
            R_ENGINE.echo(false, false)
            warnings = $stdout.string
            $stdout = STDOUT
        end
        
        if return_warnings
            return result, warnings.lines.map { |w| w.strip.chomp }
        else
            return result
        end
    end
end

.exclusiveObject



14
15
16
17
18
19
20
21
22
# File 'lib/rust-core.rb', line 14

def self.exclusive
    result = nil
    CLIENT_MUTEX.synchronize do
        @@in_client_mutex = true
        result = yield
        @@in_client_mutex = false
    end
    return result
end