[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby is simple? NooooT!

Casimir P

10/24/2007 4:26:00 PM

Hehe whoever said that ruby is not verbose was WRONG!! ;)

I mean look at the code below!

All this just to find subdirectories and to list all the jpg files inside!!
So... NOT!!! :) I hope the code posts well..

#code starts here
imgpath = "img/"
def seekPhotoDirs(path)
require 'find'
photodirs = Array.new

Find.find(path) do |item|
if FileTest.directory?(item)
if item != path #ei haluta tätä samaa hakemistoa
puts indexImages(item)
end
end
end
return photodirs
end
def indexImages(dir)
dir.each { |d|
Dir.entries(d).each { |filu|
nn = filu.to_s
ext = filu.downcase.split(".").last
if ( ext == "png" || ext == "jpg")
puts filu
end
}
}
end
puts seekPhotoDirs(imgpath)
#code ends here

Greetings to whytheluckystiff! Outfoxed, not sourced.

Csmr
--
Casimir Pohjanraito
Art Portfolio: http://csmr.dreamh...
18 Answers

Robert Dober

10/24/2007 4:37:00 PM

0

On 10/24/07, Casimir P <pikselNOSPAMMi@welnospmamho.com> wrote:
> Hehe whoever said that ruby is not verbose was WRONG!! ;)
>
> I mean look at the code below!
>
> All this just to find subdirectories and to list all the jpg files inside!!
> So... NOT!!! :) I hope the code posts well..
>
> #code starts here
> imgpath = "img/"
> def seekPhotoDirs(path)
> require 'find'
> photodirs = Array.new
>
> Find.find(path) do |item|
> if FileTest.directory?(item)
> if item != path #ei haluta tätä samaa hakemistoa
> puts indexImages(item)
> end
> end
> end
> return photodirs
> end
> def indexImages(dir)
> dir.each { |d|
> Dir.entries(d).each { |filu|
> nn = filu.to_s
> ext = filu.downcase.split(".").last
> if ( ext == "png" || ext == "jpg")
> puts filu
> end
> }
> }
> end
> puts seekPhotoDirs(imgpath)
> #code ends here
>
> Greetings to whytheluckystiff! Outfoxed, not sourced.
>
> Csmr
> --
> Casimir Pohjanraito
> Art Portfolio: http://csmr.dreamh...
>
>
Argggh, right now I have to leave the office but I predict that you
will get nice, readable ruby code to do the same in less than 10 lines
*before* I can login again in about three hours.
Folks, do not let me down, pleaaaase ;).

Cheers
Robert


--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Bill Kelly

10/24/2007 4:38:00 PM

0


From: "Casimir P" <pikselNOSPAMMi@welNOSPMAMho.com>
>
> All this just to find subdirectories and to list all the jpg files inside!!

files = Dir["img/**/*.jpg"]


Regards,

Bill



Phlip

10/24/2007 4:41:00 PM

0

> Argggh, right now I have to leave the office but I predict that you
> will get nice, readable ruby code to do the same in less than 10 lines
> *before* I can login again in about three hours.
> Folks, do not let me down, pleaaaase ;).

Should we start with Pathname?

(And note that super-terse code is not a virtue - it should be a
little readable...)

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_latest Model

Casimir P

10/24/2007 4:43:00 PM

0


On Thu, 25 Oct 2007 01:38:15 +0900, Bill Kelly wrote:
> files = Dir["img/**/*.jpg"]

Well technically that does NOT find subdirectories. Call in the
disqualifier dogs!!!!!!

Csmr


>From: "Casimir P" <pikselNOSPAMMi@welNOSPMAMho.com>
>> All this just to find subdirectories and to list all the jpg files
inside!!

--
Casimir Pohjanraito
Art Portfolio: http://csmr.dreamh...

Phlip

10/24/2007 4:46:00 PM

0

>> files = Dir["img/**/*.jpg"]
>
> Well technically that does NOT find subdirectories. Call in the
> disqualifier dogs!!!!!!

Allow me to draw your attention to the **

It sweeps subfolders.

--
Phlip


Casimir P

10/24/2007 4:48:00 PM

0

On Wed, 24 Oct 2007 09:46:23 -0700, Phlip wrote:

>>> files = Dir["img/**/*.jpg"]
>>
>> Well technically that does NOT find subdirectories. Call in the
>> disqualifier dogs!!!!!!
>
> Allow me to draw your attention to the **
>
> It sweeps subfolders.
But supposedly you cant tell whats what now. Its all just a big blur. You
get all the little images but lose the bigger picture.

Its what happens when you dont keep track of the subdirectories.

Csrm
--
Casimir Pohjanraito
Art Portfolio: http://csmr.dreamh...

Bill Kelly

10/24/2007 5:20:00 PM

0


From: "Tim McIntyre" <tmac@easystreet.com>
>
> files = Dir.glob("img/**/*.jpg")
>
> pretty sure that keeps all the sub-folders in tact e.g. dir1/
> img1.jpg, dir2/img1.jgp, dir1/dir3/img1.jpg etc....

Yes indeedy.

From: "Casimir P" <pikselNOSPAMMi@welNOSPMAMho.com>
>
> Well technically that does NOT find subdirectories. Call in the
> disqualifier dogs!!!!!!

WTFOMGLOLN00B!1!!

:)



Lloyd Linklater

10/24/2007 5:24:00 PM

0

Tim McIntyre wrote:
> files = Dir.glob("img/**/*.jpg")
>
> pretty sure that keeps all the sub-folders in tact e.g. dir1/
> img1.jpg, dir2/img1.jgp, dir1/dir3/img1.jpg etc....

I verified that this returns a fully qualified path. Ruby wins again!
--
Posted via http://www.ruby-....

Robert Klemme

10/24/2007 6:13:00 PM

0

On 24.10.2007 18:48, Casimir P wrote:
> On Wed, 24 Oct 2007 09:46:23 -0700, Phlip wrote:
>
>>>> files = Dir["img/**/*.jpg"]
>>> Well technically that does NOT find subdirectories. Call in the
>>> disqualifier dogs!!!!!!
>> Allow me to draw your attention to the **
>>
>> It sweeps subfolders.
> But supposedly you cant tell whats what now. Its all just a big blur. You
> get all the little images but lose the bigger picture.
>
> Its what happens when you dont keep track of the subdirectories.

Frankly, it's quite unclear to me what you want. When I run your script
I see *all* directories printed plus image file names without a path.
If that is what you want, then you can do this in "one" line:

$ ruby -r find -e 'Find.find(".") {|f| if File.directory? f then puts f
elsif /\.(jpg|png)$/i =~ f then puts File.basen
ame(f) end }'

Or, printed a bit more verbose and added handling of multiple base
directories:

require 'find'

ARGV.each do |dir|
Find.find dir do |f|
if File.directory? f
puts f
elsif /\.(?:jpg|png)$/i =~ f
puts File.basename(f)
end
end
end

Still very concise and readable. The complicatedness of the original
code cannot be attributed to Ruby.

Cheers

robert

Wayne Magor

10/24/2007 7:07:00 PM

0

Tim McIntyre wrote:
> files = Dir.glob("img/**/*.jpg")

Why doesn't that seem to work for me on Windows? Is there something
different about it on Windows than on Linux?

ruby -e 'puts Dir.glob("*.txt")' # Works for all .txt files in cur
dir
ruby -e 'puts Dir.glob("/**/*.txt")' # Prints nothing
ruby -e 'puts Dir.glob("**/*.txt")' # Works if done from subdir, not
from C:
Is there something different about being at the top-level directory on
Windows? It's unexpected behavior for me. Why does it operate like
that?
--
Posted via http://www.ruby-....