Lies, Damned Lies, and Substrings
of 82
/82
-
Author
haseeb-qureshi -
Category
Software
-
view
479 -
download
2
Embed Size (px)
Transcript of Lies, Damned Lies, and Substrings
- 1. Lies, Damned Lies, and Substrings HASEEB QURESHI SOFTWARE ENGINEER @
- 2. Let me tell you a story about a time Ruby lied to me.
- 3. A coworker and I were arguing about an algorithm. Him Me
- 4. It started with a classic problem: How to generate all of the substrings of a string?
- 5. Hello H, e, l, l, o He, el, ll, lo Hel, ell, llo Hell, ello Hello Hello i = 0 j = 3
- 6. Hello H, e, l, l, o He, el, ll, lo Hel, ell, llo Hell, ello Hello Hello i = 1 j = 4 Each substring is defined by a unique start and end index.
- 7. def substrings(str) (0...str.length).each_with_object([]) do |i, subs| (i...str.length).each do |j| subs "a&cdefghijklmnopqrstuvwxyz" # DEBUG: len = 26 debug.display_string(str2) # DEBUG: RString = 0x7fa2a304fae0 # DEBUG: ptr = 0x7fa2a2f50b11 -> "bcdefghijklmnopqrstuvwxyz" # DEBUG: len = 25 The write forced a copy to a new string in memory.
- 29. H e l l o 8fe0 8fe1 8fe2 8fe3 8fe4 8fe5 Memory str str2 = str[1..3] str_ptr: 8fe1 length: 3 callbacks: [str2]
- 30. str[1] = '&'
- 31. H e l l o 8fe0 8fe1 8fe2 8fe3 8fe4 8fe5 Memory str str2 = str[1..3] callbacks: [str2] e l l 52a0 52a1 52a2 52a3 52a4 52a5
- 32. H & l l o 8fe0 8fe1 8fe2 8fe3 8fe4 8fe5 Memory str str2 = str[1..3] e l l 52a0 52a1 52a2 52a3 52a4 52a5
- 33. So
- 34. def substrings(str) (0...str.length).each_with_object([]) do |i, subs| (i...str.length).each do |j| subs