Reputation: 13
I am new to programming and need some advise on the layout/structure of a program. Can you advise if this is the correct way to structure a program in ruby?
This program moves files from an SD card to named folder and gives the option to name the files.
Dir.chdir '/home/moot/pickaxe/pics' #replace this with destination directory
$pic_names = Dir['/media/moot/**/*.{CR2,cr2,JPG,jpg}'] #replace this with sd card directory
def folder
puts "Enter the folder name"
while $folder_name = gets.chomp
if File.exist?("#{$folder_name}")
puts "This folder already exists, choose a unique name"
else
require 'fileutils'
FileUtils::mkdir_p "/home/moot/pickaxe/pics/#{$folder_name}"
break
end
end
end
def files
puts "Do you want to name the files? Answer y/n"
while question = gets.chomp
if question == "y"
puts "What do you want to call them?"
$file_name = gets.chomp
print "Downloading #{$pic_names.length} Files: "
pic_number = 1
$pic_names.each do |name|
print 'ˁ˚ᴥ˚ˀ '
new_name = if pic_number < 10
"#{$file_name} 0#{pic_number} .CR2"
else
"#{$file_name} #{pic_number} .CR2"
end
require 'fileutils'
include FileUtils
cp(name, new_name)
pic_number = pic_number + 1
end
break
elsif question == "n"
print "Downloading #{$pic_names.length} Files: "
$pic_names.each do |name|
print 'ˁ˚ᴥ˚ˀ '
new_dir = "/home/moot/pickaxe/pics/#{$folder_name}"
require 'fileutils'
include FileUtils
cp(name, new_dir)
end
break
else
puts "Please answer y/n"
end
end
end
folder
Dir.chdir "/home/moot/pickaxe/pics/#{$folder_name}"
files
puts "Move Complete."
Upvotes: 1
Views: 37
Reputation: 10763
Ruby conventions generally promote shorter methods, as this improves readability, testability, etc. (Though in the end it's up to you as programmer to decide what's best to accomplish the task at hand.)
One way you might achieve this is to think about your problem in English or other human language first, and break it down into its component parts.
In other words, what specific steps are involved to solve the problem?
Those steps will likely correspond to methods, and perhaps entities--embodied as Ruby classes or modules--that model your problem domain (in fancy programmer-speak).
Practice makes perfect, and coding is the best practice, but it also helps to read code created by others.
Upvotes: 1