Skip to content

Instantly share code, notes, and snippets.

@pootsbook
Created November 1, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pootsbook/3995378 to your computer and use it in GitHub Desktop.
Save pootsbook/3995378 to your computer and use it in GitHub Desktop.
PHP -> Ruby: Listing Directory
// Removed the HTML for readability
<?php
$Directory = opendir("../");
while($entryName = readdir($Directory)) {
$dirArray[] = $entryName;
}
closedir($Directory);
$jCount = count($dirArray);
sort($dirArray);
for($j=0; $j<$jCount; $j++) {
$file = $dirArray[$j];
if ( substr($file,0,1) != "." ) {
echo $file;
echo filetype($file);
echo filesize($file);
}
}
?>
# collect the file_names in the given directory
file_names = Dir.entries("../")
# remove hidden files from the resultant array
file_names.reject! {|d| d.chars.first.eql?('.') }
# sort the array
file_names.sort!
# turn each file_name string into a File object
files = file_names.map {|f| File.new(f) }
# iterate through the files and print the required information
files.each do |file|
puts file.path
puts File.extname(file.path)
puts file.size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment