Ruby String Chopper methods, pt II
Tags: ruby
Published: 2020-12-14 19:59:40

This post builds off of what I posted [HERE](https://dev.to/crankums/building-a-word-slicer-ruby-edition-426p). So, our short recap: I’m writing methods that chop up a string in different ways to use as search queries for my database at work. 

I was running into an issue when I was trying to write something that would make overlapping queries. Basically, in my `while` loops, I was trying to subtract from the starting index the amount I would want the strings to overlap each other. But! given that my loop was only advancing a single letter at a time, it already shows any kind of overlap each substring would have with each other.

So that means that I haven’t actually made a real chopper. So let’s do that. Now our basic `#chopper` looks like this:

```
def chopper(str, len, vowels=false)
 vowels ? str = remove_vowels(str) : str
 i=0
 ret_arr = []
 while i<str.length do
   ret_arr.push(str.slice(i, len)) unless str.slice(i, len).length < len
   i+=1
 end
 ret_arr
end
```

It takes a string, the length of substrings desired, and whether or not you want vowels in it as your arguments. If you said `true` to the latter, it removes them. Then, it iterates the string, pushing substrings of the length desired into an array, which it returns. The `unless` condition prevents the smaller strings resulting from nearing the end of the word from being pushed into the return array.

But what if what I want is chunks of substring without overlap? How would I do things differently?

The answer lies in our incrementation! And our incrementation lies in our segmentation!

Instead of increasing our value of `i`, our counter, by 1, let’s increase it by the length of our desired substring. Similar to before, we’ll start with minimal arguments and build it up as we go:

```
def slicer(str, 3)
 i=0
 ret_arr = [] 
   while i<str.length do
     ret_arr.push(str.slice(i,3)) unless str.slice(i, 3).length < 3
     i += 3
   end
 ret_arr
end
```

We’ll use `hersheys` as our input. Let’s take a look at what it gives us:

```
puts slicer(input).to_s

>["her", "she"]
```

Fewer substrings, but that’s basically what I was expecting. If I took the `unless` filter off, I’d get a third element of `ys`.

Now let’s add some arguments: 

```
def slicer(str, len)
 i=0
 ret_arr = [] 
   while i<str.length do
     ret_arr.push(str.slice(i,len)) unless str.slice(i, len).length < len
     i += len
   end
 ret_arr
end
```

Which gives us:

```
puts slicer(input, 3).to_s
puts slicer(input, 4).to_s
puts slicer(input, 5).to_s

["her", "she"]
["hers", "heys"]
["hersh"]
```

Again, fewer strings, but that’s the idea. We’re looking at the limitations of different types of substrings. Finally, let’s add the vowel filter in. 

```
def slicer(str, len, vowels=false)
 vowels ? str = remove_vowels(str) : str
 i=0
 ret_arr = [] 
   while i<str.length do
     ret_arr.push(str.slice(i,len)) unless str.slice(i, len).length < len
     i += len
   end
 ret_arr
end
```
Gives us:

```
puts slicer(input, 3).to_s
puts slicer(input, 4).to_s
puts slicer(input, 5).to_s
puts slicer(input, 3, true).to_s
puts slicer(input, 4, true).to_s
puts slicer(input, 5, true).to_s
 
["her", "she"]
["hers", "heys"]
["hersh"]
["hrs", "hys"]
["hrsh"]
["hrshy"]
```

Again, not a lot of strings vs the `#chopper` methods, but maybe if I were to use a longer string:

```
input = ‘ghirardelli`

["ghi", "rar", "del", "lis"]
["ghir", "arde", "llis"]
["ghira", "rdell"]
["ghr", "rdl"]
["ghrr", "dlls"]
["ghrrd"]
```

And I start getting some interesting substrings. This could also include spaces, apostrophes, and others.


What else should I make? And I’ll need some way to keep these organized. Maybe I should make these into some sort of class to build these methods into some kind of library? Let’s give it a shot next time!

I’ll be following up with the JavaScript version of this shortly, and then whatever developments I bring into this little bunch next. Thanks for reading!