jackneedshelp jackneedshelp - 1 year ago 74
Ruby Question

How to get max and min values of an array with spaces?

I have the following array.

fourvalue = [" ", 16, " ", 6]

How would I get the max and min of this array?

You can do this if there weren't null strings but I just can't figure out how if there were.

fourvalue = [2,6,8,1,19]
puts fourvalue.max
puts fourvalue.min

I have a double array that makes an 10x10 grid. Each point has four surrounding points so I made an array to contain those to figure out which one will be the highest and lowest but sometimes it has null strings. Probably not the most efficient but this is how I code and I rather not have cheat-sheet.

The null strings always contain one space.

Answer Source
min, max = fourvalue.reject { |x| x == " " }.minmax

However, note that having this mix of spaces and integers is quite dubious. At some previous stage you could replace those spaces with a nil and then use compact instead of reject, much cleaner.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download