You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the result of select, reject, uniq, sort, sort_by, map, flatten, and reverse is assigned back to the variable it operates on, prefer the mutable version of the method, i.e.
# badlist=list.select(&:odd?)list=list.reject{ |x| x > 42}list=list.uniqlist=list.sortlist=list.sort_by(&:name)# goodlist.select!(&:odd?)list.reject!{ |x| x > 42}list.uniq!list.sort!list.sort_by!(&:name)
This could also be a performance cop since mutating in-place can have performance advantages.
The text was updated successfully, but these errors were encountered:
This could substantially reduce allocations/copies, but it's quite a can of worms.
The transformation is equivalent only if list isn't shared. This is hard enough in a statically-typed language with good lifetime analysis, but even harder in a dynamic language like Ruby.
When the result of
select
,reject
,uniq
,sort
,sort_by
,map
,flatten
, andreverse
is assigned back to the variable it operates on, prefer the mutable version of the method, i.e.This could also be a performance cop since mutating in-place can have performance advantages.
The text was updated successfully, but these errors were encountered: