Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

for ($i=0; $i<3; $i++) { echo "hello"; }

Slide 7

Slide 7 text

for ($i=0; $i < $array.length; $i++) { echo $array[$i]; }

Slide 8

Slide 8 text

$result = []; for ($i=0; $i<$array.length; $i++) { if ($array[$i] % 2 == 0) { $result[] = $array[$i]; } } return $result;

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

3.times do puts "hello" end

Slide 11

Slide 11 text

array.each do |item| puts item end

Slide 12

Slide 12 text

array.select do |n| n.even? end

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

(5..7).each do |n| puts n end # 5 # 6 # 7

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

march = Date.new(2022, 3) sept = Date.new(2022, 9) (march..sept).each do |date| # ... end # March 1, March 2, March 3, ...

Slide 22

Slide 22 text

(march..sept).select do |date| date.day == 1 end.each do |date| # ... end # March 1, April 1, May 1, ...

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

class Month def initialize(months) @months = months end end

Slide 26

Slide 26 text

class Month def self.from_parts(year, months) new (year * 12) + months end end

Slide 27

Slide 27 text

march = Month.from_parts(2022, 3) sept = Month.from_parts(2022, 9) (march..sept).each do |month| # ... end # ArgumentError: bad value for range

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

class Month def <=>(other) months <=> other.months end end

Slide 32

Slide 32 text

class Month def succ Month.new(months.succ) end end

Slide 33

Slide 33 text

march = Month.from_parts(2022, 3) sept = Month.from_parts(2022, 9) (march..sept).each do |month| # ... end # March 2022, April 2022, May 2022, ...

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

class CompositeFile def initialize(files) @files = files end end

Slide 45

Slide 45 text

monte_cristo = CompositeFile.new(chapters) monte_cristo.find { |line| line.include? "Villefort" } # Undefined method `find` for CompositeFile

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

def each @files.each do |file| file.each do |line| yield line end end end

Slide 49

Slide 49 text

monte_cristo.each do |line| puts line end

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

class CompositeFile include Enumerable # ... end

Slide 52

Slide 52 text

monte_cristo.find do |line| line.include? "Villefort" end # => "“Could you ever have credited such a thing, my dear Danglars?” asked M. Morrel, as, on his return to the port for the purpose of gleaning fresh tidings of Dantès, from M. de Villefort, the assistant procureur, he overtook his supercargo and Caderousse."

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

array.each_with_index(1) do |item, index| puts "#{index}. #{item}" end

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

array.map.with_index(1) do |item, index| "#{index}. #{item}" end # => ["1. first", "2. second", ... ]

Slide 59

Slide 59 text

array = [1, 2, 3] array.cycle(3).with_index(1).select do |item, index| index.even? && item.even? end # => [[2, 2], [2, 8]]

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

[1, 2, 3].map # => #

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

3.times #=> # 5.upto(10) #=> # [1, 2, 3].each #=> #

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

enum = [1, 2].to_enum(:each) enum.next # => 1 enum.next # 2 enum.next # raise StopIteration

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

percentages = Enumerator.produce { rand(100) } percentages.next # => 7 percentages.next # => 51

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

numbers = [1, 2, 3].cycle numbers.take(4) # => [1, 2, 3, 1]

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

class CompositeBook include Enumerable def each # by line end def each # by word end end

Slide 78

Slide 78 text

class CompositeBook def each_line # ... end def each_word # ... end end

Slide 79

Slide 79 text

class CompositeBook def each_line return to_enum(:each_line) unless block_given # actual looping end end

Slide 80

Slide 80 text

monte_cristo.each_line.find { |line| ... } monte_cristo.each_word.map { |word| ... }

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content