logoalt Hacker News

ecshaferyesterday at 4:40 PM1 replyview on HN

ruby has the convention of ! for dangerous destructive or mutating methods. This is something that I wish would spread around a bit.

For example:

# Original array

array = [1, 2, 3]

# Using map (non-destructive)

new_array = array.map { |x| x * 2 }

# new_array is [2, 4, 6]

# array is still [1, 2, 3] (unchanged)

# Using map! (destructive)

array.map! { |x| x * 2 }

# array is now [2, 4, 6] (modified in-place)


Replies

wiseowiseyesterday at 4:44 PM

> convention

Is the keyword. Anything that should never be broken isn’t a convention. There’s no better convention than compiler error.

show 1 reply