Ruby OOP convention/layout I am new to programming and I have just written my first program. It takes files from an SD card and copies then into a new named folder and has the option to name the files themselves or simply move them into the named folder.

As I am new I am unsure of the correct convention in terms of layout or structure of a program. Would a 'real' program look like this and how should it be structured?

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 &lt; 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."
I started to write a detailed review, but in middle of it I realised that best advise I could give you looking at this code is to pick up some good Ruby book and work through it carefully, paying attention to how examples are formed. You seem to understand basics of how programming works, but your code lacks proper usage of many basic concepts, like arguments, returning, modules etc. Explaining them in detail here seems pointless, it's not a place for Ruby tutorials. You should focus on that basics before you move to object oriented design/conventions.

Still, I'll post what I already wrote as advices for you.



I don't really see any need to use OO structure in this code. It definately could still be written better though.

Indentation

Your way of indenting makes code very hard to read. Compare:

  $pic_names.each do |name|
  print 'ˁ˚ᴥ˚ˀ  '
  new_name = if pic_number &lt; 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


and:

$pic_names.each do |name|
  print 'ˁ˚ᴥ˚ˀ  '
  new_name = 
    if pic_number &lt; 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


Ruby style guide contains some usefull examples on how you should indent your code to make it nice to read. Generally every block means 1 level of indentation (2 spaces).

Require

require 'fileutils'
include FileUtils


You only need to require 'fileutils' once, at top of your file. include also doesn't need to be reapated, instead generally it should be at top of a module.

Avoid globals

Instead of putting result of your methods in global variables, return from them. I.e:

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}"
      return folder_name
    end 
  end
end


And than:

folder_name = folder
Dir.chdir "/home/moot/pickaxe/pics/#{folder_name}"


This is much more flexible.