FixedArray

FixedArray

The Array With a Fixed Size
for XP VX & ACE
​

Have you ever wanted to keep a Ruby Array at a certain size? Was it a pain you know where to prevent it from ever increasing its size or length?

Now you should not worry about that anymore!

Even so you can still clear its contents at any given time. 😉

Ruby:
# * FixedArray

# Have you ever wanted to keep a Ruby Array at a certain size?
# Was it a pain you know where to prevent it from ever increasing
# its size or length?
# Now you should not worry about that anymore!
# Even so you can still clear its contents at any given time.

# Examples:

# ary = FixedArray.new(3)
# returns: []

# ary = FixedArray.new(3, :a, :b, :c, :d)
# returns: [:a, :b, :c]

class FixedArray < Array
  def initialize(total, *args)
    @maxsize = total
    repeats = args.size > total ? total : args.size
    repeats.times{ push(args.shift) }
  end
  def push(elem) size == @maxsize ? self : super(elem) end
  def <<(elem) size == @maxsize ? self : super(elem) end
  def unshift(elem) size == @maxsize ? self : super(elem) end
  alias :>> :unshift
  attr_accessor :maxsize
end

Script Calls

You may use p or print or puts or msgbox or msgbox_p script calls depending on your debugging needs and RM version.

Actually >> is an alias of the unshift method.

Code:
array = FixedArray.new(3)
# returns: []
p array.size
# returns: 0
p array.maxsize
# returns: 3
array << :b << :c
# returns: [:b, :c]
array >> :a
# returns: [:a, :b, :c]
array = FixedArray.new(3, :a, :b, :c, :d)
# returns: [:a, :b, :c]
p array.size
# returns: 3
p array.maxsize
# returns: 3

Terms & Conditions

You may include my nickname if you want.
It's free as in beer and speech!
I can't say I've ever had a burning need for a fixed-size array, or even a regular non-burning need for one! But hopefully an entire pile of people will find a use for this. <3
Same. If anyone finds a use for this. I would be curious what it is for?
Well, let's think about something akin to XP's equipment slots that only featured 4 slots. Instead of declaring 4 different variables and their corresponding methods, you can now use the FixedArray class to keep them intact in case somebody wanted to increase that custom array without going through some in game training or just by cheating. XD

At least something like that was what crossed my mind when I was working on my KPartyActors script and its custom handling of the skill learning process. Yet, it won't deter the game developer from increasing its maximum limit under certain conditions.
Well, let's think about something akin to XP's equipment slots that only featured 4 slots. Instead of declaring 4 different variables and their corresponding methods, you can now use the FixedArray class to keep them intact in case somebody wanted to increase that custom array without going through some in game training or just by cheating. XD

At least something like that was what crossed my mind when I was working on my KPartyActors script and its custom handling of the skill learning process. Yet, it won't deter the game developer from increasing its maximum limit under certain conditions.
What additional utility would this have over the .first or .take methods?

Original thread: https://forums.rpgmakerweb.com/threads/fixedarray.143480/