Learn Ruby: Arrays and Hashes Cheatsheet | Codecademy (2024)

Ruby Hash

In Ruby, a hash is a collection of key-value pairs.

A hash is denoted by a set of curly braces ({}) which contains key-value pairs separated by commas. Each value is assigned to a key using a hash rocket (=>). Calling the hash followed by a key name within brackets grabs the value associated with that key.

profile = {

"name" => "Magnus",

"profession" => "chess player",

"ranking" => 1,

"grandmaster?" => true

}

# "name", "profession", "ranking", and "grandmaster?" are the keys. "Magnus", "chess player", 1 and true are the values.

puts profile["name"] # => Magnus

Ruby Array

In Ruby, an array is an ordered collection of Ruby objects separated by commas and enclosed in []. An array can contain the same or different types of Ruby objects, such as Integers, Strings, Floats, etc. An array can also be empty.

numbers = [1, 2, 3, 4, 5]

#An array of Integers

words = ["See", "Spot", "run"]

#An array of Strings

mixed = ["hello", 5, true, 3.0]

#An array with a String, Integer, Boolean, and Float

empty = []

#An empty array

Ruby Hash New

In Ruby, a hash can be created through literal notation (because we are literally assigning what key=>value pairs we want in the hash) or by assigning a variable equal to Hash.new which generates a new, empty hash.

#Creating a hash through literal notation:

lunch = {

"protein" => "chicken",

"greens" => "lettuce",

"organic?" => true

}

#Creating a hash through Hash.new

lunch = Hash.new

puts lunch # => {}

Ruby Hash Bracket Notation Adding Pairs

In Ruby, a new key-value pair can be added to a hash using bracket notation. The new key is bracketed after the name of the hash and then the value is assigned after the equals sign.

#Bracket notation applies to any hash, regardless of how it was initialized

teammates = Hash.new

teammates["forward"] = "Messi"

salary = {

"starting" => 40000

}

salary["mid-level"] = 60000

Ruby Multidimensional Arrays

In Ruby, arrays can be nested within one another representing multi dimensional arrays. An array can hold another array as if it was like any other Ruby object, such as an Integer or a String.

multi_array = [[0,1,2,3],[4.5, true, "hi"]]

# Accessing the array at index 1

puts multi_array[1] # => [4.5, true, "hi"]

# Accessing the element at index 0 within the array at index 1

puts multi_array[1][0] # => 4.5

Ruby Array Index

In Ruby, each item inside of an array is at a numbered position called an index. The first item is at index 0, the second item is at index 1, and so on. We can access the ith element of an array by putting the index in square brackets after invoking the array’s name; this is known as access by index

example = ["Car", "Boar", 45, 9.9, true]

#For an array named `example`, you can retrieve an item of a particular index by referencing its index.

puts example[2] # => 45

puts example[0] # => Car

Ruby Method .Each

In Ruby, the .each method is used to iterate over arrays and hashes. This allows each element in an array and each key-value pair in a hash to be iterated.

#In this example, the each method iterates over every color in the colors array and prints it to the console.

colors = ["red", "blue", "green", "yellow"]

colors.each { |color| puts color }

#Output

#red

#blue

#green

#yellow

#When iterating over hashes, two placeholder variables are needed to represent each key/value pair.

polygons = {

"pentagon" => 5,

"hexagon" => 6,

"nonagon" => 9

}

polygons.each do |shape, sides|

puts "A #{shape} has #{sides} sides."

end

#Output

#A pentagon has 5 sides.

#A hexagon has 6 sides.

#A nonagon has 9 sides.

Ruby Hash Bracket Notation Value

In Ruby, the values in a hash can be accessed using bracket notation. After the hash name, type the key in square brackets in order to access the value.

my_love = {

"dog" => "Keanu",

"breed" => "Shiba Inu",

"age_in_years" => 1,

}

puts my_love["breed"] # => Shiba Inu

Learn More on Codecademy

Learn Ruby: Arrays and Hashes Cheatsheet | Codecademy (2024)
Top Articles
Wisconsin Voluntary Termination of Parental Rights and Adoption
Bank Deposits Method of Proving Income | Brown Tax, PC
Kmart near me - Perth, WA
Inducement Small Bribe
Visitor Information | Medical Center
Nfr Daysheet
Teenbeautyfitness
David Packouz Girlfriend
Craigslist Dog Kennels For Sale
Fredericksburg Free Lance Star Obituaries
Available Training - Acadis® Portal
Price Of Gas At Sam's
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Kp Nurse Scholars
Lola Bunny R34 Gif
Tripadvisor Napa Restaurants
Naval Academy Baseball Roster
Crossword Help - Find Missing Letters & Solve Clues
Accuweather Minneapolis Radar
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
Bn9 Weather Radar
Top 20 scariest Roblox games
Star Wars Armada Wikia
Is Poke Healthy? Benefits, Risks, and Tips
Intel K vs KF vs F CPUs: What's the Difference?
Bridgestone Tire Dealer Near Me
Elanco Rebates.com 2022
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Flaky Fish Meat Rdr2
Rust Belt Revival Auctions
EST to IST Converter - Time Zone Tool
Reading Craigslist Pa
Manatee County Recorder Of Deeds
Devotion Showtimes Near The Grand 16 - Pier Park
Hellgirl000
A Comprehensive 360 Training Review (2021) — How Good Is It?
Riverton Wyoming Craigslist
Stewartville Star Obituaries
Kenner And Stevens Funeral Home
412Doctors
Aurora Southeast Recreation Center And Fieldhouse Reviews
Lightfoot 247
Barback Salary in 2024: Comprehensive Guide | OysterLink
25100 N 104Th Way
Online TikTok Voice Generator | Accurate & Realistic
Makes A Successful Catch Maybe Crossword Clue
Google Flights Missoula
Joe Bartosik Ms
Ocean County Mugshots
Law Students
Www Extramovies Com
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 6223

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.