[RUBY] A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...

Ruby has special variables $ 1, $ 2, $ 3 ..., and $ n is assigned the value that matches the nth parenthesis in the last successful pattern match.

"abc" =~ /(.)(.)(.)/
p $1 #=> "a"
p $2 #=> "b"
p $3 #=> "c"

As of September 26, 2020

Number n You can use any number of large positive integers.

It says, but in reality, when I try to use a huge value, a warning is displayed (I noticed when playing)

$ ruby -e '$9999999999999999'
-e:1: warning: `$9999999999999999' is too big for a number variable, always nil

In this case, I am wondering how much value can be used as n of $ n, so when I looked it up with the following code using a simple binary search, the maximum value seems to be 1073741823 in my environment. did.

require 'stringio'

ok = 0
ng = 10**10

while (ok-ng).abs > 1
  io = StringIO.new
  $stderr = io

  mi = (ok+ng)/2
  if io.size == 0
    ok = mi
    ng = mi

  $stderr = STDERR

p ok #=> 1073741823

Even if you check it by another method, it seems that you will be warned if $ 1073741823 is the maximum and the value is larger than that. However, the value 1073741823 seems to be environment-dependent, so depending on the environment, you may be able to use a larger value, or you may get a warning with a smaller value.

$ ruby -e '$1073741823' 
#=>Nothing happens

$ ruby -e '$1073741824'
#=>A warning is output
#=> -e:1: warning: `$1073741824' is too big for a number variable, always nil

The obvious question is what happens if the match is successful with a regular expression that captures more than 1073741823, but that's okay. In Ruby, the upper limit of the number of captures is set to 32767, and regular expressions that exceed that limit will fail to compile.

$ ruby -e 'Regexp.compile("(.)"*32767)' 
#=>Nothing happens

$ ruby -e 'Regexp.compile("(.)"*32768)'
#=>RegexpError occurs. The message is as follows
#=> `initialize': too many capture groups are specified:

I found the value 32767 with a simple script using the following binary search, but when I look at the source code of Onikumo, which is the regular expression engine of Ruby, I see the upper limit of the number of captures [ʻONIG_MAX_CAPTURE_GROUP_NUM. There is a variable to show, which seems to be 32767. ](Https://github.com/ruby/ruby/blob/c5eb24349a4535948514fe765c3ddb0628d81004/include/ruby/onigmo.h#L438), then the maximum value on the side of the special variable $ 1, $ 2, $ 3 ...I feel like it can be 32767 (´ ・ ω ・)

ok = 0
ng = 10**8

while (ok-ng).abs > 1
  mi = (ok+ng)/2
    ok = mi
  rescue RegexpError => e
    ng = mi

p ok #=> 32767

** Environmental information **

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Recommended Posts

A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
[Rails] What to do when the view collapses when a message is displayed with the errors method
[WSL] Solution for the phenomenon that 404 is displayed when trying to insert Java with apt (personal memo)
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
When requested access to the resource is denied when pushing with Docker
[Ruby] This is the solution. When should I use instance variables?
I got an IllegalAccessError when trying to use PowerMock with JUnit
When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What to do if Operation not permitted is displayed when you execute a command in the terminal
[IOS] What to do when the image is filled with one color
A program that determines whether the entered integer is close to an integer
[Java small story] Monitor when a value is added to the List
Error when trying to use heroku command
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
Possibility when deploying to EC2 but nothing is displayed in the error log
When is it said that you can use try with a Swift error?
[Rails] When transitioning to a page with link_to, move to the specified location on the page
What to do when is invalid because it does not start with a'-'
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
I want to solve the problem that JS is not displayed properly unless reloaded when transitioning with Turbolinks: link_to
When the project is not displayed in eclipse
A story about PKIX path building failed when trying to deploy to tomcat with Jenkins
Use the --build-arg option of docker-compose to pass environment variables to the container built with Dockerfile
When importing CSV with Rails, it was really easy to use the nkf command
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
A memo to do for the time being when building CentOS 6 series with VirtualBox
What to do when Address already in use is displayed after executing rails s
Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
Summary of how to use the proxy set in IE when connecting with Java
When the hover of Eclipse is hard to see
A memorandum when trying Spring Data JPA with STS
Considering a property editor to use with SpringToolSuite (STS)
A memorandum when trying to create a GUI using JavaFX
A story about trying to get along with Mockito
A memo to check when you try to use Lombok
How to perform a specific process when the back button is pressed in Android Fragment
When trying to insert a specific gem (levenshtein) with Docker, an error occurred without ruby-dev
When a Java file created with the Atom editor is garbled when executed at the command prompt
How to reduce the load on the program even a little when combining characters with JAVA
wsimport error handling (A class / interface with the same name "xxx" is already in use)