Rserve::WithNames

Provides names to elements on an Array

Attributes

names[R]

Public Instance Methods

[](v) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
clear() click to toggle source
# File lib/rserve/withnames.rb, line 45
def clear
  @names=nil
  super()
end
delete_at(i) click to toggle source
# 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) click to toggle source
# File lib/rserve/withnames.rb, line 82
def has_name?(v)
  named? and @names.include? v
end
inspect() click to toggle source
# File lib/rserve/withnames.rb, line 42
def inspect
  "#<#{self.class}:#{self.object_id} #{to_s}>"
end
key_at(v) click to toggle source
# File lib/rserve/withnames.rb, line 91
def key_at(v)
  @names.nil? ?  nil : @names[v]
end
named?() click to toggle source
# File lib/rserve/withnames.rb, line 85
def named?
  !@names.nil?
end
names=(v) click to toggle source
# File lib/rserve/withnames.rb, line 5
def names=(v)
  raise ArgumentError, "#{self}: names size #{v.size} should be equal to object size #{self.size}" if !v.nil? and v.size!=self.size
  raise ArgumentError, "element must be String or nil" unless v.nil? or v.all? {|v1| v1.nil? or v1.is_a? String}
  @names=v
end
pop() click to toggle source
# File lib/rserve/withnames.rb, line 55
def pop
  unless @names.nil?
    @names.pop
  end
  super
end
pretty_print(q) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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!() click to toggle source
# File lib/rserve/withnames.rb, line 62
def reverse!
  unless @names.nil?
    @names.reverse!
  end
  super
end
shift() click to toggle source
# File lib/rserve/withnames.rb, line 68
def shift
  unless @names.nil?
    @names.shift
  end
  super  
end
slice(*args) click to toggle source
# 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_a() click to toggle source
# File lib/rserve/withnames.rb, line 88
def to_a
  Array.new(self)
end
to_s() click to toggle source
# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.