monkeyatwar monkeyatwar -5 years ago 129
Ruby Question

How to use map! in ruby to determine if number is in a range

I'm trying to get map! to substitute out values if they're not in range but I can't get the range to work.

array[0].map! { |x| x != (2..8) ? "foo" : x}

I'm working with a 2d array that will have numbers and I need to replace anything not in range within the first array with another static value so it can be later sorted out.

The array I'm working on will look something like this

[ "bar", "2", "3", "baz"]

Which needs to be converted to look like

[ "foo", "2", "3", "foo" ]

Answer Source


array[0].map! { |x| (2..8).include?(x) ? x : "foo" }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download