じ☆ve冰风 发表于 2026-8-2 18:32:29

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.
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

                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 unshiftmethod.

                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!


本贴来自国际rpgmaker官方论坛作者:kyonides处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/fixedarray.143480/
页: [1]
查看完整版本: FixedArray