Skip to content

Commit 21258fc

Browse files
committed
Array.find replaces iteration with early return
1 parent 29382c0 commit 21258fc

File tree

4 files changed

+9
-28
lines changed

4 files changed

+9
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88
### Added
9-
109
- Provide an `itoa` function. It is present in Arduino's runtime environment but not on most (all?) host systems because itoa is not a portable standard function.
1110

1211
### Changed
12+
- Simplified the use of `Array.each` with a return statement; it's now simply `Array.find`
1313

1414
### Deprecated
1515

lib/arduino_ci/arduino_downloader.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ def self.autolocated_executable
3232
# if it exists. I'm not sure why we would have both, but if we did
3333
# a force install then let's make sure we actually use it.
3434
locations = [self.force_installed_executable, self.existing_executable]
35-
locations.each do |loc|
36-
next if loc.nil?
37-
next unless File.exist? loc
38-
39-
return loc
40-
end
41-
nil
35+
locations.find { |loc| !loc.nil? && File.exist?(loc) }
4236
end
4337

4438
# The autolocated directory of the installation
@@ -49,13 +43,7 @@ def self.autolocated_installation
4943
# if it exists. I'm not sure why we would have both, but if we did
5044
# a force install then let's make sure we actually use it.
5145
locations = [self.force_install_location, self.existing_installation]
52-
locations.each do |loc|
53-
next if loc.nil?
54-
next unless File.exist? loc
55-
56-
return loc
57-
end
58-
nil
46+
locations.find { |loc| !loc.nil? && File.exist?(loc) }
5947
end
6048

6149
# The path to the directory of an existing installation, or nil

lib/arduino_ci/arduino_downloader_osx.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,17 @@ def self.force_install_location
2727
# @param Array<string> a list of places to look
2828
# @return [string]
2929
def self.find_existing_arduino_dir(paths)
30-
paths.each do |path|
31-
return path if File.exist? path
32-
end
33-
nil
30+
paths.find(&File.method(:exist?))
3431
end
3532

3633
# An existing Arduino file in one of the given directories, or nil
3734
# @param Array<string> a list of places to look for the executable
3835
# @return [string]
3936
def self.find_existing_arduino_exe(paths)
40-
paths.each do |path|
37+
paths.find do |path|
4138
exe = File.join(path, "MacOS", "Arduino")
42-
return exe if File.exist? exe
39+
File.exist? exe
4340
end
44-
nil
4541
end
4642

4743
# The path to the directory of an existing installation, or nil

lib/arduino_ci/arduino_downloader_windows.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def self.existing_installation
8181
# @return [string]
8282
def self.existing_executable
8383
arduino_reg = 'SOFTWARE\WOW6432Node\Arduino'
84-
Win32::Registry::HKEY_LOCAL_MACHINE.open(arduino_reg) do |reg|
84+
Win32::Registry::HKEY_LOCAL_MACHINE.open(arduino_reg).find do |reg|
8585
path = reg.read_s('Install_Dir')
8686
exe = File.join(path, "arduino_debug.exe")
87-
return exe if File.exist? exe
87+
File.exist? exe
8888
end
8989
rescue
9090
nil
@@ -93,10 +93,7 @@ def self.existing_executable
9393
# The executable Arduino file in a forced installation, or nil
9494
# @return [string]
9595
def self.force_installed_executable
96-
exe = File.join(self.force_install_location, "arduino_debug.exe")
97-
return nil if exe.nil?
98-
99-
exe
96+
File.join(self.force_install_location, "arduino_debug.exe")
10097
end
10198

10299
end

0 commit comments

Comments
 (0)