Re: Truncate an array

SOME_ARRAY = [1, 2, 3]

  1. SOME_ARRAY.each_index{|i| SOME_ARRAY[i].nil}.compact!
  2. SOME_ARRAY.slice!(1…0)

=> SOME_ARRAY == []

SOME_ARRAY = []

Regards,

Dan

This communication is the property of Qwest and may contain confidential
or
privileged information. Unauthorized use of this communication is
strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and
destroy
all copies of the communication and any attachments.

I’d thought about that too. This is bad for two reasons.

  1. SOME_ARRAY is ‘supposedly’ a constant, in that ruby complains if
    you assign to it more than once.

  2. We didn’t really truncate the array, just made a new empty one.

SOME_ARRAY = [1,2,3]
a = SOME_ARRAY
SOME_ARRAY = []
p a
=> [1, 2, 3]

As opposed to something like:
a = SOME_ARRAY
SOME_ARRAY.length.times{SOME_ARRAY.shift}
p a
=> []

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs