396 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 dcd4fe9fb0 range.rb: fix Range#hash for endless/beginless ranges
Use self.begin/self.end instead of first/last to compute hash for
ranges. The first/last methods raise RangeError for endless/beginless
ranges, but the internal begin/end accessors return nil safely.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 17:43:59 +09:00
Yukihiro "Matz" Matsumoto 21472638b9 mruby-compiler: implement hash pattern matching
Add support for hash patterns in pattern matching expressions:
- {key:} shorthand binds to variable with same name
- {key: pattern} matches key against pattern
- {**rest} captures remaining keys
- {**nil} requires exact match (no extra keys)
- {**} ignores extra keys without capturing

Parser adds new grammar rules (p_hash, p_hash_body, p_hash_elems,
p_hash_elem, p_kwrest) and new_pat_hash() constructor.

Codegen generates code to call deconstruct_keys on the target hash,
then iterates through key-pattern pairs to match each key's value.

Adds Hash#deconstruct_keys method that returns self for pattern matching.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27: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 bc24a32f2d enum.rb: standardize block parameter spacing
changed block spacing from method{ to method { for consistency.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:08 +09:00
Yukihiro "Matz" Matsumoto 35bab8dfed hash.rb: standardize block parameter spacing
changed block spacing from {|param| to {|param| (space before brace)
for consistency with the most common pattern in the codebase.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:08 +09:00
Yukihiro "Matz" Matsumoto 6b2d0b3e0f numeric.rb: add parentheses to block.call
added parentheses to block.call where the return value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:07 +09:00
Yukihiro "Matz" Matsumoto 1b85ad2bfe hash.rb: add parentheses to block.call
added parentheses to block.call where the return value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:07 +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 fa8f66ed1b enum.rb: fix Array#hash infinite loop with self-referencing enumerables
Modify `Enumerable#hash` to use `__method_recursive?(:hash)` for recursion
detection, preventing infinite loops when hashing self-referencing enumerables.
Add a test case to verify the fix.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:04 +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 96355f33b2 hash.c: implement Hash#eql? in C for better performance
Move Hash#eql? implementation from Ruby to C to improve performance and
consistency with other core methods. The C implementation uses mrb_eql
for value comparison, providing proper eql? semantics.

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-11 10:09:36 +09:00
Yukihiro "Matz" Matsumoto 633317a809 hash.c: implement Hash#== in C for better performance
Move Hash#== implementation from Ruby to C to improve performance
and consistency with other core methods. The C implementation
provides the same functionality while being more efficient.

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-11 10:09:35 +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 30d1db4528 10error.rb: move 2 more classes to error.c
- FloatDomainError
- RegexpError
2025-05-02 08:50:20 +09:00
Yukihiro "Matz" Matsumoto 0fb280adbf 10error.rb: move some error class definitions to error.c
- Exception
- ArgumentError
- LocalJumpError
- RangeErrpr
- TypeError
- IndexError
- KeyError
- NotImplementError
- FrozenError
- ZeroDivisionError
2025-05-01 15:10:55 +09:00
Yukihiro "Matz" Matsumoto 1bc16676a1 class.c: implement BasicObject#!= in C file
The method is defined in inline bytecode. As a side-effect, `00class.rb`
was removed.
2025-05-01 10:23:03 +09:00
Yukihiro "Matz" Matsumoto 08f74dd2be class.c: move alias attr attr_reader from 00class.rb 2025-04-28 14:20:07 +09:00
Yukihiro "Matz" Matsumoto 6c72f8b378 class.c (extend_object): remove method; implement Kernel#extend in C 2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 6c25b5896a class.c (prepend_features): remove the method
Just like `#append_features`, we remove `#prepend_features` and
implemented `#prepend` directly in C.
2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 9387cd382e class.c (append_features): remove the method
The technique is called "double dispatch" (that was popular in
Smalltalk), but it does not work well with mruby. It's slower and
consumes more memory. Even thought `#append_features` defined in ISO
standard (15.2.2.4.11), we decided to remove it. Strictly speaking, it
is mruby limitation. And it should be documented clearly.
2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 95b30a38dc kernel.rb: make Kernel#loop private 2025-03-07 17:17:44 +09:00
Yukihiro "Matz" Matsumoto 541cdf35eb 00kernel.rb: use #__send__ to skip private check 2025-03-07 17:17:41 +09:00
Yukihiro "Matz" Matsumoto 798fd7dbdf 00class.rb: use #__send__ to skip private check 2025-03-07 17:17:41 +09:00
Yukihiro "Matz" Matsumoto 4122cee9e4 class.c: implement attr_accessor in C 2025-03-07 17:17:39 +09:00
dearblue a7a81aa761 Storing method-id inside Symbol#to_proc
When called in combination with a method like `*_eval` or `*_exec` that switches self, `__send__` was passed an object that was not necessarily a symbol as the method name.

This problem was discovered during the #6389 correction process.
2024-10-27 10:41:51 +09:00
Yukihiro "Matz" Matsumoto e8bb03da40 numeric.rb (step): iterate over integers even if end is a float
It used to check all `start`, `end` and `step`. If either of them are
float number, `#step` iterated over float number. Now we don't check the
type of `end` argument.
2024-08-03 13:35:09 +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 fc9fffc5b5 kernel.rb (_inspect): _inspect method no longer needed 2023-06-22 23:32:31 +09:00
Yukihiro "Matz" Matsumoto 113565a1c5 hash.c (mrb_hash_to_s): add recursive inspect check to Hash#inspect 2023-06-20 11:18:38 +09: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 89a1719a13 compar.rb: use equal?() instead of ==
To avoid unnecessary infinite recursion.
2023-06-19 15:33:13 +09:00
Yukihiro "Matz" Matsumoto f5a7b6aad2 hash.rb: defer local variable initialization 2023-02-02 22:45:30 +09:00
Yukihiro "Matz" Matsumoto ecd2d8c11e wrap method arguments by parentheses as convention 2023-02-01 14:33:46 +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 abec98c681 numeric.c: implement ceil, floor, round, truncate methods in C.
- ISO 15.2.8.3.14 Integer#ceil
- ISO 15.2.8.3.17 Integer#floor
- ISO 15.2.8.3.20 Integer#round
- ISO 15.2.8.3.26 Integer#truncate
2022-08-10 13:38:23 +09:00
Yukihiro "Matz" Matsumoto 89f22158d3 string.rb (each_line): may truncate string by mistake; fix #5727 2022-06-24 13:25:45 +09: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