| 1 |
# Limitations and Differences |
| 2 |
|
| 3 |
The philosophy of mruby is to be a lightweight implementation of |
| 4 |
the Ruby ISO standard. These two objectives are partially contradicting. |
| 5 |
Ruby is an expressive language with complex implementation details which |
| 6 |
are difficult to implement in a lightweight manner. To cope with this, |
| 7 |
limitations to the "Ruby Compatibility" are defined. |
| 8 |
|
| 9 |
This document is collecting these limitations. |
| 10 |
|
| 11 |
## Integrity |
| 12 |
|
| 13 |
This document does not contain a complete list of limitations. |
| 14 |
Please help to improve it by submitting your findings. |
| 15 |
|
| 16 |
## `Kernel.raise` in rescue clause |
| 17 |
|
| 18 |
`Kernel.raise` without arguments does not raise the current exception within |
| 19 |
a rescue clause. |
| 20 |
|
| 21 |
```ruby |
| 22 |
begin |
| 23 |
1 / 0 |
| 24 |
rescue |
| 25 |
raise |
| 26 |
end |
| 27 |
``` |
| 28 |
|
| 29 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 30 |
|
| 31 |
`ZeroDivisionError` is raised. |
| 32 |
|
| 33 |
#### mruby [3.1.0 (2022-05-12)] |
| 34 |
|
| 35 |
`RuntimeError` is raised instead of `ZeroDivisionError`. To re-raise the exception, you have to do: |
| 36 |
|
| 37 |
```ruby |
| 38 |
begin |
| 39 |
1 / 0 |
| 40 |
rescue => e |
| 41 |
raise e |
| 42 |
end |
| 43 |
``` |
| 44 |
|
| 45 |
## Fiber execution can't cross C function boundary |
| 46 |
|
| 47 |
mruby's `Fiber` is implemented similarly to Lua's co-routine. This |
| 48 |
results in the consequence that you can't switch context within C functions. |
| 49 |
Only exception is `mrb_fiber_yield` at return. |
| 50 |
|
| 51 |
## `Array` does not support instance variables |
| 52 |
|
| 53 |
To reduce memory consumption `Array` does not support instance variables. |
| 54 |
|
| 55 |
```ruby |
| 56 |
class Liste < Array |
| 57 |
def initialize(str = nil) |
| 58 |
@field = str |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
p Liste.new "foobar" |
| 63 |
``` |
| 64 |
|
| 65 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 66 |
|
| 67 |
`[]` |
| 68 |
|
| 69 |
#### mruby [3.1.0 (2022-05-12)] |
| 70 |
|
| 71 |
`ArgumentError` is raised. |
| 72 |
|
| 73 |
## Method visibility |
| 74 |
|
| 75 |
For simplicity reasons no method visibility (public/private/protected) is |
| 76 |
supported. Those methods are defined, but they are dummy methods. |
| 77 |
|
| 78 |
```ruby |
| 79 |
class VisibleTest |
| 80 |
|
| 81 |
def public_method; end |
| 82 |
|
| 83 |
private |
| 84 |
def private_method; end |
| 85 |
|
| 86 |
end |
| 87 |
|
| 88 |
p VisibleTest.new.respond_to?(:private_method, false) |
| 89 |
p VisibleTest.new.respond_to?(:private_method, true) |
| 90 |
``` |
| 91 |
|
| 92 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 93 |
|
| 94 |
``` |
| 95 |
false |
| 96 |
true |
| 97 |
``` |
| 98 |
|
| 99 |
#### mruby [3.1.0 (2022-05-12)] |
| 100 |
|
| 101 |
``` |
| 102 |
true |
| 103 |
true |
| 104 |
``` |
| 105 |
|
| 106 |
### Visibility Declaration |
| 107 |
|
| 108 |
The declaration form of following visibility methods are not implemented. |
| 109 |
|
| 110 |
- `public` |
| 111 |
- `private` |
| 112 |
- `protected` |
| 113 |
- `module_function` |
| 114 |
|
| 115 |
Especially, `module_function` method is not dummy, but no declaration form. |
| 116 |
|
| 117 |
```ruby |
| 118 |
module TestModule |
| 119 |
module_function |
| 120 |
def test_func |
| 121 |
p 'test_func called' |
| 122 |
end |
| 123 |
|
| 124 |
test_func |
| 125 |
end |
| 126 |
|
| 127 |
p 'ok' |
| 128 |
``` |
| 129 |
|
| 130 |
#### Ruby [ruby 2.5.5p157 (2019-03-15 revision 67260)] |
| 131 |
|
| 132 |
``` |
| 133 |
ok |
| 134 |
``` |
| 135 |
|
| 136 |
#### mruby [3.1.0 (2022-05-12)] |
| 137 |
|
| 138 |
``` |
| 139 |
test.rb:8: undefined method 'test_func' (NoMethodError) |
| 140 |
``` |
| 141 |
|
| 142 |
## `defined?` |
| 143 |
|
| 144 |
The `defined?` keyword is considered too complex to be fully |
| 145 |
implemented. It is recommended to use `const_defined?` and |
| 146 |
other reflection methods instead. |
| 147 |
|
| 148 |
```ruby |
| 149 |
defined?(Foo) |
| 150 |
``` |
| 151 |
|
| 152 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 153 |
|
| 154 |
``` |
| 155 |
nil |
| 156 |
``` |
| 157 |
|
| 158 |
#### mruby [3.1.0 (2022-05-12)] |
| 159 |
|
| 160 |
`NameError` is raised. |
| 161 |
|
| 162 |
## `alias` on global variables |
| 163 |
|
| 164 |
Aliasing a global variable works in CRuby but is not part |
| 165 |
of the ISO standard. |
| 166 |
|
| 167 |
```ruby |
| 168 |
alias $a $__a__ |
| 169 |
``` |
| 170 |
|
| 171 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 172 |
|
| 173 |
`nil` |
| 174 |
|
| 175 |
#### mruby [3.1.0 (2022-05-12)] |
| 176 |
|
| 177 |
Syntax error |
| 178 |
|
| 179 |
## Operator modification |
| 180 |
|
| 181 |
An operator can't be overwritten by the user. |
| 182 |
|
| 183 |
```ruby |
| 184 |
class String |
| 185 |
def + |
| 186 |
end |
| 187 |
end |
| 188 |
|
| 189 |
'a' + 'b' |
| 190 |
``` |
| 191 |
|
| 192 |
#### Ruby [ruby 2.0.0p645 (2015-04-13 revision 50299)] |
| 193 |
|
| 194 |
`ArgumentError` is raised. |
| 195 |
The re-defined `+` operator does not accept any arguments. |
| 196 |
|
| 197 |
#### mruby [3.1.0 (2022-05-12)] |
| 198 |
|
| 199 |
`'ab'` |
| 200 |
Behavior of the operator wasn't changed. |
| 201 |
|
| 202 |
## `Kernel#binding` is not supported until [3.0.0 (2021-03-05)] |
| 203 |
|
| 204 |
`Kernel#binding` method is not supported. |
| 205 |
|
| 206 |
#### Ruby [ruby 2.5.1p57 (2018-03-29 revision 63029)] |
| 207 |
|
| 208 |
```shell |
| 209 |
$ ruby -e 'puts Proc.new {}.binding' |
| 210 |
#<Binding:0x00000e9deabb9950> |
| 211 |
``` |
| 212 |
|
| 213 |
## `nil?` redefinition in conditional expressions |
| 214 |
|
| 215 |
Redefinition of `nil?` is ignored in conditional expressions. |
| 216 |
|
| 217 |
```ruby |
| 218 |
a = "a" |
| 219 |
def a.nil? |
| 220 |
true |
| 221 |
end |
| 222 |
puts(a.nil? ? "truthy" : "falsy") |
| 223 |
``` |
| 224 |
|
| 225 |
Ruby outputs `truthy`. mruby outputs `falsy`. |
| 226 |
|
| 227 |
## Argument Destructuring |
| 228 |
|
| 229 |
```ruby |
| 230 |
def m(a,(b,c),d); p [a,b,c,d]; end |
| 231 |
m(1,[2,3],4) # => [1,2,3,4] |
| 232 |
``` |
| 233 |
|
| 234 |
Destructured arguments (`b` and `c` in above example) cannot be accessed |
| 235 |
from the default expression of optional arguments and keyword arguments, |
| 236 |
since actual assignment is done after the evaluation of those default |
| 237 |
expressions. Thus: |
| 238 |
|
| 239 |
```ruby |
| 240 |
def f(a,(b,c),d=b) |
| 241 |
p [a,b,c,d] |
| 242 |
end |
| 243 |
f(1,[2,3]) |
| 244 |
``` |
| 245 |
|
| 246 |
CRuby gives `[1,2,3,nil]`. mruby raises `NoMethodError` for `b`. |
| 247 |
|
| 248 |
Keyword argument expansion has similar restrictions. The following example, gives `[1, 1]` for CRuby, mruby raises `NoMethodError` for `b`. |
| 249 |
|
| 250 |
```ruby |
| 251 |
def g(a: 1, b: a) |
| 252 |
p [a,b] |
| 253 |
end |
| 254 |
g(a:1) |
| 255 |
``` |