Class: Rserve::REXP::Logical

Inherits:
Vector show all
Defined in:
lib/rserve/rexp/logical.rb

Constant Summary collapse

NA_internal =
-2147483648;
NA =
-128
TRUE =
1
FALSE =
0

Constants inherited from Rserve::REXP

MaxDebugItems, MismatchError

Instance Attribute Summary collapse

Attributes inherited from Rserve::REXP

#attr

Instance Method Summary collapse

Methods inherited from Vector

#==, #vector?

Methods inherited from Rserve::REXP

#as_double, #as_double_matrix, #as_factor, #as_float, #as_floats, #as_integer, #as_list, #as_matrix, #as_nested_array, #as_string, #complex?, create_data_frame, #dim, #environment?, #expression?, #factor?, #get_attribute, #has_attribute?, #inherits?, #integer?, #language?, #list?, #null?, #numeric?, #pair_list?, #raw?, #recursive?, #reference?, #split_array, #string?, #symbol?, #to_debug_string, #to_f, #to_i, #to_ruby, #vector?

Constructor Details

#initialize(l, attr = nil) ⇒ Logical

l should be a value or array of 0 and 1.



17
18
19
20
21
22
23
24
# File 'lib/rserve/rexp/logical.rb', line 17

def initialize(l,attr=nil)
  super(attr)
  if l.is_a? Array
    @payload= l
  else
    @payload = [ l==NA ? NA : (l==1 ? TRUE : FALSE) ]
  end
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



4
5
6
# File 'lib/rserve/rexp/logical.rb', line 4

def payload
  @payload
end

Instance Method Details

#as_bytesObject



31
32
33
# File 'lib/rserve/rexp/logical.rb', line 31

def as_bytes
  @payload
end

#as_doublesObject



50
51
52
# File 'lib/rserve/rexp/logical.rb', line 50

def as_doubles
  @payload.map {|v| v==NA ? REXP::Double::NA : ( v == FALSE ? 0.0 : 1.0 )}
end

#as_integersObject



47
48
49
# File 'lib/rserve/rexp/logical.rb', line 47

def as_integers
  @payload.map {|v| v==NA ? REXP::Integer::NA : ( v == FALSE ? 0 : 1 )}
end

#as_stringsObject



53
54
55
# File 'lib/rserve/rexp/logical.rb', line 53

def as_strings
  @payload.map {|v| v==NA ? REXP::Double::NA : ( v == FALSE ? "FALSE" : "TRUE" )}
end

#false?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rserve/rexp/logical.rb', line 59

def false?
  @payload.map {|v| v==FALSE}
end

#lengthObject



25
26
27
# File 'lib/rserve/rexp/logical.rb', line 25

def length
  @payload.length
end

#logical?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rserve/rexp/logical.rb', line 28

def logical?
  true
end

#na?(value = nil) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/rserve/rexp/logical.rb', line 9

def na?(value=nil)
  if value
    value==NA
  else
    @payload.map {|v| v==NA}
  end
end

#to_aObject

Retrieves values as Ruby array of true and false values NA will be replaced with nils



37
38
39
40
41
42
43
44
45
# File 'lib/rserve/rexp/logical.rb', line 37

def to_a
  @payload.map {|v|
    case v
    when NA then nil
    when TRUE then true
    when FALSE then false
    end
  }
end

#to_ruby_internalObject



63
64
65
66
67
68
69
70
71
# File 'lib/rserve/rexp/logical.rb', line 63

def to_ruby_internal
  if @payload.nil? or @payload.size==0
    nil
  elsif @payload.size==1
    @payload[0]==1 ? true : false
  else
    @payload.map {|v| na?(v) ? nil : (v==1 ? true : false)}
  end
end

#true?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rserve/rexp/logical.rb', line 56

def true?
  @payload.map {|v| v==TRUE}
end