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!
Written by Steven Nunez
Related protips
1 Response
It's potential includes Bruteforce password hacking.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#