class Envo::ListVal

Attributes

ar[R]

Public Class Methods

new(ar) click to toggle source
# File lib/envo/val/list_val.rb, line 3
def initialize(ar)
  @ar = ar
end

Public Instance Methods

accept_assign?(other) click to toggle source
# File lib/envo/val/list_val.rb, line 85
def accept_assign?(other)
  other.list?
end
accept_item?(item) click to toggle source
# File lib/envo/val/list_val.rb, line 97
def accept_item?(item)
  true
end
clean!() click to toggle source
# File lib/envo/val/list_val.rb, line 34
def clean!
  uniq!
end
delete(elem) click to toggle source
# File lib/envo/val/list_val.rb, line 25
def delete(elem)
  @ar.delete(elem)
end
delete_at(index) click to toggle source
# File lib/envo/val/list_val.rb, line 28
def delete_at(index)
  @ar.delete_at(index)
end
insert(elem, pos = nil) click to toggle source
# File lib/envo/val/list_val.rb, line 8
def insert(elem, pos = nil)
  # assume unique elements
  old_index = @ar.index(elem)
  new_index = case pos
    when :front then 0
    when :back then -1
    else old_index
  end

  return @ar << elem if !new_index
  return @ar if new_index == old_index
  return @ar.insert(new_index, elem) if !old_index

  # we need to reorder
  @ar.delete_at(old_index)
  @ar.insert(new_index, elem)
end
invalid_description() click to toggle source
# File lib/envo/val/list_val.rb, line 88
def invalid_description
  @ar.empty? ? "empty list" : nil
end
list?() click to toggle source
# File lib/envo/val/list_val.rb, line 91
def list?
  true
end
pretty_print(ctx) click to toggle source
# File lib/envo/val/list_val.rb, line 70
def pretty_print(ctx)
  ctx.puts "["
  @ar.each_with_index do |v, i|
    str = @ar.count(v) > 1 ? 'D ' : '  '
    str += "#{i}:".ljust(4)
    str += v
    ctx.puts str
  end
  ctx.puts ']'
end
shift(elem, dir) click to toggle source
# File lib/envo/val/list_val.rb, line 37
def shift(elem, dir)
  i = @ar.index(elem)
  return nil if i == nil
  shift_at(i, dir)
end
shift_at(i, dir) click to toggle source
# File lib/envo/val/list_val.rb, line 42
def shift_at(i, dir)
  return nil if i>@ar.size

  if dir == :front
    return i if i == 0
    elem = ar[i]
    @ar.delete_at i
    @ar.unshift(elem)
    0
  elsif dir == :back
    return i if i == (@ar.size-1)
    elem = ar[i]
    @ar.delete_at i
    @ar << elem
    @ar.size-1
  elsif dir == :up
    return i if i == 0
    @ar[i-1], @ar[i] = @ar[i], @ar[i-1]
    i - 1
  elsif dir == :down
    return i if i == (@ar.size-1)
    @ar[i+1], @ar[i] = @ar[i], @ar[i+1]
    i + 1
  else
    -1
  end
end
to_list() click to toggle source
# File lib/envo/val/list_val.rb, line 94
def to_list
  return self
end
to_s() click to toggle source
# File lib/envo/val/list_val.rb, line 100
def to_s
  raise StandardError.new "list can't be converted to String"
end
type() click to toggle source

casts

# File lib/envo/val/list_val.rb, line 82
def type
  :list
end
uniq!() click to toggle source
# File lib/envo/val/list_val.rb, line 31
def uniq!
  @ar.uniq!
end