Someone ELI5 the difference between .sort and .sort! please.

I have read that .sort, returns a sorted array while leaving the original array alone, and .sort!, modifies the actual array. but can someone explain what that actually means?

2 thoughts on “Someone ELI5 the difference between .sort and .sort! please.”

  1. irb(main):001:0> arr = [1,3,5,4,2]
    => [1, 3, 5, 4, 2]
    irb(main):002:0> arr.sort
    => [1, 2, 3, 4, 5]
    irb(main):003:0> arr
    => [1, 3, 5, 4, 2]
    irb(main):004:0> arr.sort!
    => [1, 2, 3, 4, 5]
    irb(main):005:0> arr
    => [1, 2, 3, 4, 5]

    Reply

Leave a Comment