pyjjzg
Last Updated: February 25, 2016
·
628
· stevennunez
148187 557681800912781 556845050 n

Sequential Strings

This one is short, but I thought it was neat.
"bar".upto("baz").to_a \# => ["bar", "bas", "bat", "bau", "bav", "baw", "bax", "bay", "baz"]
The upto method on 'String' returns an enumerable object that yields every string from the starting string until the end. Because it's enumerable, you can chain on other enumerable methods.

"a".upto("zz").select {|ltr| ltr.include?('s') }
\# => ["s", "as", "bs", "cs", "ds", "es", "fs", "gs", "hs", "is", "js", "ks", "ls", "ms", "ns", "os", "ps", "qs", "rs", "sa", "sb", "sc", "sd", "se", "sf", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sp", "sq", "sr", "ss", "st", "su", "sv", "sw", "sx", "sy", "sz", "ts", "us", "vs", "ws", "xs", "ys", "zs"]

This is the same as:
('a'..'zz').select {|ltr| ltr.include?('s') }

As with the range, there is no downto.

Not sure when I'd used this, there we are!

Say Thanks
Respond

1 Response
Add your response

17497
None

It's potential includes Bruteforce password hacking.

over 1 year ago ·
Awesome Job

A3e45f3a cade 11e7 83bc 71c9546a2749
Systems Engineer
·
Fulton, Maryland or Remote
·
Full Time