Class: FAM::Machine::RAM

Inherits:
Object show all
Defined in:
lib/fam/machine/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ RAM

Returns a new instance of RAM.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fam/machine/memory.rb', line 14

def initialize size
  alloc = 0
  case size
  when Numeric
    alloc = size
  when String
    suffix = size.upcase[/[A-Z]+/] || ''
    suffix = 'kB' if suffix == 'KB'
    unless suffix.empty?
      alloc = size[/[0-9]+/].to_i * SIZES[suffix.to_sym]
    else
      alloc = size.to_i
    end
  else
    abort 'ERROR: `size` argument must be a string or numeric!'.red
  end
  @array = Array.new(alloc, NULL)
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



12
13
14
# File 'lib/fam/machine/memory.rb', line 12

def array
  @array
end

Instance Method Details

#[](i) ⇒ Object



33
34
35
# File 'lib/fam/machine/memory.rb', line 33

def [](i)
  @array[i]
end

#[]=(i, value) ⇒ Object



37
38
39
40
# File 'lib/fam/machine/memory.rb', line 37

def []=(i, value)
  abort 'ERROR: Only integers can be stored in memory!' unless i.class == Integer
  @array[i] = value
end

#to_aObject



42
43
44
# File 'lib/fam/machine/memory.rb', line 42

def to_a
  @array
end

#to_sObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fam/machine/memory.rb', line 46

def to_s
  result = String.new
  width = %x{ tput cols }.to_i
  i = j = 0
  loop do
    break if j >= @array.size
    line = String.new
    i = j
    while line.size < width && j <= @array.size
      line << "#{@array[j].to_s.rjust 4, '0'}XXXXX" # `X` padding
      j += 1
    end
    result << "| #{@array[i..j].map {|e| e.to_s.rjust 4, '0'}.join ' | '} |\n"
  end
  result
end