Class: FAM::Machine::RAM
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #[]=(i, value) ⇒ Object
-
#initialize(size) ⇒ RAM
constructor
A new instance of RAM.
- #to_a ⇒ Object
- #to_s ⇒ Object
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
#array ⇒ Object
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_a ⇒ Object
42 43 44 |
# File 'lib/fam/machine/memory.rb', line 42 def to_a @array end |
#to_s ⇒ Object
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 |