SlideShare a Scribd company logo
1 of 11
Collections & Iterators With Hashes & Arrays
Iterators: Conditional Looping “while” allows us to loop through code while a set condition is true x = 1 while x < 10 	puts x.to_s + “ iteration” x += 1 end
Times 5.times { puts “hello” } 5.times { |num| puts “hi”+num.to_s} 99.times do |beer_num|  		puts "#{beer_num} bottles of beer” end 99.times do  puts "some bottles of beer” end
Creating a new array x = [1, 2, 3, 4] => [1, 2, 3, 4] x = %w(1 2 3 4)  => [“1”, “2”, “3”, “4”] chef = Array.new(3, “bork”) => [“bork”, “bork”, bork”]
Accessing Array Values a = [ "a", "b", "c", "d", "e" ]  a[0] #=> "a” a[2] #=> "c” a[6] #=> nil a[1, 2] #=> ["b", "c”] a[1..3] #=> ["b", "c", "d”] a[1…3] #=> ["b", "c"]
Operations on Arrays [ 1, 2, 3 ] * 3  => [1, 2, 3, 1, 2, 3, 1, 2, 3] [ 1, 2, 3 ].join(“,”) ,[object Object],[ 1, 2, 3 ] + [ 4, 5 ]  => [1, 2, 3, 4, 5] [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] => [3, 3, 5] [ 1, 2 ] << "c" << "d" << [ 3, 4 ]  => [1, 2, "c", "d", [3, 4]]
Creating a Hash h = { "a" => 100, "b" => 200 } h[“a”] h = { 1 => “a”, “b” => “hello” } h[1]
Operations on Hashes: Merge h1 = { "a" => 100, "b" => 200 }  => {"a"=>100, "b"=>200} h2 = { "b" => 254, "c" => 300 } =>{"b"=>254, "c"=>300} h3 = h1.merge(h2) => {"a"=>100, "b"=>254, "c"=>300} h1 => {"a"=>100, "b"=>200} h1.merge!(h2) => {"a"=>100, "b"=>254, "c"=>300}
Operations on Hashes h = { "a" => 100, "b" => 200 }  h.delete("a”) h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 } letters = h.keys  h = { "a" => 100, "b" => 200, "c" => 300 }  numbers = h.values
Each >> superheroes = ["catwoman", "batman",  "wonderwoman"] >> superheroes.each { | s | puts "#{ s } save me!" } catwoman save me! batman save me! wonderwomansave me! >> dogs = ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want = [] >> dogs.each do |dog|  >?	dogs_i_want.push(dog) if dog != "fluffy"  >? end >> dogs => ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want => ["fido", "fifi", "rex"]
Map superheroes = ["catwoman", "batman", "wonderwoman"] >> superheroes.map { |s| s.upcase }  => ["CATWOMAN", "BATMAN", "WONDERWOMAN"] Shorter (available in Rails or Ruby 1.8.7+):  >> superheroes.map(&:upcase)  => ["CATWOMAN", "BATMAN", ”WONDERWOMAN”]

More Related Content

What's hot

Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
pugpe
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful Soup
Jim Chang
 

What's hot (20)

Ruby nooks & crannies
Ruby nooks & cranniesRuby nooks & crannies
Ruby nooks & crannies
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
 
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Grails queries
Grails   queriesGrails   queries
Grails queries
 
Grokking regex
Grokking regexGrokking regex
Grokking regex
 
The Chain Rule, Part 2
The Chain Rule, Part 2The Chain Rule, Part 2
The Chain Rule, Part 2
 
P2 2017 python_strings
P2 2017 python_stringsP2 2017 python_strings
P2 2017 python_strings
 
{:from => 'Java', :to => 'Ruby'}
{:from => 'Java', :to => 'Ruby'}{:from => 'Java', :to => 'Ruby'}
{:from => 'Java', :to => 'Ruby'}
 
Extending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::UtilExtending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::Util
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181
 
2 BytesC++ course_2014_c2_ flow of control
2 BytesC++ course_2014_c2_ flow of control 2 BytesC++ course_2014_c2_ flow of control
2 BytesC++ course_2014_c2_ flow of control
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful Soup
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
轻量级文本工具集
轻量级文本工具集轻量级文本工具集
轻量级文本工具集
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 

Similar to Ruby Language: Array, Hash and Iterators

2 Slides Conditionals Iterators Blocks Hashes Arrays
2 Slides   Conditionals Iterators Blocks Hashes Arrays2 Slides   Conditionals Iterators Blocks Hashes Arrays
2 Slides Conditionals Iterators Blocks Hashes Arrays
liahhansen
 
Gareth hayes. non alphanumeric javascript-php and shared fuzzing
Gareth hayes. non alphanumeric javascript-php and shared fuzzingGareth hayes. non alphanumeric javascript-php and shared fuzzing
Gareth hayes. non alphanumeric javascript-php and shared fuzzing
Yury Chemerkin
 
Unit8
Unit8Unit8
Unit8
md751
 
Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
Wen-Tien Chang
 
Template Haskell Tutorial
Template Haskell TutorialTemplate Haskell Tutorial
Template Haskell Tutorial
kizzx2
 

Similar to Ruby Language: Array, Hash and Iterators (19)

Intro To Ruby
Intro To RubyIntro To Ruby
Intro To Ruby
 
2 Slides Conditionals Iterators Blocks Hashes Arrays
2 Slides   Conditionals Iterators Blocks Hashes Arrays2 Slides   Conditionals Iterators Blocks Hashes Arrays
2 Slides Conditionals Iterators Blocks Hashes Arrays
 
Swift Study #2
Swift Study #2Swift Study #2
Swift Study #2
 
Gareth hayes. non alphanumeric javascript-php and shared fuzzing
Gareth hayes. non alphanumeric javascript-php and shared fuzzingGareth hayes. non alphanumeric javascript-php and shared fuzzing
Gareth hayes. non alphanumeric javascript-php and shared fuzzing
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
The Ring programming language version 1.5.2 book - Part 44 of 181
The Ring programming language version 1.5.2 book - Part 44 of 181The Ring programming language version 1.5.2 book - Part 44 of 181
The Ring programming language version 1.5.2 book - Part 44 of 181
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
 
Unit8
Unit8Unit8
Unit8
 
Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
 
Template Haskell Tutorial
Template Haskell TutorialTemplate Haskell Tutorial
Template Haskell Tutorial
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
Ip lab
Ip labIp lab
Ip lab
 
Writing your own programming language to understand Ruby better - Euruko 2011
Writing your own programming language to understand Ruby better - Euruko 2011Writing your own programming language to understand Ruby better - Euruko 2011
Writing your own programming language to understand Ruby better - Euruko 2011
 
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 

More from Sarah Allen

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 

Recently uploaded

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Ruby Language: Array, Hash and Iterators

  • 1. Collections & Iterators With Hashes & Arrays
  • 2. Iterators: Conditional Looping “while” allows us to loop through code while a set condition is true x = 1 while x < 10 puts x.to_s + “ iteration” x += 1 end
  • 3. Times 5.times { puts “hello” } 5.times { |num| puts “hi”+num.to_s} 99.times do |beer_num| puts "#{beer_num} bottles of beer” end 99.times do puts "some bottles of beer” end
  • 4. Creating a new array x = [1, 2, 3, 4] => [1, 2, 3, 4] x = %w(1 2 3 4) => [“1”, “2”, “3”, “4”] chef = Array.new(3, “bork”) => [“bork”, “bork”, bork”]
  • 5. Accessing Array Values a = [ "a", "b", "c", "d", "e" ] a[0] #=> "a” a[2] #=> "c” a[6] #=> nil a[1, 2] #=> ["b", "c”] a[1..3] #=> ["b", "c", "d”] a[1…3] #=> ["b", "c"]
  • 6.
  • 7. Creating a Hash h = { "a" => 100, "b" => 200 } h[“a”] h = { 1 => “a”, “b” => “hello” } h[1]
  • 8. Operations on Hashes: Merge h1 = { "a" => 100, "b" => 200 } => {"a"=>100, "b"=>200} h2 = { "b" => 254, "c" => 300 } =>{"b"=>254, "c"=>300} h3 = h1.merge(h2) => {"a"=>100, "b"=>254, "c"=>300} h1 => {"a"=>100, "b"=>200} h1.merge!(h2) => {"a"=>100, "b"=>254, "c"=>300}
  • 9. Operations on Hashes h = { "a" => 100, "b" => 200 } h.delete("a”) h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 } letters = h.keys h = { "a" => 100, "b" => 200, "c" => 300 } numbers = h.values
  • 10. Each >> superheroes = ["catwoman", "batman", "wonderwoman"] >> superheroes.each { | s | puts "#{ s } save me!" } catwoman save me! batman save me! wonderwomansave me! >> dogs = ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want = [] >> dogs.each do |dog| >? dogs_i_want.push(dog) if dog != "fluffy" >? end >> dogs => ["fido", "fifi", "rex", "fluffy"] >> dogs_i_want => ["fido", "fifi", "rex"]
  • 11. Map superheroes = ["catwoman", "batman", "wonderwoman"] >> superheroes.map { |s| s.upcase } => ["CATWOMAN", "BATMAN", "WONDERWOMAN"] Shorter (available in Rails or Ruby 1.8.7+): >> superheroes.map(&:upcase) => ["CATWOMAN", "BATMAN", ”WONDERWOMAN”]

Editor's Notes

  1. 5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
  2. Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
  3. IRBif you go off the array it will be nil
  4. join is cool because it makes a string for youshovel operatormultidimensional array
  5. Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  6. merge takes the value from the second hashmerge! changes h1
  7. you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value