How to be cool with map

So I’ve got this file-checking script:

def check
ok = true
for_every_app do |app,app_info|
ok = File.exists?(app_info[:root])
end
ok
end

And it has a bug, which is that if it evaluates a good file after a
bad file, it’ll return true for the whole list.

So here’s my suggested change:

def check
ok = true
for_every_app.values.each do |app_info|
ok = File.exists?(app_info[:root]) if ok
end
ok
end

Because if we have one false value, then the whole test is
unnecessary. It’s basically like a short-circuit operator.

But I think there’s a better way to do it with map.

Something like

File.exists?(for_every_app.values.map{|x| x}

I think I’m way off, though. How do I get that right?


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Hi –

On Thu, 23 Aug 2007, Giles B. wrote:

So I’ve got this file-checking script:

def check
ok = true
for_every_app do |app,app_info|

Do you mean for there to be an .each in there?

def check
But I think there’s a better way to do it with map.

Something like

File.exists?(for_every_app.values.map{|x| x}

I think I’m way off, though. How do I get that right?

You could do:

def check
for_every_app.all? {|app,info| File.exists?(info[:root])}
end

David

Faster, because it breaks the loop as soon as possible, and
less LOC:

def check
for_every_app.values.find do |app_info|
not File.exists?(app_info[:root])
end ? false : true
end

You may want to skip the “? false : true” part, but that breaks
the encapsulation.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

On Aug 22, 2:54 pm, “Giles B.” [email protected] wrote:

And it has a bug, which is that if it evaluates a good file after a
end
I think I’m way off, though. How do I get that right?


Giles B.

Blog:http://gilesbowkett.blogspot.com
Portfolio:http://www.gilesgoatboy.org
Tumblelog:http://giles.tumblr.com/

How about inject?

for_every_app.inject(true) {|good,x| good = File.exists?(x) if good}

Alle mercoledì 22 agosto 2007, Erik V. ha scritto:

the encapsulation.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

This is shorter, and breaks as soon as a non-existing file is found

def check
!for_every_app.values.any?{|f| !File.exist?(f)}
end

Stefano

Hi –

On Thu, 23 Aug 2007, Stefano C. wrote:

You may want to skip the “? false : true” part, but that breaks
the encapsulation.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

This is shorter, and breaks as soon as a non-existing file is found

def check
!for_every_app.values.any?{|f| !File.exist?(f)}
end

You can save yourself the double negative:

for_every_app.values.all?{|f| File.exist?(f)}

And even save the creation of an intermediate array of values:

for_every_app.all?{|a,b| File.exist?(b)}

But I repeat myself :slight_smile: (See my earlier post.)

David

On Aug 22, 4:34 pm, Kaldrenon [email protected] wrote:

end
for_every_app.values.each do |app_info|

Something like
Tumblelog:http://giles.tumblr.com/

How about inject?

for_every_app.inject(true) {|good,x| good = File.exists?(x) if good}

I guess this would be for_every_app.values.inject(true) {|good,x| good
= File.exists?(x) if good}

But I think the idea was clear.

On Aug 22, 2007, at 12:54 PM, Giles B. wrote:

So I’ve got this file-checking script:

def check
ok = true
for_every_app do |app,app_info|
ok = File.exists?(app_info[:root])
end
ok
end

this sends alarm bells ringing to me. i would expect, from that
small sample of code, that i could do

a_ok = apps.all?{|app| app.info.root.exist?}

which is to say apps is a method or collection, an app knows it’s
info, an info object knows it’s root, and that root is a Pathname
object so therefore has an exist method. which would obviate the
need for a method :wink:

2cts.

a @ http://drawohara.com/

Alle mercoledì 22 agosto 2007, David A. Black ha scritto:

David

You’re right. Somehow, I thought that all? needed to iterate over all
the
values and any? didn’t. Of course, now I realized that, just as any?
will
stop at the first positive result, all? will stop at the first negative
result.

Stefano

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs