Last Updated: February 25, 2016
·
989
· stevennunez

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!

1 Response
Add your response

It's potential includes Bruteforce password hacking.

over 1 year ago ·