RSS

Find 3rd party gems global variables in ruby

Find 3rd party gems global variables in ruby

Why Need Find Global Variables

In rails, if you want to use puma as rails server, you need make sure your app is thread safe, not only your app code itself, also 3rd party gems should also thread safe.

One of requirement for thread safe is avoid global variables.

Global variables are dangerous because they can be written to from anywhere. Overuse of globals can make isolating bugs difficult; it also tends to indicate that the design of a program has not been carefully thought out. Whenever you do find it necessary to use a global variable, be sure to give it a descriptive name that is unlikely to be inadvertently used for something else later (calling it something like $foo as above is probably a bad idea).

Ruby Global Variables

According ruby doc, global variable has a name beginning with $:

A global variable has a name beginning with $. It can be referred to from anywhere in a program. Before initialization, a global variable has the special value nil.

https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/globalvars.html

Find Global Variables

If ruby code follow ruby name convention to use $ as prefix of global variable, we can do text search with regular expression.

One of best tools to do code text search is ripgrep

Install ripgrep

If you’re a macOS Homebrew or a Linuxbrew user, then you can install ripgrep from homebrew-core:

$ brew install ripgrep

If you’re a MacPorts user, then you can install ripgrep from the official ports:

$ sudo port install ripgrep

If you’re a Windows Chocolatey user, then you can install ripgrep from the official repo:

$ choco install ripgrep

If you’re a Windows Scoop user, then you can install ripgrep from the official bucket:

$ scoop install ripgrep

If you’re an Arch Linux user, then you can install ripgrep from the official repos:

$ pacman -S ripgrep

If you’re a Gentoo user, you can install ripgrep from the official repo:

$ emerge sys-apps/ripgrep

If you’re a Fedora user, you can install ripgrep from official repositories.

$ sudo dnf install ripgrep

If you’re an openSUSE user, ripgrep is included in openSUSE Tumbleweed and openSUSE Leap since 15.1.

$ sudo zypper install ripgrep

If you’re a RHEL/CentOS 7/8 user, you can install ripgrep from copr:

$ sudo yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/repo/epel-7/carlwgeorge-ripgrep-epel-7.repo
$ sudo yum install ripgrep

Find the global variables with ripgrep

I use rbenv, so search the rbenv path like ~/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/.

Regular expression '\$[a-zA-Z0-9]+[ ]*[=]' will try to match global variable starts with $ and there is a = in the same line.

Here is an example:

$ rg -truby '\$[a-zA-Z0-9]+[ ]*[=]' ~/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/
...
...
~/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/resque-1.27.4/lib/resque/worker.rb
352:      $0 = "resque: Starting"
840:      $0 = "#{ENV['RESQUE_PROCLINE_PREFIX']}resque-#{Resque::Version}: #{string}"
...
...

References

OmniLock - Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps

Ad