Module: Rserve::WithNames

Included in:
Rlist
Defined in:
lib/rserve/withnames.rb

Overview

Provides names to elements on an Array

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namesObject

Returns the value of attribute names.



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

def names
  @names
end

Instance Method Details

#[](v) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rserve/withnames.rb', line 131

def [](v)
  case v
  when Integer
      at(v)
    when Array
      raise "You must use something like v[[1]]" if v.size!=1 or !v[0].is_a? Integer
      at(v[0]-1)
    when String
      if !@names.nil?
        i=@names.index(v)
        i.nil? ? nil : at(i)
      else
        nil
      end
  end
end

#[]=(i, v) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/rserve/withnames.rb', line 121

def []=(i,v)
  case i
    when Integer
      super(i,v)
    when String
      put(i,v)
    else
      raise "Should be Integer or String"
  end
end

#__add(a, b = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/rserve/withnames.rb', line 111

def __add(a,b=nil)
  if b.nil?
    @data.push(a)
    @names=Array.new(@data.size-1) if @names.nil?
    @names.push(nil)
  else
    @data.insert(a,b)
    @names.insert(a,nil)
  end
end

#clearObject



45
46
47
48
# File 'lib/rserve/withnames.rb', line 45

def clear
  @names=nil
  super()
end

#delete_at(i) ⇒ Object



49
50
51
52
53
54
# File 'lib/rserve/withnames.rb', line 49

def delete_at(i)
  unless @names.nil?
    @names.delete_at(i)
  end
  super(i)
end

#has_name?(v) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rserve/withnames.rb', line 82

def has_name?(v)
  named? and @names.include? v
end

#inspectObject



42
43
44
# File 'lib/rserve/withnames.rb', line 42

def inspect
  "#<#{self.class}:#{self.object_id} #{to_s}>"
end

#key_at(v) ⇒ Object



91
92
93
# File 'lib/rserve/withnames.rb', line 91

def key_at(v)
  @names.nil? ?  nil : @names[v]
end

#named?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/rserve/withnames.rb', line 85

def named?
  !@names.nil?
end

#popObject



55
56
57
58
59
60
# File 'lib/rserve/withnames.rb', line 55

def pop
  unless @names.nil?
    @names.pop
  end
  super
end

#pretty_print(q) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rserve/withnames.rb', line 16

def pretty_print(q)
  q.group(1,'[|WN|',']') {
    q.seplist(self,nil,:each_with_index) {|v,i|
      if (@names.nil? or @names[i].nil?) 
       q.pp v
      else
        q.group {
        q.pp @names[i]
        q.text '='
        q.group(1) { q.pp v } 
        }
      end
    }
  }
end

#push(v, name = nil) ⇒ Object



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

def push(v,name=nil)
  @names||=Array.new(self.size)
  (@names.size-self.size).times { @names.push(nil)}
  @names.push(name)
  super(v)
end

#put(key, value) ⇒ Object

Put a value on specific key If key exists, replaces element of apropiate index If key doesn’t exists, works as push(value,key)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rserve/withnames.rb', line 97

def put(key,value)
  if key.nil?
    add(value)
    return nil
  end
  
  if !@names.nil?
    pos=@names.index(key)
    if !pos.nil?
      return self[pos]=value
    end
  end      
  push(value,key)      
end

#reverse!Object



62
63
64
65
66
67
# File 'lib/rserve/withnames.rb', line 62

def reverse!
  unless @names.nil?
    @names.reverse!
  end
  super
end

#shiftObject



68
69
70
71
72
73
# File 'lib/rserve/withnames.rb', line 68

def shift
  unless @names.nil?
    @names.shift
  end
  super  
end

#slice(*args) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/rserve/withnames.rb', line 74

def slice(*args)
  sliced=super(*args)
  unless @names.nil?
    sliced.extend Rserve::WithNames
    sliced.names=@names.slice(*args)
  end
  sliced
end

#to_aObject



88
89
90
# File 'lib/rserve/withnames.rb', line 88

def to_a
  Array.new(self)
end

#to_sObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rserve/withnames.rb', line 31

def to_s
  out=[]
  self.each_with_index { |v,i| 
    if !@names.nil? and !@names[i].nil?
      out.push("#{@names[i]}=#{v}")
    else
      out.push("#{v}")
    end
  }
  "[#{out.join(", ")}]"
end