Posts

Showing posts with the label ruby

Error installing ruby 2.6.7 on mac os - how to resolve?

5 I get the following error when trying to upgrade Ruby to 2.6.7: $ rbenv install 2.6.7 ... implicit declaration of function 'rb_native_mutex_destroy' is invalid in C99 vm.c:2489:34: warning: expression does not compute the number of elements in this array; element type is 'const int', not 'VALUE' (aka 'unsigned long') [-Wsizeof-array-div] sizeof(ec->machine.regs) / sizeof(VALUE)); ~~~~~~~~~~~~~~~~ ^ vm.c:2489:34: note: place parentheses around the 'sizeof(VALUE)' expression to silence this warning Is there a fix? ruby ...

Why is the string '3' not matched in a case statement with the range ('0'…'10')?

28 1 When I try to match for the string '3' in a case statement, it matches if the range goes up to '9', but not '10'. I'm guessing it has something to do with the triple equals operator, but I don't know the exact reason why it can be in the range, but not matched. Here is an IRB run documenting both cases that work (with '9'), and don't work (with '10'): case '3' when ('0'...'9') puts "number is valid" else puts "number is not valid" end Output: number is valid case '3' when ('0'...'10') puts "number is valid" else puts "number is not valid" end Output: number is not valid The methods that I used as ...

Array#push causes “stack level too deep” error with large arrays

I made two arrays, each with 1 million items: a1 = 1_000_000.times.to_a a2 = a1.clone I tried to push a2 into a1: a1.push *a2 This returns SystemStackError: stack level too deep. However, when I try with concat, I don't get the error: a1.concat a2 a1.length # => 2_000_000 I also don't get the error with the splat operator: a3 = [*a1, *a2] a3.length # => 2_000_000 Why is this the case? I looked at the documentation for Array#push, and it is written in C. I suspect that it may be doing some recursion under the hood, and that's why it's causing this error for large arrays. Is this correct? Is it not a good idea to use push for large arrays? I think that this is not a recursion error, but an argument stack error. You are running up against the limit of the Ruby VM stack depth for arguments. The problem is the splat operator, which is passed as an argument to push. The splat operator is expanded into a million element argument list for push. As function arguments ...