RUBY ITERATORS
IN 15 MINUTES OR LESS
WHAT YOU’LL NEED FROM
PREVIOUS LESSONS
BLOCKS
ARRAYS AND HASHES
RAISE YOUR HAND IF YOU DON’T FEEL
COMFORTABLE WITH ALL OF ...
THE COMMON ITERATORS
.TIMES()
.EACH()
.COLLECT(), .MAP(), .MAP!()
.REDUCE(), .INJECT()
WHAT YOU’LL ITERATE OVER
ARRAYS
HASHES
ANYTHING THAT IMPLEMENTS
“ENUMERABLE”
THE SIMPLEST POSSIBLE EXAMPLE
5.times { puts “hello”}
THE SIMPLEST POSSIBLE EXAMPLE
5.times { puts “hello”}
AN ITERATOR
THE SIMPLEST POSSIBLE EXAMPLE
5.times { puts “hello”}
A BLOCK
AN ITERATOR
A MORE USEFUL EXAMPLE
GIVEN:
some_data = [
{name: ”tim c”},
{name: “larry”},
{name: “lindsey”},
{name: “curly”},
{name: “m...
THE SOLUTION (EACH)
some_data.each do |i|
puts i[:name]
end
WHY?
ITS EASIER TO READ (ONCE YOU’RE USED TO
IT)
YOU’LL MAKE FEWER DUMB MISTAKES
THE NON-ITERATOR WAY
num = some_data.length - 1
for i in 0..num do
puts some_data[i][:name]
end
YOU DECIDE
num = some_data.length - 1
for i in 0..num do
puts some_data[i][:name]
end
some_data.each do |i|
puts i[:name]
...
NEXT PROBLEM
GIVEN:
some_data = [
{name: ”tim c”},
{name: “larry”},
{name: “lindsey”},
{name: “curly”},
{name: “moe”}
]
TH...
THE SOLUTION (MAP)
names = some_data.map do |i|
i[:name]
end
ALSO PERFECTLY GOOD
(COLLECT)
names = some_data.collect do |i|
i[:name]
end
LAST, CALCULATE SOMETHING
GIVEN:
some_data = [
{name: ”tim c”},
{name: “larry”},
{name: “lindsey”},
{name: “curly”},
{name...
THE SOLUTION (REDUCE)
count = some_data.reduce(0)
do |so_far, i|
if i[:name].start_with?(“l”)
so_far = so_far + 1
end
so_f...
PULLING BACK THE CURTAIN
THIS IS LITERALLY ALL THE CODE IN THE
RUBY .EACH() ITERATOR
def each
for 0..self.length - 1 do
yi...
...THERE ARE MORE. BUT YOU’LL
HARDLY EVER USE THEM.
STEP, DOWNTO, UPTO
FIND, FIND_ALL, SELECT, CYCLE
EACH_SLICE
CHUNK, REJ...
SOME REVIEW
WHICH ITERATOR WOULD YOU USE TO....
CALCULATE THE AVERAGE CHARACTER
COUNT IN AN ARRAY OF STRINGS
SOME REVIEW
WHICH ITERATOR WOULD YOU USE TO....
SEND AN EMAIL FOR EACH ADDRESS IN AN
ARRAY OF ADDRESSES
SOME REVIEW
WHICH ITERATOR WOULD YOU USE TO....
SPLIT AN ARRAY OF FULL NAMES (IE. “TIM
CULL”) INTO AN ARRAY OF FIRST NAME/...
EXERCISE
NOW, LET’S ACTUALLY DO THAT LAST ONE
SPLIT AN ARRAY OF FULL NAMES (IE. “TIM
CULL”) INTO AN ARRAY OF FIRST NAME/
L...
Upcoming SlideShare
Loading in …5
×

Ruby iterators

178
-1

Published on

A 15 minute tutorial on Ruby iterators like each(), reduce() and map()

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
178
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby iterators

  1. 1. RUBY ITERATORS IN 15 MINUTES OR LESS
  2. 2. WHAT YOU’LL NEED FROM PREVIOUS LESSONS BLOCKS ARRAYS AND HASHES RAISE YOUR HAND IF YOU DON’T FEEL COMFORTABLE WITH ALL OF THESE...
  3. 3. THE COMMON ITERATORS .TIMES() .EACH() .COLLECT(), .MAP(), .MAP!() .REDUCE(), .INJECT()
  4. 4. WHAT YOU’LL ITERATE OVER ARRAYS HASHES ANYTHING THAT IMPLEMENTS “ENUMERABLE”
  5. 5. THE SIMPLEST POSSIBLE EXAMPLE 5.times { puts “hello”}
  6. 6. THE SIMPLEST POSSIBLE EXAMPLE 5.times { puts “hello”} AN ITERATOR
  7. 7. THE SIMPLEST POSSIBLE EXAMPLE 5.times { puts “hello”} A BLOCK AN ITERATOR
  8. 8. A MORE USEFUL EXAMPLE GIVEN: some_data = [ {name: ”tim c”}, {name: “larry”}, {name: “lindsey”}, {name: “curly”}, {name: “moe”} ] THEN: PRINT EVERYONE’S NAME ON THE SCREEN
  9. 9. THE SOLUTION (EACH) some_data.each do |i| puts i[:name] end
  10. 10. WHY? ITS EASIER TO READ (ONCE YOU’RE USED TO IT) YOU’LL MAKE FEWER DUMB MISTAKES
  11. 11. THE NON-ITERATOR WAY num = some_data.length - 1 for i in 0..num do puts some_data[i][:name] end
  12. 12. YOU DECIDE num = some_data.length - 1 for i in 0..num do puts some_data[i][:name] end some_data.each do |i| puts i[:name] end VS.
  13. 13. NEXT PROBLEM GIVEN: some_data = [ {name: ”tim c”}, {name: “larry”}, {name: “lindsey”}, {name: “curly”}, {name: “moe”} ] THEN: GATHER AN ARRAY OF JUST THE NAMES
  14. 14. THE SOLUTION (MAP) names = some_data.map do |i| i[:name] end
  15. 15. ALSO PERFECTLY GOOD (COLLECT) names = some_data.collect do |i| i[:name] end
  16. 16. LAST, CALCULATE SOMETHING GIVEN: some_data = [ {name: ”tim c”}, {name: “larry”}, {name: “lindsey”}, {name: “curly”}, {name: “moe”} ] THEN: COUNT HOW MANY NAMES START WITH “L”
  17. 17. THE SOLUTION (REDUCE) count = some_data.reduce(0) do |so_far, i| if i[:name].start_with?(“l”) so_far = so_far + 1 end so_far end
  18. 18. PULLING BACK THE CURTAIN THIS IS LITERALLY ALL THE CODE IN THE RUBY .EACH() ITERATOR def each for 0..self.length - 1 do yield self[i] end end
  19. 19. ...THERE ARE MORE. BUT YOU’LL HARDLY EVER USE THEM. STEP, DOWNTO, UPTO FIND, FIND_ALL, SELECT, CYCLE EACH_SLICE CHUNK, REJECT EACH_WITH_INDEX NEARLY EVERYTHING IN ENUMERABLE: HTTP:// WWW.RUBY-DOC.ORG/CORE-2.1.1/ENUMERABLE.HTML
  20. 20. SOME REVIEW WHICH ITERATOR WOULD YOU USE TO.... CALCULATE THE AVERAGE CHARACTER COUNT IN AN ARRAY OF STRINGS
  21. 21. SOME REVIEW WHICH ITERATOR WOULD YOU USE TO.... SEND AN EMAIL FOR EACH ADDRESS IN AN ARRAY OF ADDRESSES
  22. 22. SOME REVIEW WHICH ITERATOR WOULD YOU USE TO.... SPLIT AN ARRAY OF FULL NAMES (IE. “TIM CULL”) INTO AN ARRAY OF FIRST NAME/ LAST NAME TUPLES (IE. [“TIM”,”CULL”])
  23. 23. EXERCISE NOW, LET’S ACTUALLY DO THAT LAST ONE SPLIT AN ARRAY OF FULL NAMES (IE. “TIM CULL”) INTO AN ARRAY OF FIRST NAME/ LAST NAME TUPLES (IE. [“TIM”,”CULL”])

×