length = ar.length
class FileNameSwap
def switch( letters, ordinals, length)
for i in 0..length -1
if File.file?(letters[i])
File.rename(letters[i], ordinals[i])
puts "switched to ordinal"
else
File.rename(ordinals[i], letters[i])
puts "switch to letter"
end
end
end
end
length = ar.length
class FileNameSwap
def switch( letters, ordinals, length)
for i in 0..length -1
if File.file?(letters[i])
File.rename(letters[i], ordinals[i])
puts "switched to ordinal"
else
File.rename(ordinals[i], letters[i])
puts "switch to letter"
end
end
end
end
class FileNameSwap
# I don't know why you chose to make a class for this, but since you did I'll
# at least store the arrays as instance variables
def initialize(letters,ordinals)
@letters=letters
@ordinals=ordinals
end
def switch @letters.zip(@ordinals).each do |letter, ordinal|
if File.file? letter
File.rename(letter, ordinal)
else
File.rename(ordinal, letter)
end
end
end
end
Instead of doing for...in and use i as an index, you could do
letters.each...do...|letter| and there's no index. letter is the
extracted index.
The only problem would be the ordinals[i] which could stay as it is
and note your own iterations (not recommended) or do, instead of
letters.each, do letters.each_index do |i| and little would change.
Both suggestions would rid you of needing the length argument, but to
be honest that's the only real disadvantage to your algorithm. It's
pretty much fine how it is. You should only change it if it's really
hard to find length in my opinion.
class FileNameSwap
# I don't know why you chose to make a class for this, but since you did I'll
# at least store the arrays as instance variables
def initialize(letters,ordinals)
@letters=letters @ordinals=ordinals
end
def switch @letters.zip(@ordinals).each do |letter, ordinal|
if File.file? letter
File.rename(letter, ordinal)
else
File.rename(ordinal, letter)
end
end
end
end