Slide 1

Slide 1 text

RUBYCONF 2021 Ariel Caplan • @amcaplan • amcaplan.ninja The Audacious Array

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

[]

Slide 4

Slide 4 text

CatWalk

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

https://github.com/amcaplan/catwalk

Slide 7

Slide 7 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • • Ruby programmer since 2014 • Backend Developer at • Find me online: @amcaplan • https://amcaplan.ninja Hi! I’m Ariel Caplan.

Slide 8

Slide 8 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[0] #=> "basics"

Slide 9

Slide 9 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • • Ordered collection of values • Values can be any Ruby object • Get/set by index (zero-based) • Literal syntax: • Explicit syntax: • Setting by index: • Strict fetching: • Standard retrieval: • Descriptive Information: What is an Array? [1, 2, 3] Array.new(3, :a) #=> [:a, :a, :a] array[2] #=> "val2" array[3] #=> nil array[1..2] #=> ["val1", "val2"] array[1..-1] #=> ["val1", “val2”] array.first #=> "val0" array.last #=> “val2" array.fetch(3) # IndexError (index 3 outside of array bounds: -3...3) array.size #=> 4 array.empty? #=> false array[2] = "val2"

Slide 10

Slide 10 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[1] #=> "adding and removing elements”

Slide 11

Slide 11 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Front and Back array = [1, 2, 3, 4, 5] # adding items array.push(6) #=> [1, 2, 3, 4, 5, 6] array << 7 #=> [1, 2, 3, 4, 5, 6, 7] array.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6, 7] # removing items array.pop #=> 7 array #=> [0, 1, 2, 3, 4, 5, 6] array.shift #=> 0 array #=> [1, 2, 3, 4, 5, 6]

Slide 12

Slide 12 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end

Slide 13

Slide 13 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new

Slide 14

Slide 14 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) :a

Slide 15

Slide 15 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) s.push(:b) :a :b

Slide 16

Slide 16 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) s.push(:b) s.push(:c) :a :b :c

Slide 17

Slide 17 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) s.push(:b) s.push(:c) s.pop #=> :c :a :b

Slide 18

Slide 18 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) s.push(:b) s.push(:c) s.pop #=> :c s.pop #=> :b :a

Slide 19

Slide 19 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Stack: #push and #pop class Stack def initialize @stack = [] end def push(item) @stack.push(item) end def pop @stack.pop end end s = Stack.new s.push(:a) s.push(:b) s.push(:c) s.pop #=> :c s.pop #=> :b s.pop #=> :a

Slide 20

Slide 20 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end

Slide 21

Slide 21 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new

Slide 22

Slide 22 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) :a

Slide 23

Slide 23 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) q.push(:b) :a :b

Slide 24

Slide 24 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) q.push(:b) q.push(:c) :a :b :c

Slide 25

Slide 25 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) q.push(:b) q.push(:c) q.pop #=> :a :b :c

Slide 26

Slide 26 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) q.push(:b) q.push(:c) q.pop #=> :a q.pop #=> :b :c

Slide 27

Slide 27 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: #push and #shift class Queue def initialize @queue = [] end def push(item) @queue.push(item) end def pop @queue.shift end end q = Queue.new q.push(:a) q.push(:b) q.push(:c) q.pop #=> :a q.pop #=> :b q.pop #=> :c

Slide 28

Slide 28 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D E

Slide 29

Slide 29 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D E B C D E F

Slide 30

Slide 30 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? B C D E F

Slide 31

Slide 31 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? B C D E F

Slide 32

Slide 32 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D E

Slide 33

Slide 33 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D

Slide 34

Slide 34 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D F?

Slide 35

Slide 35 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? F? A B C D

Slide 36

Slide 36 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? F? A B C D

Slide 37

Slide 37 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? F? A B C D

Slide 38

Slide 38 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D F

Slide 39

Slide 39 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Queue: why not #unshift? A B C D F SPACE!!! NO SPACE !

Slide 40

Slide 40 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • A Surprising Benchmark require 'benchmark/ips' Benchmark.ips do |x| x.report('Array#unshift') do array = (1..100).to_a 100_000.times { |i| array.unshift(i) } end x.report('Array#push') do array = (1..100).to_a 100_000.times { |i| array.push(i) } end x.compare! end

Slide 41

Slide 41 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • A Surprising Benchmark Warming up -------------------------------------- Array#unshift 17.000 i/100ms Array#push 19.000 i/100ms Calculating ------------------------------------- Array#unshift 181.828 (± 6.6%) i/s - 918.000 in 5.076970s Array#push 187.124 (± 8.0%) i/s - 931.000 in 5.011387s Comparison: Array#push: 187.1 i/s Array#unshift: 181.8 i/s - same-ish: difference falls within error " ❓

Slide 42

Slide 42 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • A Moment of Gratitude Ufuk Kayserilioglu Peter Zhu Kevin Newton

Slide 43

Slide 43 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • jk lol, #unshift is fine

Slide 44

Slide 44 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • jk lol, #unshift is fine

Slide 45

Slide 45 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • jk lol, #unshift is fine

Slide 46

Slide 46 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • jk lol, #unshift is fine

Slide 47

Slide 47 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • jk lol, #unshift is fine $

Slide 48

Slide 48 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[2] #=> "order and randomness”

Slide 49

Slide 49 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Creating randomness array = [1, 2, 3, 4, 5] array.shuffle #=> [5, 1, 4, 3, 2] array.shuffle #=> [1, 3, 5, 4, 2] array.shuffle #=> [5, 3, 2, 4, 1] array.sample #=> 4 array.sample(3) => [5, 2, 1]

Slide 50

Slide 50 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Cleaning up array = [2, 5, 6, 7, 10, 3, 1, 4, 8, 9] array.sort #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] array.sort_by { |i| -i } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Slide 51

Slide 51 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Mixing it up array = [1, 2, 3] array.combination(2).to_a #=> [[1, 2], [1, 3], [2, 3]] array.permutation(2).to_a #=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]

Slide 52

Slide 52 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Mixing it up array = [1, 2, 3] array.combination(2).to_a #=> [[1, 2], [1, 3], [2, 3]] array.permutation(2).to_a #=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]

Slide 53

Slide 53 text

ITEMS = ["chair", "sofa", "bed", "pillow", "cushion", "blanket"] def related_items ITEMS.sample(3) end RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Displaying random items ITEM_GROUPS = ITEMS.permutation(3).cycle def related_items_group ITEM_GROUPS.next end

Slide 54

Slide 54 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[3] #=> "interpretation"

Slide 55

Slide 55 text

Array includes Enumerable!

Slide 56

Slide 56 text

Array includes Enumerable! Iteration #each #each_cons #each_slice Introspection #all? #any? #none? #one? Selection #filter/#select #find/#detect #reject Extrapolation #map/#collect #reduce/#inject #each_with_object

Slide 57

Slide 57 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Iteration [1, 2, 3].each do |int| puts int end # This will print: # 1 # 2 # 3 [1, 2, 3, 4, 5, 6].each_slice(3).to_a => [[1, 2, 3], [4, 5, 6]]

Slide 58

Slide 58 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Introspection [1, 3, 5].all?(&:odd?) #=> true

Slide 59

Slide 59 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Selection [1, 2, 3, 4, 5].select { |i| i >= 4 } #=> [4, 5] [1, 2, 3, 4, 5].find { |i| i >= 4 } #=> 4

Slide 60

Slide 60 text

[1, 2, 3].map { |i| i * 2 } #=> [2, 4, 6] ["a", "b", "c"].inject("start") { |accum, str| accum + ' ' + str } #=> "start a b c" RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Extrapolation "start" => "start a" => "start a b" => "start a b c"

Slide 61

Slide 61 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Minimum and Maximum array = [2, 5, 6, 7, 10, 3, 1, 4, 8, 9] array.min #=> 1 array.max #=> 10 array.minmax #=> [1, 10]

Slide 62

Slide 62 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Minimum and Maximum words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"] words.minmax #=> ["brown", "the"]

Slide 63

Slide 63 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Adding it All Up array = [2, 5, 6, 7, 10, 3, 1, 4, 8, 9] array.sum #=> 55 array.sum { |i| i ** 2 } #=> 385

Slide 64

Slide 64 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Shopping Cart catnip = { name: "catnip", price: 6.99 } milk = { name: "milk", price: 2.49 } hat = { name: "Cat in the Hat hat", price: 15.99 } shopping_cart = [[catnip, 2], [milk, 3], [hat, 1]]

Slide 65

Slide 65 text

most_common_item = shopping_cart. max_by { |product, quantity| quantity }. first[:name] #=> "milk" RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Shopping Cart catnip = { name: "catnip", price: 6.99 } milk = { name: "milk", price: 2.49 } hat = { name: "Cat in the Hat hat", price: 15.99 } shopping_cart = [[catnip, 2], [milk, 3], [hat, 1]] most_expensive_item = shopping_cart. max_by { |product, quantity| product[:price] }. first[:name] #=> "Cat in the Hat hat"

Slide 66

Slide 66 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Shopping Cart catnip = { name: "catnip", price: 6.99 } milk = { name: "milk", price: 2.49 } hat = { name: "Cat in the Hat hat", price: 15.99 } shopping_cart = [[catnip, 2], [milk, 3], [hat, 1]] order_total = shopping_cart. sum { |product, quantity| product[:price] * quantity } #=> 37.44

Slide 67

Slide 67 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[4] #=> "The Second Dimension”

Slide 68

Slide 68 text

array_2d = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps

Slide 69

Slide 69 text

array_2d.reverse #=> [ # [7, 8, 9], # [4, 5, 6], # [1, 2, 3] # ] array_2d = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps array_2d.map(&:reverse) #=> [ # [3, 2, 1], # [6, 5, 4], # [9, 8, 7] # ]

Slide 70

Slide 70 text

array_2d.transpose #=> [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps array_2d = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]

Slide 71

Slide 71 text

array_2d.transpose #=> [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps array_2d = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] array_2d.transpose.reverse #=> [ # [3, 6, 9], # [2, 5, 8], # [1, 4, 7] # ] array_2d.transpose.map(&:reverse) #=> [ # [7, 4, 1], # [8, 5, 2], # [9, 6, 3] # ]

Slide 72

Slide 72 text

array_2d.rotate #=> [ # [4, 5, 6], # [7, 8, 9], # [1, 2, 3] # ] array_2d = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps

Slide 73

Slide 73 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Maps class PuzzlePiece def initialize(array) @array = array end def rotated(i) piece = @array i.times do piece = piece.transpose.reverse end piece end def orientations (0..3).map { |i| rotated(i) } end end

Slide 74

Slide 74 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[5] #=> "Search"

Slide 75

Slide 75 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Autocomplete CAT_BREEDS = CatBreed.pluck(:name) user_input = Regexp.escape(params[:breed]) #=> "pe" CAT_BREEDS.grep(Regexp.new(user_input, 'i')) #=> ["European Shorthair", "LaPerm", "Persian", "Peterbald", "Serrade Petit"] CAT_BREEDS.grep(Regexp.new('\b' + user_input, 'i')) #=> ["Persian", "Peterbald", "Serrade Petit"] CAT_BREEDS.grep(Regexp.new('\A' + user_input, 'i')) #=> ["Persian", "Peterbald"]

Slide 76

Slide 76 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Filtering by Regexp CAT_BREEDS.grep(/ /) #=> ["American Bobtail", "American Curl", ... "Ukrainian Levkoy", "York Chocolate"] CAT_BREEDS.grep_v(/ /) #=> ["Abyssinian", "Aegean", ... "Thai", "Tonkinese", "Toyger"]

Slide 77

Slide 77 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #


Slide 78

Slide 78 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #


Slide 79

Slide 79 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #


Slide 80

Slide 80 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #


Slide 81

Slide 81 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #
 array.bsearch { |order| order.date >= "2021-01-01" }
 #=> #

Slide 82

Slide 82 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #
 array.bsearch { |order| order.date >= "2021-01-01" }
 #=> # true

Slide 83

Slide 83 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #
 array.bsearch { |order| order.date >= "2021-01-01" }
 #=> # true false

Slide 84

Slide 84 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First array = [
 #,
 #,
 #,
 #,
 #,
 #,
 #,
 #
 ]
 array.find { |order| order.date >= "2021-01-01" }
 #=> #
 array.bsearch { |order| order.date >= "2021-01-01" }
 #=> # true false true

Slide 85

Slide 85 text

array = [
 #,
 #,
 #, #,
 #,
 #,
 #,
 #
 ]
 index = array.bsearch_index { |order| order.date >= "2021-01-01" } #=> 3 array[index..-1] #=> [#,…] RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Finding the First }

Slide 86

Slide 86 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[6] #=> "Arrays as Sets"

Slide 87

Slide 87 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Sets array = [1, 1, 2, 2, 3, 3, 3] array.uniq #=> [1, 2, 3] array - [2, 3] #=> [1, 1]

Slide 88

Slide 88 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Sets set1 = [1, 2, 3] set2 = [2, 3, 4, 5] set1 | set2 #=> [1, 2, 3, 4, 5] set1 & set2 #=> [2, 3]

Slide 89

Slide 89 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Arrays as Sets author.posts.replace(new_posts_array) # under the hood, performs a diff # let current_posts = author's current posts reload(current_posts & new_posts_array) delete(current_posts - new_posts_array) insert(new_posts_array - current_posts)

Slide 90

Slide 90 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • •Arrays are indexed collections of elements •Randomize and sort •Iterate/Interpret one-by-one or together •2D arrays can be used as maps •Rudimentary search capabilities •Set-like functionality What have we covered?

Slide 91

Slide 91 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • talk[7] #=> “CatWalk"

Slide 92

Slide 92 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Random Map map_items = [:%] * 200 + [:&] * 4 + [:'] * 4 + [:(] * 2 + [')', '*', '+', ','] SIDE_SIZE = 25 map = SIDE_SIZE.times. map { map_items.sample(SIDE_SIZE) } map_items = [:%] * 200 + [:&] * 4 + [:'] * 4 + [:(] * 2 + [')', '*', '+', ',']

Slide 93

Slide 93 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Random Map [ [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :& , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :& , :( , :% , :% , :% , :% , :% , :% , :, , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :& , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :+ , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% , :) , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :, , :% , :* , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% , :( , :* , :% , :) ], [:% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :* , :% , :% , :% , :% , :% , :% , :( , :% , :% , :% , :% , :% , :% , :% , :& , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :& , :% , :) , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :, , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :& , :% , :% , :% , :% , :% , :% , :% , :& ], [:% , :, , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:+ , :% , :% , :% , :% , :& , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :( , :' , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :+ , :( , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :( , :% , :% , :% , :% , :% , :% , :% , :% , :% ], [:% , :' , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :( , :% , :% , :% , :% , :% , :% , :% , :% , :' , :% , :% , :% , :% , :% , :% ], [:% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :% , :( , :% , :% , :% ] ]

Slide 94

Slide 94 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Map Shuffling , state[:map] = state[:map]. flatten. shuffle. each_slice(SIDE_SIZE). to_a

Slide 95

Slide 95 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Map Shuffling , map = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] map.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9] .shuffle #=> [4, 9, 7, 6, 8, 1, 5, 3, 2] .each_slice(3).to_a #=> [ # [4, 9, 7], # [6, 8, 1], # [5, 3, 2] # ]

Slide 96

Slide 96 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Map Movement case current_symbol when :& state[:map] = state[:map].transpose.map(&:reverse) else state[:map].each { |line| line.rotate!(-1) } end

Slide 97

Slide 97 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Controls # move up = move the map down state[:map].rotate!(-1) # move down = move the map up state[:map].rotate!(1)

Slide 98

Slide 98 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items # Poor performance as array grows def active_items(temp_items) temp_items.select(&:active?) end

Slide 99

Slide 99 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end

Slide 100

Slide 100 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) ,

Slide 101

Slide 101 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) ,

Slide 102

Slide 102 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , -

Slide 103

Slide 103 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , -

Slide 104

Slide 104 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , - .

Slide 105

Slide 105 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , - .

Slide 106

Slide 106 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , - . .

Slide 107

Slide 107 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1] end ) * * + ) * * + , ) , - . .

Slide 108

Slide 108 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end

Slide 109

Slide 109 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) ,

Slide 110

Slide 110 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) ,

Slide 111

Slide 111 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , .

Slide 112

Slide 112 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , .

Slide 113

Slide 113 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , - .

Slide 114

Slide 114 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , - .

Slide 115

Slide 115 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , - . .

Slide 116

Slide 116 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , - . .

Slide 117

Slide 117 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items) current_temp_item_index = temp_items.bsearch_index(&:active?) temp_items[current_temp_item_index..-1].select(&:active?) end * + * + , ) , - . .

Slide 118

Slide 118 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Tracking Active Items def active_items(temp_items:, **) earliest_active_start_time = Time.now - Item::MAX_DURATION current_temp_item_index = temp_items.bsearch_index { |i| i.start_time > earliest_active_start_time } temp_items[current_temp_item_index..-1].select(&:active?) end

Slide 119

Slide 119 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • •Multiplicative items implemented with #inject •Cheat codes use a queue-based keylogger •Final score uses #tally CatWalk Miscellany

Slide 120

Slide 120 text

https://github.com/amcaplan/catwalk

Slide 121

Slide 121 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Appreciation Circle array = %w(Alex Ariel Daniel Erez Ira Omer Oren) array.shuffle.cycle.each_cons(2).first(array.size).to_h #=> { # "Erez"=>"Omer", # "Omer"=>"Ira", # "Ira"=>"Alex", # "Alex"=>"Oren", # "Oren"=>"Ariel", # "Ariel"=>"Daniel", # "Daniel"=>"Erez" # }

Slide 122

Slide 122 text

RUBYCONF 2021 • ARIEL CAPLAN • @AMCAPLAN • Please say hi in Discord / real life!