Files
mruby-mruby/mrbgems/mruby-range-ext
Yukihiro "Matz" Matsumoto 0fab703028 class.c: use linear search for method tables; make ROM entries const
Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 08:26:05 +09:00
..

mruby-range-ext

This gem extends the functionality of Ruby's Range class in mruby. It provides several new methods to make working with ranges more convenient and powerful.

Methods

first / first(n)

Returns the first object in the range, or an array of the first n elements.

Example:

(10..20).first     #=> 10
(10..20).first(3)  #=> [10, 11, 12]

last / last(n)

Returns the last object in the range, or an array of the last n elements. Note that with no arguments last will return the object that defines the end of the range even if exclude_end? is true.

Example:

(10..20).last      #=> 20
(10...20).last     #=> 20
(10..20).last(3)   #=> [18, 19, 20]
(10...20).last(3)  #=> [17, 18, 19]

max

Returns the maximum value in the range. Returns nil if the range is empty or excludes its end and the end is not an Integer. For non-numeric ranges or when a block is given, it delegates to Enumerable#max.

Example:

(10..20).max      #=> 20
(10...20).max     #=> 19
('a'..'z').max    #=> "z"

min

Returns the minimum value in the range. For non-numeric ranges or when a block is given, it delegates to Enumerable#min.

Example:

(10..20).min      #=> 10
('a'..'z').min    #=> "a"

overlap?(other_range)

Returns true if self and other_range have at least one element in common.

Example:

(1..5).overlap?(4..6) #=> true
(1..5).overlap?(7..9) #=> false

cover?(obj_or_range)

Returns true if the given object or all elements of the given range are within self.

Example:

("a".."z").cover?("c")    #=> true
("a".."z").cover?("5")    #=> false
("a".."z").cover?("cc")   #=> true # Note: This behavior might be surprising for strings.
(1..5).cover?(2..4)      #=> true
(1..5).cover?(2..7)      #=> false

size

Returns the number of elements in the range. Both the begin and the end of the Range must be Numeric, otherwise nil is returned. For endless ranges with an Integer beginning, it returns Infinity.

Example:

(10..20).size    #=> 11
('a'..'z').size  #=> nil
(1..).size       #=> Infinity

License

This gem is released under the MIT License.