Fellow Stranger Fellow Stranger - 1 year ago 78
Ruby Question

Where to put exclamation mark when methods are chained

I have the following array and methods:

array = ["1", 0, "a", ""]
array.reject(&:blank?).map(&:to_i).reject{|i| i == 0}

If I'd like to save changes in place utilizing the exclamation mark, e.g.
, should the mark be placed after every method or otherwise - where?

Ven Ven
Answer Source

Explanation mark are not modifiers.

array.reject!(&:blank?).map!(&:to_i).reject!{|i| i == 0}

However, this code is subtly wrong. From reject! docs:

returns nil if no changes were made.

Whoops! This could break your whole chain. Instead, you're supposed to use delete_if.

array.delete_if(&:blank?).map!(&:to_i).delete_if{|i| i == 0}

Yes, it's confusing it doesn't have a bang, but it does modify in-place. From the docs:

The array is changed instantly every time the block is called, not after the iteration is over.

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