I want to remove the first digit of my array and return the array without the first term
new_array always comes out as just a single number
n is an integer
array = (n.to_s).split(//)
print array
new_array = array.delete_at(0)
puts new_array
Drops first n elements from Array and returns the rest of the elements in an array.
a = [41,42,43,45,46]
=> [41, 42, 43, 45, 46]
a.drop 1
=> [42, 43, 45, 46]
drop doesn't delete elements from the existing array, it returns a new array without those elements.You could use Integer#digits and Object#tap this way:
n = 12345678
position = 3
n.digits.reverse.tap { |ary| ary.delete_at(position) }
#=> [1, 2, 3, 5, 6, 7, 8]
position = 0 remove the firs digit.
You can use array method drop to delete n number of elements from array
arr = [1, 2, 3, 4, 5]
arr.drop 2
=> [3, 4, 5]
ref: https://docs.ruby-lang.org/en/2.0.0/Array.html#method-i-drop
Because delete_at returns the deleted item.
If you want to remove and retrieve at the same time for the first item, you can use -
new_array = array.drop(1)
array(i.e. alteringarray) or do you just want a new array without the 1st element? (i.e. without alteringarray)(n.to_s).split(//)returns an array of characters, not an array of digits.