70 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 13bc858dff mrblib: use yield instead of block.call for iteration methods
Replace block.call(x) with yield x in core iteration methods to take
advantage of the new OP_BLKCALL optimization. This improves Integer#times
by 13% and Array#each by 7%.

Methods updated:
- Integer#times, Integer#upto, Integer#downto
- Array#each, Array#each_index

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-27 14:57:26 +09:00
Yukihiro "Matz" Matsumoto ec67fd9587 mruby-compiler: implement array pattern matching
add support for array destructuring patterns in case/in expressions:
- [a, b, c] - fixed length match
- [first, *rest] - head + rest
- [*init, last] - init + tail
- [first, *middle, last] - head + middle + tail
- [1, x, 3] - mixed value and variable patterns
- [first, *, last] - anonymous rest (discarded)

implementation includes:
- grammar rules for p_array, p_array_body, p_rest in parse.y
- NODE_PAT_ARRAY codegen with deconstruct call and length checks
- rest variable binding via range slicing (arr[pre..-(post+1)])
- Array#deconstruct method (returns self)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:26 +09:00
Yukihiro "Matz" Matsumoto ec1f9860b1 mrblib: add parentheses to method calls with used return values
added parentheses to all `to_enum` and `super` calls where the return
value is used (returned, assigned, or passed to another method). this
makes the code style consistent with the guideline that method calls
should use parentheses when their return values are consumed.

changes:
- return to_enum :symbol -> return to_enum(:symbol)
- return to_enum :symbol, arg -> return to_enum(:symbol, arg)
- super message, name -> super(message, name)

affected files: 10error.rb, array.rb, enum.rb, hash.rb, kernel.rb,
numeric.rb, range.rb

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:06 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)

Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 05ad26f92a array: implement to_a and entries methods in c
Array#to_a now properly converts subclasses to Array objects. For example,
'class A<Array;end; p A.new(1,2).to_a.class' now returns Array, not A.

Co-authored-by: Atlassian Rovo Dev
2025-06-30 12:21:29 +09:00
Yukihiro "Matz" Matsumoto 7c7ee5c244 array.c: implement Array#delete method in C 2024-07-02 11:45:20 +09:00
Yukihiro "Matz" Matsumoto dd808a0be4 array.c: implement Array#<=> in C 2024-06-29 15:03:12 +09:00
Yukihiro "Matz" Matsumoto 8cffa04def array.c: implement Array#== and Array#eql? in C
It seems OK to call comparison from C method from measurement.
2024-06-29 08:32:06 +09:00
Yukihiro "Matz" Matsumoto 5b8de8d616 Array.c (mrb_ary_init): implement Array#initialize in C 2024-06-28 08:56:41 +09:00
Yukihiro "Matz" Matsumoto 5bd63d6232 array.c: replace sort! method implementation
- use heap sort (O(1)) instead of merge sort (O(n)) for better space
  complexity.

- method implemented in C for better performance

As a result, simple sorting now consumes far less memory and is faster.
Since it's implemented in C, fiber context switching is not allowed from
comparison, but we consider the risk is minimal (no one switches context
in the comparison, right?)
2024-06-26 11:28:56 +09:00
dearblue f0cd35c78b Passes the nonexistent key as a block argument in Array#delete
```ruby
a = %w(R G B A)
p a.delete("Y") { _1 }
# BEFORE => nil (same for mruby 3.3)
# AFTER  => "Y" (same for CRuby)
```
2024-05-10 22:46:11 +09:00
leviongit 67df0aca67 fix: Array#delete mistakingly calling block even if not passed 2024-05-09 14:33:23 +02:00
leviongit bb78c2cbc9 reimplement Array#delete with a helper method 2024-04-13 07:41:00 +02:00
leviongit 312d49c59f reimplement Array#delete in ruby 2024-04-13 07:21:51 +02:00
leviongit c04afb9e99 revert moving Array#delete to c 2024-04-13 06:52:37 +02:00
leviongit 14bd875d70 fix Array#delete
reimplement `Array#delete` in c, fixing `ary.delete(nil, &blk)` firing the block regardless of removal

minimal reproduction:
```rb
ary = [nil]
ret = ary.delete(nil) { "not deleted?" }
```
2024-04-12 17:30:03 +02:00
Yukihiro "Matz" Matsumoto 5cb0c7463b array.c (mrb_ary_to_s): add recursive check to Array#inspect 2023-06-20 07:06:33 +09:00
Yukihiro "Matz" Matsumoto 6bc44f8aa8 Remove non-existent ISO section numbers suffixed with "(x)" [ci skip]
We don't know how we introduced these section references.
Weak memories.
2023-01-08 22:32:16 +09:00
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10:00
John Bampton 44db4f18d3 Remove # coding: utf-8 from Ruby files
The default script encoding is Encoding::UTF-8 after v2.0.

https://ruby-doc.org/core-2.1.2/Encoding.html#class-Encoding-label-Script+encoding
2022-10-27 04:43:13 +10:00
Yukihiro "Matz" Matsumoto 01392e2719 mrblib/array.rb: add call-seq to each method description. [ci skip] 2022-06-22 19:02:54 +09:00
Yukihiro "Matz" Matsumoto 91255c7e9f mrblib/array.rb: Array#<=> to handle NoMethodError exception.
Array element may not respond to `<=>` method.
2022-03-26 22:57:41 +09:00
Yukihiro "Matz" Matsumoto 5c7aa4f299 Simplify module inclusion for Array, Hash and Range. 2021-05-13 16:07:09 +09:00
Yukihiro "Matz" Matsumoto 955c7f703e array.rb: add Array#entries as an alias to Array#to_a.
The performance of `Enumerable#entries` are significantly slower than
`Array#to_a`.
2021-05-12 07:17:49 +09:00
John Bampton 4fa3359d44 refactor: remove trailing whitespace from C, Header, Ruby and YAML files
Lint
2020-12-15 19:44:02 +10:00
Yukihiro "Matz" Matsumoto 71f9add1c5 Move inline iseq in array.c to array.rb.
There's no efficiency difference since `cdump` is implemented.
2020-11-25 23:14:38 +09:00
Yukihiro "Matz" Matsumoto afba74ff50 Add Array.new([1,2,3]) initialization. 2020-11-05 14:29:25 +09:00
Yukihiro "Matz" Matsumoto 0bb4d10ca7 Define Array#to_a to avoid unnecessary loops. 2020-07-22 14:56:56 +09:00
Yukihiro "Matz" Matsumoto 914da3d712 Small comment fix in mrblib/array.c. 2020-07-22 14:56:36 +09:00
Yukihiro "Matz" Matsumoto 5b1f25a4e0 Implement Array#each using inline mruby bytecode. 2019-08-17 14:46:32 +09:00
Yukihiro "Matz" Matsumoto d605b72c1d Merge branch 'master' into i110/inspect-recursion 2019-07-17 10:35:41 +09:00
Yukihiro "Matz" Matsumoto af51119d3a Raise error on failed comparison in sort; ref #4307 2019-03-02 15:46:50 +09:00
Yukihiro "Matz" Matsumoto afca99a40b Remove implicit conversion using to_int method.
The ISO standard does not include implicit type conversion using
`to_int`. This implicit conversion often causes vulnerability.
There will be no more attacks like #4120.

In addition, we have added internal convenience method `__to_int` which
does type check and conversion (from floats).
2018-11-19 11:28:51 +09:00
Yukihiro "Matz" Matsumoto 34872e90d4 Re-implement Array#_inspect and Hash#_inspect without blocks.
To reduce the env object allocation; ref #4143
2018-10-29 11:55:49 +09:00
Tomoyuki Sahara 91444c4747 replace quicksort with mergesort. 2018-10-18 13:07:47 +09:00
Yukihiro "Matz" Matsumoto fee9ec61bb Make Array.new to accept both integers and floats.
This time we used `Integral` module which is mruby specific.
2018-08-13 23:15:52 +09:00
Ichito Nagata 2af92d0ebc Let inspect recursion do the right thing 2018-06-04 11:25:10 +09:00
Tomoyuki Sahara f0e54df4fe sort method should not consume system stack. 2018-04-23 12:28:15 +09:00
Yukihiro "Matz" Matsumoto d88191e7e7 Implement Array#__svalue in C. 2018-04-17 11:32:31 +09:00
Yukihiro "Matz" Matsumoto 1a8483f17e __sort_sub__ is a method defined in Array; fix #3970
Reorganize `__sort_sub__` arguments.
2018-03-16 14:49:51 +09:00
Christopher Aue c956e17c02 Replaced Array#each with while loop for performance reasons
Example benchmark:
$ time build/bench/bin/mruby -e "Array.new(2_000_000){ |i| i }.index{ |i| i == 1_999_999 }"

Before:
real    0m0.934s
user    0m0.922s
sys     0m0.003s

After:
real    0m0.590s
user    0m0.583s
sys     0m0.007s
2017-08-26 15:57:25 +02:00
Christopher Aue 3359b86ec7 Improved speed of enumeration methods 2017-07-30 13:19:31 +02:00
Yukihiro "Matz" Matsumoto 7b8d784040 Reimplement sort method to reduce array copying. 2017-07-25 14:42:38 +09:00
Jun Hiroe 88076901fd Refactor Array#each 2015-12-26 10:12:01 +09:00
Yusuke Tanaka d3f59db597 Remove redundant conditions of Array#each in ruby extension 2015-12-15 19:54:29 +09:00
go.kikuta 931a7223e6 array.rb: refactor (use onliner code if possible) 2015-08-21 14:56:49 +09:00
go.kikuta c326f065e1 array.rb: refactor some code 2015-08-19 16:17:06 +09:00
Franck Verrot 2588507285 Remove unnecessary backticks.
Dr Markus Kuhn published in 1999 an article [1] explaining in details
why we shouldn't use the ASCII grave accent (0x60) as a left quotation.

Backticks have been used most notably to produce nice-looking LaTeX
documents but it doesn't seem to be an issue on modern platforms and
for the oldest ones, there are workarounds as mentioned by Dr Kuhn.

[1]: https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html
2015-06-24 13:07:07 +02:00
Yukihiro "Matz" Matsumoto b05d736b49 update mrblib/*.rb files to conform (some of) Rubocop checks 2015-05-29 17:01:52 +09:00
Yukihiro "Matz" Matsumoto 79fb18445d rescue SystemStackError that comes from inspecting self-referencing Hashes and Arrays; fix #2461 2014-07-12 23:22:20 +09:00