Module: Rserve::With2DNames

Defined in:
lib/rserve/with2dnames.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#column_namesObject (readonly)

Returns the value of attribute column_names


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

def column_names
  @column_names
end

#row_namesObject (readonly)

Returns the value of attribute row_names


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

def row_names
  @row_names
end

Instance Method Details

#by_name(row_name, column_name) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rserve/with2dnames.rb', line 22

def by_name(row_name, column_name)
  return nil unless @row_names and @column_names
  i = @row_names.index(row_name)
  j = @column_names.index(column_name)
  return nil unless i and j
  if self.is_a? Array
    at_2d(i, j)
  elsif self.is_a? Matrix
    self[i, j]
  else
    raise ArgumentError, "unsupported type for With2DNames"
  end
end

#column_by_name(name) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rserve/with2dnames.rb', line 48

def column_by_name(name)
  return nil unless @column_names
  j = @column_names.index(name)
  return nil unless j
  result = column(j).to_a
  if @row_names
    result.extend WithNames
    result.names = @row_names
  end
  result
end

#named_2d?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'lib/rserve/with2dnames.rb', line 60

def named_2d?
  !(@row_names.nil? or @column_names.nil?)
end

#names=(names) ⇒ Object

Raises:

  • (ArgumentError)

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rserve/with2dnames.rb', line 6

def names=(names)
  raise ArgumentError, "sizes must be of size 2" unless (names.size == 2)
  if self.is_a? Array
    raise ArgumentError, "mismatch between the size of row labels and the actual number of elements" if (names[0] and ((self.count % names[0].size) != 0))
    raise ArgumentError, "mismatch between the size of column labels and the actual number of elements" if (names[1] and ((self.count % names[1].size) != 0))
    raise ArgumentError, "mismatch between the provided sizes and the actual number of elements" if (names[0] and names[1] and ((names[0].size * names[1].size) != self.count))
  elsif self.is_a? Matrix
    raise ArgumentError, "mismatch between the size of row labels and the actual number of rows" if (names[0] and (names[0].size != self.row_size))
    raise ArgumentError, "mismatch between the size of column labels and the actual number of rows" if (names[1] and (names[1].size != self.column_size))
  else
    raise ArgumentError, "unsupported type for With2DNames"
  end
  @row_names = names[0]
  @column_names = names[1]
end

#row_by_name(name) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rserve/with2dnames.rb', line 36

def row_by_name(name)
  return nil unless @row_names
  i = @row_names.index(name)
  return nil unless i
  result = row(i).to_a
  if @column_names
    result.extend WithNames
    result.names = @column_names
  end
  result
end