Skip to content

Commit 0330d22

Browse files
committed
(maint) Use match? methods
RuboCop's Performance/RegexpMatch cop flagged several files for using `=~` to regex match when the `match?` method (introduced in Ruby 2.4) is often more performant. This commit updates instances of `=~` to use `match?` instead.
1 parent 53c3599 commit 0330d22

File tree

31 files changed

+52
-52
lines changed

31 files changed

+52
-52
lines changed

install.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def prepare_installation
153153
# /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin
154154
# which is not generally where people expect executables to be installed
155155
# These settings are appropriate defaults for all OS X versions.
156-
RbConfig::CONFIG['bindir'] = '/usr/bin' if RUBY_PLATFORM =~ /^universal-darwin[\d\.]+$/
156+
RbConfig::CONFIG['bindir'] = '/usr/bin' if RUBY_PLATFORM.match?(/^universal-darwin[\d\.]+$/)
157157

158158
# if InstallOptions.configdir
159159
# configdir = InstallOptions.configdir
@@ -176,7 +176,7 @@ def prepare_installation
176176
sitelibdir = $LOAD_PATH.find { |x| x =~ /site_ruby/ }
177177
if sitelibdir.nil?
178178
sitelibdir = File.join(libdir, 'site_ruby')
179-
elsif sitelibdir !~ Regexp.quote(version)
179+
elsif !sitelibdir&.match?(Regexp.quote(version))
180180
sitelibdir = File.join(sitelibdir, version)
181181
end
182182
end
@@ -232,7 +232,7 @@ def install_binfile(from, op_file, target)
232232
File.open(tmp_file.path, 'w') do |op|
233233
op.puts "#!#{ruby}"
234234
contents = ip.readlines
235-
contents.shift if contents[0] =~ /^#!/
235+
contents.shift if /^#!/.match?(contents[0])
236236
op.write contents.join
237237
end
238238
end

lib/facter/custom_facts/core/execution/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def log_stderr(msg, command, logger)
140140

141141
def builtin_command?(command)
142142
output, _status = Open3.capture2("type #{command}")
143-
output.chomp =~ /builtin/ ? true : false
143+
/builtin/.match?(output.chomp) ? true : false
144144
end
145145
end
146146
end

lib/facter/custom_facts/core/execution/posix.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def expand_command(command)
4444

4545
return unless exe && (expanded = which(exe))
4646

47-
expanded = "'#{expanded}'" if expanded =~ /\s/
47+
expanded = "'#{expanded}'" if /\s/.match?(expanded)
4848
expanded << " #{args}" if args
4949

5050
expanded

lib/facter/custom_facts/core/execution/windows.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def search_paths
1111
def which(bin)
1212
# `echo` is allowed for facter 3.x compatibility, otherwise
1313
# all commands much be found on the PATH or absolute.
14-
return bin if /^echo$/i =~ bin
14+
return bin if /^echo$/i.match?(bin)
1515

1616
if absolute_path?(bin)
1717
return bin if File.executable?(bin)
@@ -56,7 +56,7 @@ def expand_command(command)
5656

5757
return unless exe && (expanded = which(exe))
5858

59-
expanded = "\"#{expanded}\"" if expanded =~ /\s+/
59+
expanded = "\"#{expanded}\"" if /\s+/.match?(expanded)
6060
expanded << " #{args}" if args
6161

6262
expanded

lib/facter/custom_facts/util/normalization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def normalize(value)
5151
# @param value [String]
5252
# @return [void]
5353

54-
if RUBY_VERSION =~ /^1\.8/
54+
if RUBY_VERSION.match?(/^1\.8/)
5555
require 'iconv'
5656

5757
def normalize_string(value)

lib/facter/facts/debian/architecture.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Architecture
1010
def call_the_resolver
1111
fact_value = Facter::Resolvers::Uname.resolve(:machine)
1212
fact_value = 'amd64' if fact_value == 'x86_64'
13-
fact_value = 'i386' if fact_value =~ /i[3456]86|pentium/
13+
fact_value = 'i386' if /i[3456]86|pentium/.match?(fact_value)
1414

1515
[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)]
1616
end

lib/facter/facts/linux/hypervisors/kvm.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def discover_provider
5252

5353
return { google: true } if manufacturer == 'Google'
5454

55-
return { openstack: true } if manufacturer =~ /^OpenStack/
55+
return { openstack: true } if /^OpenStack/.match?(manufacturer)
5656

57-
return { amazon: true } if manufacturer =~ /^Amazon/
57+
return { amazon: true } if /^Amazon/.match?(manufacturer)
5858

5959
{}
6060
end

lib/facter/facts/linux/os/architecture.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Architecture
1010
def call_the_resolver
1111
fact_value = Facter::Resolvers::Uname.resolve(:machine)
1212

13-
fact_value = 'i386' if fact_value =~ /i[3456]86|pentium/
13+
fact_value = 'i386' if /i[3456]86|pentium/.match?(fact_value)
1414

1515
[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)]
1616
end

lib/facter/facts/windows/hypervisors/kvm.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def discover_provider
2727

2828
return { google: true } if manufacturer == 'Google'
2929

30-
return { openstack: true } if Facter::Resolvers::DMIComputerSystem.resolve(:name) =~ /^OpenStack/
30+
return { openstack: true } if /^OpenStack/.match?(Facter::Resolvers::DMIComputerSystem.resolve(:name))
3131

32-
return { amazon: true } if manufacturer =~ /^Amazon/
32+
return { amazon: true } if /^Amazon/.match?(manufacturer)
3333
end
3434
end
3535
end

lib/facter/framework/config/fact_groups.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_group_ttls(group_name)
6464
def get_fact(fact_name)
6565
return @facts_ttls[fact_name] if @facts_ttls[fact_name]
6666

67-
result = @facts_ttls.select { |name, fact| break fact if fact_name =~ /^#{name}\..*/ }
67+
result = @facts_ttls.select { |name, fact| break fact if /^#{name}\..*/.match?(fact_name) }
6868
return nil if result == {}
6969

7070
result

lib/facter/framework/core/fact_loaders/fact_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def construct_reject_lists(blocked_facts, facts)
103103

104104
blocked_facts.each do |blocked|
105105
facts.each do |fact|
106-
next unless fact.name =~ /^#{blocked}\..*|^#{blocked}$/
106+
next unless /^#{blocked}\..*|^#{blocked}$/.match?(fact.name)
107107

108108
if fact.type == :core
109109
reject_list_core << fact

lib/facter/framework/formatters/legacy_fact_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def remove_comma_and_quotation(output)
9595
# quotation marks that come after \ are not removed
9696
@log.debug('Remove unnecessary comma and quotation marks on root facts')
9797
output.split("\n")
98-
.map! { |line| line =~ /^[\s]+/ ? line : line.gsub(/,$|(?<!\\)\"/, '').gsub('\\"', '"') }.join("\n")
98+
.map! { |line| /^[\s]+/.match?(line) ? line : line.gsub(/,$|(?<!\\)\"/, '').gsub('\\"', '"') }.join("\n")
9999
end
100100
end
101101
end

lib/facter/framework/formatters/yaml_fact_formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def unquote_keys(fact_hash)
5252
end
5353

5454
def needs_quote?(value)
55-
return false if value =~ /true|false/
55+
return false if /true|false/.match?(value)
5656
return false if value[/^[0-9]+$/]
57-
return true if value =~ /y|Y|yes|Yes|YES|n|N|no|No|NO|True|TRUE|False|FALSE|on|On|ON|off|Off|OFF|:/
57+
return true if /y|Y|yes|Yes|YES|n|N|no|No|NO|True|TRUE|False|FALSE|on|On|ON|off|Off|OFF|:/.match?(value)
5858
return false if value[/[a-zA-Z]/]
5959
return false if value[/[0-9]+\.[0-9]+/]
6060

lib/facter/patches/sysfilesystem/sys/statvfs.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Statvfs < FFI::Struct
1010
# it the second time will make FFI log a warning message.
1111
remove_instance_variable(:@layout) if @layout
1212

13-
if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
13+
if /darwin|osx|mach/i.match?(RbConfig::CONFIG['host_os'])
1414
layout(
1515
:f_bsize, :ulong,
1616
:f_frsize, :ulong,
@@ -24,7 +24,7 @@ class Statvfs < FFI::Struct
2424
:f_flag, :ulong,
2525
:f_namemax, :ulong
2626
)
27-
elsif RbConfig::CONFIG['host'] =~ /bsd/i
27+
elsif /bsd/i.match?(RbConfig::CONFIG['host'])
2828
layout(
2929
:f_bavail, :uint64,
3030
:f_bfree, :uint64,
@@ -38,7 +38,7 @@ class Statvfs < FFI::Struct
3838
:f_fsid, :ulong,
3939
:f_namemax, :ulong
4040
)
41-
elsif RbConfig::CONFIG['host'] =~ /sunos|solaris/i
41+
elsif /sunos|solaris/i.match?(RbConfig::CONFIG['host'])
4242
layout(
4343
:f_bsize, :ulong,
4444
:f_frsize, :ulong,
@@ -55,7 +55,7 @@ class Statvfs < FFI::Struct
5555
:f_fstr, [:char, 32],
5656
:f_filler, [:ulong, 16]
5757
)
58-
elsif RbConfig::CONFIG['host'] =~ /i686/i
58+
elsif /i686/i.match?(RbConfig::CONFIG['host'])
5959
layout(
6060
:f_bsize, :ulong,
6161
:f_frsize, :ulong,

lib/facter/resolvers/aix/filesystem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def read_vtf_file(fact_name)
1818
return if file_content.empty?
1919

2020
file_content = file_content.map do |line|
21-
next if line =~ /#|%/ # skip lines that are comments or defaultvfs line
21+
next if /#|%/.match?(line) # skip lines that are comments or defaultvfs line
2222

2323
line.split(' ').first
2424
end

lib/facter/resolvers/aix/memory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def execute_svmon(fact_name)
2525

2626
result.each_line do |line|
2727
@fact_list[:system] = populate_system(line, pagesize) if line.include?('memory')
28-
@fact_list[:swap] = populate_swap(line, pagesize) if line =~ /pg\sspace/
28+
@fact_list[:swap] = populate_swap(line, pagesize) if /pg\sspace/.match?(line)
2929
end
3030

3131
@fact_list[fact_name]

lib/facter/resolvers/aix/mountpoints.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def read_mount(fact_name)
1919
@fact_list[:mountpoints] = {}
2020
output = Facter::Core::Execution.execute('mount', logger: log)
2121
output.split("\n").drop(2).map do |line|
22-
next if line =~ /procfs|ahafs/
22+
next if /procfs|ahafs/.match?(line)
2323

2424
add_mount_points_fact(line)
2525
end
@@ -40,7 +40,7 @@ def add_mount_points_fact(line)
4040
def retrieve_sizes_for_mounts
4141
output = Facter::Core::Execution.execute('df -P', logger: log)
4242
output.split("\n").drop(1).map do |line|
43-
next if line =~ /-\s+-\s+-/
43+
next if /-\s+-\s+-/.match?(line)
4444

4545
mount_info = line.split("\s")
4646
mount_info[3] = translate_to_bytes(mount_info[3])

lib/facter/resolvers/aix/networking.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def build_bindings(name, ip, mask_length, is_ipv4)
4343
def populate_with_mtu_and_mac!(interfaces)
4444
output = Facter::Core::Execution.execute('netstat -in', logger: log)
4545
output.each_line do |line|
46-
next if line =~ /Name\s/
46+
next if /Name\s/.match?(line)
4747

4848
info = line.split("\s")
4949
interface_name = info[0]

lib/facter/resolvers/aix/partitions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def populate_from_lslv(name)
5050
}
5151
mount = info_hash['MOUNT POINT']
5252
label = info_hash['LABEL']
53-
part_info[:mount] = mount unless %r{N/A} =~ mount
54-
part_info[:label] = label.strip unless /None/ =~ label
53+
part_info[:mount] = mount unless %r{N/A}.match?(mount)
54+
part_info[:label] = label.strip unless /None/.match?(label)
5555
part_info
5656
end
5757

lib/facter/resolvers/lspci.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def lspci_command(fact_name)
2626
end
2727

2828
def retrieve_vm(output)
29-
output.each_line { |line| REGEX_VALUES.each { |key, value| return value if line =~ /#{key}/ } }
29+
output.each_line { |line| REGEX_VALUES.each { |key, value| return value if /#{key}/.match?(line) } }
3030

3131
nil
3232
end

lib/facter/resolvers/networking.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def extract_mac(raw_data, parsed_interface_data)
7171
end
7272

7373
def extract_dhcp(interface_name, raw_data, parsed_interface_data)
74-
return unless raw_data =~ /status:\s+active/
74+
return unless /status:\s+active/.match?(raw_data)
7575

7676
result = Facter::Core::Execution.execute("ipconfig getoption #{interface_name} " \
7777
'server_identifier', logger: log)

lib/facter/resolvers/open_vz.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read_proc_status
2929
parts = line.split("\s")
3030
next unless parts.size.equal?(2)
3131

32-
next unless /^envID:/ =~ parts[0]
32+
next unless /^envID:/.match?(parts[0])
3333

3434
@fact_list[:id] = parts[1]
3535

lib/facter/resolvers/os_release.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ def fill_fact_list(pairs)
6363
def process_version_id
6464
return unless @fact_list[:version_id]
6565

66-
@fact_list[:version_id] = "#{@fact_list[:version_id]}.0" unless @fact_list[:version_id] =~ /\./
66+
@fact_list[:version_id] = "#{@fact_list[:version_id]}.0" unless /\./.match?(@fact_list[:version_id])
6767
end
6868

6969
def process_id
7070
return unless @fact_list[:id]
7171

72-
@fact_list[:id] = 'opensuse' if @fact_list[:id] =~ /opensuse/i
72+
@fact_list[:id] = 'opensuse' if /opensuse/i.match?(@fact_list[:id])
7373
end
7474

7575
def process_name

lib/facter/resolvers/selinux.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def selinux_mountpoint
2626
mountpoint = ''
2727

2828
output.each_line do |line|
29-
next unless line =~ /selinuxfs/
29+
next unless /selinuxfs/.match?(line)
3030

3131
mountpoint = line.split("\s")[1]
3232
break
@@ -54,8 +54,8 @@ def read_selinux_config
5454
file_lines = Facter::Util::FileHelper.safe_readlines('/etc/selinux/config')
5555

5656
file_lines.map do |line|
57-
@fact_list[:config_mode] = line.split('=').last.strip if line =~ /^SELINUX=/
58-
@fact_list[:config_policy] = line.split('=').last.strip if line =~ /^SELINUXTYPE=/
57+
@fact_list[:config_mode] = line.split('=').last.strip if /^SELINUX=/.match?(line)
58+
@fact_list[:config_policy] = line.split('=').last.strip if /^SELINUXTYPE=/.match?(line)
5959
end
6060

6161
!file_lines.empty? ? true : false

lib/facter/resolvers/solaris/filesystems.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def read_sysdef_file(fact_name)
1818

1919
file_content = Facter::Core::Execution.execute('/usr/sbin/sysdef', logger: log)
2020
files = file_content.split("\n").map do |line|
21-
line.split('/').last if line =~ /^fs\.*/
21+
line.split('/').last if /^fs\.*/.match?(line)
2222
end
2323

2424
@fact_list[:file_systems] = files.compact.sort.join(',')

lib/facter/resolvers/virt_what.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ def determine_xen(output)
2828
return unless xen_info
2929

3030
xen_info = xen_info.to_s
31-
return 'xenu' if xen_info =~ /xen-domu/
32-
return 'xenhvm' if xen_info =~ /xen-hvm/
33-
return 'xen0' if xen_info =~ /xen-dom0/
31+
return 'xenu' if /xen-domu/.match?(xen_info)
32+
return 'xenhvm' if /xen-hvm/.match?(xen_info)
33+
return 'xen0' if /xen-dom0/.match?(xen_info)
3434
end
3535

3636
def determine_other(output)
3737
values = output.split("\n")
3838
other_vm = values.first
3939
return unless other_vm
4040

41-
return 'zlinux' if other_vm =~ /ibm_systemz/
42-
return retrieve_vserver if other_vm =~ /linux_vserver/
41+
return 'zlinux' if /ibm_systemz/.match?(other_vm)
42+
return retrieve_vserver if /linux_vserver/.match?(other_vm)
4343
return (values - ['redhat']).first if values.include?('redhat')
4444

4545
other_vm
@@ -53,7 +53,7 @@ def retrieve_vserver
5353
parts = line.split("\s")
5454
next unless parts.size.equal?(2)
5555

56-
next unless parts[0] =~ /^s_context:|^VxID:/
56+
next unless /^s_context:|^VxID:/.match?(parts[0])
5757
return @fact_list[:vserver] = 'vserver_host' if parts[1] == '0'
5858

5959
return @fact_list[:vserver] = 'vserver'

lib/facter/resolvers/windows/virtualization.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def determine_hypervisor_by_manufacturer(comp)
4242
manufacturer = comp.Manufacturer
4343
if comp.Model =~ /^Virtual Machine/ && manufacturer =~ /^Microsoft/
4444
'hyperv'
45-
elsif manufacturer =~ /^Xen/
45+
elsif /^Xen/.match?(manufacturer)
4646
'xen'
47-
elsif manufacturer =~ /^Amazon EC2/
47+
elsif /^Amazon EC2/.match?(manufacturer)
4848
'kvm'
4949
else
5050
'physical'

lib/facter/resolvers/xen.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def detect_domains
4545
return if output.empty?
4646

4747
output.each_line do |line|
48-
next if line =~ /Domain-0|Name/
48+
next if /Domain-0|Name/.match?(line)
4949

5050
domain = line.match(/^([^\s]*)\s/)
5151
domain = domain&.captures&.first

lib/facter/util/facts/windows_release_finder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def find_release(input)
1313
description = input[:description]
1414
kernel_version = input[:kernel_version]
1515

16-
if version =~ /10.0/
16+
if /10.0/.match?(version)
1717
check_version_10_11(consumerrel, kernel_version)
1818
else
1919
check_version_6(version, consumerrel) || check_version_5(version, consumerrel, description) || version
@@ -47,7 +47,7 @@ def check_version_6(version, consumerrel)
4747
end
4848

4949
def check_version_5(version, consumerrel, description)
50-
return unless version =~ /5.2/
50+
return unless /5.2/.match?(version)
5151
return 'XP' if consumerrel
5252

5353
description == 'R2' ? '2003 R2' : '2003'

lib/facter/util/linux/dhcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def search_dhclient_leases(interface_name)
4545

4646
lease_files.select do |file|
4747
content = Facter::Util::FileHelper.safe_read("#{dir}#{file}", nil)
48-
next unless content =~ /interface.*#{interface_name}/
48+
next unless /interface.*#{interface_name}/.match?(content)
4949

5050
dhcp = content.match(/dhcp-server-identifier ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)
5151
return dhcp[1] if dhcp

0 commit comments

Comments
 (0)