Class: Rust::List

Inherits:
RustDatatype show all
Defined in:
lib/rust/core/types/list.rb

Overview

Mirror of the list type in R.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RustDatatype

pull_priority, #r_hash, #r_mirror, #r_mirror_to

Constructor Details

#initialize(klass, names = []) ⇒ List

Creates an empty list of a given class (klass) and the specified names.



37
38
39
40
41
# File 'lib/rust/core/types/list.rb', line 37

def initialize(klass, names = [])
    @data = {}
    @names = names
    @klass = klass
end

Class Method Details

.can_pull?(type, klass) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/rust/core/types/list.rb', line 9

def self.can_pull?(type, klass)
    return type == "list"
end

.pull_variable(variable, type, klass) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rust/core/types/list.rb', line 13

def self.pull_variable(variable, type, klass)
    return List.new(klass) if Rust._pull("length(#{variable})") == 0
    
    names    = [Rust["names(#{variable})"]].flatten
    length   = Rust["length(#{variable})"]
    
    list = List.new(klass, names)
    for i in 0...length
        list[i] = Rust["#{variable}[[#{i + 1}]]"]
    end
    
    return list
end

Instance Method Details

#[](key) ⇒ Object Also known as: |

Returns the elements for the name key.



46
47
48
49
50
# File 'lib/rust/core/types/list.rb', line 46

def [](key)
    key = get_key(key)
    
    return @data[key]
end

#[]=(key, value) ⇒ Object

Sets the value for name key.



56
57
58
59
60
# File 'lib/rust/core/types/list.rb', line 56

def []=(key, value)
    key = get_key(key)
    
    return @data[key] = value
end

#inspectObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rust/core/types/list.rb', line 69

def inspect
    result = ""
    values_inspected = @data.map { |k, v| [k, v.inspect.split("\n").map { |l| "  " + l }.join("\n")] }.to_h
    max_length = [values_inspected.map { |k, v| v.split("\n").map { |line| line.length }.max.to_i }.max.to_i, 100].min
    
    @data.keys.each do |i|
        result << "-" * max_length + "\n"
        result << (@names[i] || "[[#{i}]]") + "\n"
        result << values_inspected[i] + "\n"
    end
    result << "-" * max_length
    
    return result
end

#load_in_r_as(variable_name) ⇒ Object



27
28
29
30
31
32
# File 'lib/rust/core/types/list.rb', line 27

def load_in_r_as(variable_name)
    Rust._eval("#{variable_name} <- list()")
    @data.each do |key, value|
        Rust["#{variable_name}[[#{key + 1}]]"] = value
    end
end

#namesObject

Returns the names of the list.



65
66
67
# File 'lib/rust/core/types/list.rb', line 65

def names
    @names
end