Intro to Ruby - Arrays

Overview
The following code demonstrates the basics of Ruby arrays and builds upon examples of strings, numbers, and ranges previously discussed. Be sure to download the code and play with arrays more yourself.
Code
# Basic initialization. The array size is printed to show that the array is new.
puts "01 Initialization\n\n"
a = Array.new
puts a.size
# Another way to initialize an array but with less typing. The array size is printed to show that the array is new.
puts "\n02 Alternative Initialization\n\n"
a = []
puts a.size
# An array can also be initialized via a range.
puts "\n03 Range Array\n\n"
puts (1..5).to_a
# An array can be initialized with comma separated values (in this case, an array of strings).
puts "\n04 String Array\n\n"
puts ["red", "green", "blue"]
# Another string array this time using the %W delimiter (which is basically using double quote notation).
# HINT: Use the %w delimiter for single quotes.
puts "\n05 Another String Array\n\n"
puts %W(red hot minute)
# Spend a little time playing with the following. Since a string is essentially an array of characters,
# you can always grab specific characters at will. The outputs various characters pulled from the
# alphabet string using arrays, ranges, and methods.
puts "\n06 Grabbing Specific Values\n\n"
alphabet = ('a'..'z').to_a
puts alphabet[1]
puts "
puts alphabet[0..5]
puts "
puts alphabet.at(2)
# The following initializes an array and then appends the word "end" to the original array.
puts "\n07 Appending\n\n"
a = %W(some random text)
puts a << "end"
# Using the above array, we remove the first element of the array.
puts "\n08 Shifting\n\n"
a.shift
puts a
# Using the above array, we remove the last element of the array.
puts "\n09 Popping\n\n"
a.pop
puts a
# Using what is left of the above array, we create a new array (b) and add them both together.
puts "\n10 Addition\n\n"
b = [1, 2]
puts a + b
Output
01 Initialization
0
02 Alternative Initialization
0
03 Range Array
1
2
3
4
5
04 String Array
red
green
blue
05 Another String Array
red
hot
minute
06 Grabbing Specific Values
b
a
b
c
d
e
f
c
07 Appending
some
random
text
end
08 Shifting
random
text
end
09 Popping
random
text
10 Addition
random
text
1
2
Download
The code above is provided as a Ruby script so that you can quickly execute all the examples above if you like. Just download, unzip, and type the following to execute all examples via the command line: ruby 04-arrays.rb.
What’s Next
Hashes
Article Series - Intro to Ruby
- Intro to Ruby - Getting Started
- Intro to Ruby - Strings
- Intro to Ruby - Numbers
- Intro to Ruby - Ranges
- Intro to Ruby - Arrays
- Intro to Ruby - Hashes
- Intro to Ruby - Variables and Scopes
- Intro to Ruby - Methods
- Intro to Ruby - Blocks
- Intro to Ruby - Classes
- Intro to Ruby - Modules
- Intro to Ruby - Conditionals
- Intro to Ruby - Loops
- Intro to Ruby - Exceptions
- Intro to Ruby - Testing
- Intro to Ruby - Next Steps
No comments yet.
Leave a comment
You must be logged in to post a comment.
Search
Categories
- Adventures
(112)
- Announcements
(36)
- Business
(19)
- Electronics
(21)
- Epicurean
(10)
- Games
(3)
- Literature
(1)
- Mechanical
(4)
- Meetups
(18)
- Movies
(2)
- Music
(26)
- Photography
(1)
- Services
(27)
- Software
(132)