anonyfox and many others are using Hackety Hack to learn how to code.

Hackety Hack is a free program for Windows, Mac OS X and Linux that can teach you how to make games, applications and more and share those programs with your friends. You can ask other budding programmers questions and follow them to see what code they're working on through the Hackety Hack website.

Sign up Learn More

picsorterhelper by anonyfox

#encoding: utf-8

module PicSorterHelper
  
  def get_subdirs
    list = Dir.entries $to
    list.delete "."
    list.delete ".."
    folders = list.map do |f|
      File.directory?($to+"/"+f) ? f : false
    end
    folders.delete false while folders.include? false
    folders
  end
  
  def get_files
    list = Dir.entries $from
    list.delete "."
    list.delete ".."
    fail_list = list.map do |f|
      if File.directory?$from + "/" + f
        f
      elsif f.include? "."
        ex = f.split(".")[-1]
        f unless ex =~ /png/i || ex =~ /gif/i || ex =~ /jpg/i || ex =~ /jpeg/i
      else
        false
      end #if
    end #list.map
    fail_list.delete false while fail_list.include? false
    fail_list.each {|f| list.delete f}
    list
  end
  
  def move_file(name, to)
    FileUtils.mv($from+"/"+name, $to+"/"+to+"/"+name)
  end
  
  def draw_buttons(folders)
    @folder_list.clear do
      folders.each do |f|
        button f do
          pic = @pics[0]
          move_file(pic, f)
          @pics.delete_at(0)
          @i.path = ""
          @i.path = $from+"/"+@pics[0] unless @pics == []
          @p.text = @pics[0]
        end #button
      end #folders.each
      new_button = button "add new category" do
        name = ask("categoryname?")
        name.gsub!(" ", "")
        name.gsub!("/","")
        unless File.exists?($to+"/"+name)
          Dir.mkdir($to+"/"+name)
          @folders.push name
          draw_buttons(@folders)
        end #unless
      end #button
    end #clear
  end
  
end