[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Add files to array matching pattern

Leon Bogaert

10/4/2007 11:09:00 PM

Hi all,

I wanted to simply add files to an array. The files must match a
pattern. I'm just starting with ruby so I really have to find my way in
it. I tried something like:

dir = "/home/leon/images"
test = Array.new
test << Dir.new(dir).entries.each { |x| if 1 == 1 }

Then I wanted to add a sort of if to that last line with a block ( I
think I have to use a block).
I tried this in irb, but it doesn't give an error.

What am I doing wrong here?

Thanks in advance!
--
Posted via http://www.ruby-....

7 Answers

Leon Bogaert

10/4/2007 11:23:00 PM

0

Tried this:

dir = "/home/leon/"; test = Array.new; test =
Dir.new(dir).entries.collect { |x| x == 'filezilla' ? x : next }

But that didn't work also. It gives back:

[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil]
--
Posted via http://www.ruby-....

Leon Bogaert

10/4/2007 11:36:00 PM

0

This would work:

i = 0; dir = "/home/leon/"; test = Array.new; test =
Dir.new(dir).entries.map! { |x| x =~ /filezilla$/ ? x : next };
test.compact

Is the .compact needed? Can't I get rid of that?
--
Posted via http://www.ruby-....

Rob Biedenharn

10/4/2007 11:48:00 PM

0

On Oct 4, 2007, at 7:35 PM, Leon Bogaert wrote:

> This would work:
>
> i = 0; dir = "/home/leon/"; test = Array.new; test =
> Dir.new(dir).entries.map! { |x| x =~ /filezilla$/ ? x : next };
> test.compact
>
> Is the .compact needed? Can't I get rid of that?

test = Dir.entries("/home/leon").grep(/filezilla$/)

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



7stud 7stud

10/5/2007 12:04:00 AM

0

Leon Bogaert wrote:
> This would work:
>
> i = 0; dir = "/home/leon/"; test = Array.new; test =
> Dir.new(dir).entries.map! { |x| x =~ /filezilla$/ ? x : next };
> test.compact
>
> Is the .compact needed? Can't I get rid of that?

You can do something like this:

d = "./"
matching_files = Dir.open(d) do |dir|
dir.find_all { |filename| filename == "r8test.rb"}
end
p matching_files


collect puts the return values from the block into an array. With
find_all, if the block returns true, then the current filename is added
to the array.
--
Posted via http://www.ruby-....

7stud 7stud

10/5/2007 3:25:00 AM

0

Leon Bogaert wrote:
> Tried this:
>
> dir = "/home/leon/"; test = Array.new; test =
> Dir.new(dir).entries.collect { |x| x == 'filezilla' ? x : next }
>
> But that didn't work also. It gives back:
>
> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
> nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
> nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
> nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
> nil, nil, nil, nil, nil, nil, nil, nil, nil]

The reason you get that output is:

1) collect adds the value returned by the block to a results array.

2) There is no file named "filezilla" in the directory /home/leon

3) That means the value of the block is always whatever is produced by
the statement: next. If you try this:

[1, 2, 3].each {|x| puts next}

you'll get the error

r9test.rb:1: void value expression

I think that means that the next statement is one of the few statements
in ruby that doesn't produce a value. So what is the value of the block
when the next statement gets executed as the last statement? If you
try this:

results = [1, 2, 3].collect {|x| next}
p results

the output is:

[nil, nil, nil]

Look familiar? Apparently, a block returns nil by default if the
statements in the block do not produce any values.
--
Posted via http://www.ruby-....

7stud 7stud

10/5/2007 3:41:00 AM

0

Leon Bogaert wrote:
> This would work:
>
> i = 0; dir = "/home/leon/"; test = Array.new; test =
> Dir.new(dir).entries.map! { |x| x =~ /filezilla$/ ? x : next };
> test.compact
>
> Is the .compact needed? Can't I get rid of that?

Also, next is used skip the rest of the code in a block. For instance:

(1..4).each do |x|
next if x==2
puts x
end

--output:--
1
3
4

On the other hand, using next as the last statement in a block does
nothing:

(1..4).each do |x|
puts x
next
end

--output:---
1
2
3
4

...except screw up the return value of the block:

results = (1..4).collect do |x|
x if x != 2
next
end

p results

--output:---
[nil, nil, nil, nil]

For methods like collect that use the block's return value, the next
statement ruins the results array.
--
Posted via http://www.ruby-....

Robert Klemme

10/5/2007 7:21:00 AM

0

2007/10/5, Leon Bogaert <leon@tim-online.nl>:
> This would work:
>
> i = 0; dir = "/home/leon/"; test = Array.new; test =
> Dir.new(dir).entries.map! { |x| x =~ /filezilla$/ ? x : next };
> test.compact
>
> Is the .compact needed? Can't I get rid of that?

You need it with your code because the map! will map some values to
nil. You probably rather want #select.

files = Dir.new(dir).entries.select { |x| /filezilla$/ =~ x }

But you can have that much easier with

files = Dir["/home/leon/*filezilla"]

Kind regards

robert