I'm trying to get very comfortable with all of the array methods and enumerables in Ruby, but I don't understand why some don't mutate and others do. For instance, is there a difference between:
def double(array)
array.map {|x| x * 2}
end
and
def double(array)
return array.map! {|x| x * 2}
end
Also, when I tried to call
b.select{|x| x.even?}
where b is an array of integers, it did not change, but
b = b.select{|x| x.even?} OR
.delete_if
did seem to mutate it.
Is
a.each do |word|
word.capitalize!
end
the same as
a.map do |word|
word.capitalize
end