Ruby Programming Tutorial - Lesson 13 - Arrays and use of loops with arrays

in #ruby7 years ago (edited)

expert1.jpg

Introduction

An array is a data structure that represents a list of values, called elements. Arrays let you store multiple values in a single variable. This can condense and organize your code, making it more readable and maintainable. And because arrays are objects with their own methods, they can make working with lists of data much easier.

Lets take an example

bilal = ["cute", "lovely", "adorable", "pakistani", "programmer"]

puts "Using puts with array"
puts bilal

puts "Using print with array"
print bilal

Here is executed code sample..

Now lets learn to access individual values from arrays

bilal = ["cute", "lovely", "adorable", "pakistani", "programmer"]

puts bilal[0]
puts bilal[1]
puts bilal[2]
puts bilal[3]
puts bilal[4]


Another way to store values inside of arrays,

days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}

# These two methods of writing arrays serve same purpose. you can use both methods interchangeably 

days =  ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Lets take another example.
Here is how you use arrays to hold records in them ..

# Create an array that holds a user's information and Display individual value using EACH loop 


user = [
    "Bilal Haider",
     27,
    "Programmer",
    "Single",
    "1000 STEEM"
]

user.each do |value|
   puts value
end

You can also store Arrays inside of arrays ...
We are extending our previous array and adding array to user's Array that contains user's Crypto balances,

# Create an array that holds a user's information and Display individual value using for .. in loop 


user = [
    "Bilal Haider",
     27,
    "Programmer",
    "Single",
    "1000 STEEM",
    ["1.5 BTC", "10 LTC", "1000 BERY"]
]

for value in user do
  puts value
end

Lets now learn about methods which you can perform on arrays

record = [
    "Bilal Haider",
     27,
    "Programmer",
    "Single",
    "1000 STEEM",
    ["1.5 BTC", "10 LTC", "1000 BERY"]
]

## Returns the length of array, in our case array contains 6 items
puts record.length

## Returns the item's index,  "Bilal Haider" being at 0th index
puts record.index("Single")

## Returns first element of the Array
puts record.first

## Returns Last element of the Array
puts record.last

## Note that record.last and record[-1] return us same result. 
puts record[-1]

Congratulations, you have learned about a data structured that is used in all the languages,
It will help you a lot in writing your programs. its widely used in computer programs.

Here is a reference which contains more cool usage examples of arrays, i could not cover all the usages in this article,
but we will explore more usages in future examples..
Reference : https://www.digitalocean.com/community/tutorials/how-to-work-with-arrays-in-ruby

Sort:  

nice post, upvoted plz if you have time visit my blog ,, best of luck

Thanks for comment :) Appreciated

Leaving comments asking for votes, follows, or other self promotional messages could be seen as spam.

Your Reputation Could be a Tasty Snack with the Wrong Comment!

Thank You! ⚜

You got a 1.50% upvote from @postpromoter courtesy of @bilal-haider!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!

dlive-broadcast
your ppst all so good. i like it @bilal-haider