Extracting substrings

Ruby:
Extracting substrings

How to:

Ruby makes extracting substrings simple. Let’s cut to the chase:

str = "Hello, Ruby World!"

# Method 1: Using array indices
substring = str[7, 4] # "Ruby"
puts substring

# Method 2: Using the slice method
slice = str.slice(7, 4) # "Ruby"
puts slice

# Method 3: Regular expressions
match = str[/[Rr]uby/] # "Ruby"
puts match

# Method 4: split and array access
split_array = str.split # default splits on whitespace
picked_word = split_array[2] # "World!"
puts picked_word

Sample output for each snippet will be “Ruby”, “Ruby”, “Ruby”, “World!” respectively.

Deep Dive

Back in the day, extracting substrings was a more verbose process. Ruby’s evolved, though. Today, you’ve got methods and regex at your disposal.

Here’s what’s happening under the hood: [7, 4] means start at the 7th character and grab the next 4. slice is just a methodic way to say the same thing. With regex, /[Rr]uby/ is like saying, “Catch me a ‘Ruby’ or ‘ruby’, whichever you find first.” split chops the string into an array at each space, and [2] picks the third word—arrays start at zero, remember.

Alternatives? Sure, Ruby’s got ’em. partition, rpartition, and match could also play here. Each has its case but knowing .slice and regex covers most bases.

Bottom line: substring extraction is about precise text manipulation. The right tool means clean, effective code.

See Also