scmuser created the topic: Short Notes – Ruby Arrays- Insert, Append, length, Index, Removed
Short Notes – Ruby Arrays- Insert, Append, length, Index, Removed
Create a Ruby Array
[code language=”css”]
letters = Array.new
letters = []
letters = Array.new()
letters =
letters = Array.new(3) ( Define how many elements you array would be)
letters = Array.new(3, ‘me rocks’) (Define each element with default values)
[/code]
Accessing Elements
[code language=”css”]
letters[0] – accessing First element
letters[-1] – Accessing last element
letters[-2]
[/code]
Inserting/Adding elements
[code language=”css”]
letters = Array.new() OUTPUT = [“a”, “b”, “c”]
letters.insert(0, 1) OUTPUT [1, “a”, “b”, “c”, “d”]
letters.insert(-1, ‘d’) OUTPUT [1, “a”, “b”, “c”, “d”]
letters << ‘e’ OUTPUT [1, “a”, “b”, “c”, “d”, “e”]
letters.push(‘f’) OUTPUT [1, “a”, “b”, “c”, “d”, “e”, “f”]
[/code]
Removing Elements
[code language=”css”]
letters.pop – Remove the last elements
letters.delete_at(2) – Delete the element of particular index
[/code]
- Best AI tools for Software Engineers - November 4, 2024
- Installing Jupyter: Get up and running on your computer - November 2, 2024
- An Introduction of SymOps by SymOps.com - October 30, 2024