String tools

Split and join

The method split cuts a string into an array around a separator. Without arguments it splits on whitespace, which tidies messy input. The method join performs the reverse trip, gluing an array back together with a separator between elements.

words = "Ada,Bob,Lin".split(",") puts words.length puts words.join(" and ")

ruby split_join.rb

3
Ada and Bob and Lin

sub/gsub and replace

The method sub replaces the first match, while gsub replaces every match in the whole string. Both build a fresh string and leave the original untouched. Bang variants ending with an exclamation mark rewrite the string in place instead.

s = "aaa" puts s.sub("a", "o") puts s.gsub("a", "o") puts s

ruby sub_gsub.rb

oaa
ooo
aaa

Regexp match

The operator =~ returns the index of the first match, or nil when nothing matches. The method match returns a MatchData object, with captures available in brackets. The predicate match? gives a plain boolean when the captures are not needed.

s = "age 36" puts(s =~ /[0-9]+/) m = s.match(/([0-9]+)/) puts m[1] puts s.match?(/[0-9]+/)

ruby match_demo.rb

4
36
true

The digits start at index 4, and the first capture group holds 36.

sprintf

The methods format and sprintf fill a template with values: %d takes an integer, %s takes a string, and %.2f rounds a float to two decimals. Templates keep padding and precision in one readable place.

puts format("%d plus %d is %d", 2, 3, 5) puts sprintf("%.2f", 3.14159)

ruby format_demo.rb

2 plus 3 is 5
3.14

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? words = "Ada,Bob,Lin".split(",") puts words.length puts words.join(" and ")

    Answer

    3 / Ada and Bob and Lin

    split cuts around the comma and join glues the three names back with its separator.

  2. What does this program print? s = "aaa" puts s.sub("a", "o") puts s.gsub("a", "o") puts s

    Answer

    oaa / ooo / aaa

    sub replaces the first match, gsub every match, and both leave the original alone.

  3. What does this program print? s = "age 36" puts(s =~ /[0-9]+/) m = s.match(/([0-9]+)/) puts m[1] puts s.match?(/[0-9]+/)

    Answer

    4 / 36 / true

    The digits start at index 4, the capture holds 36, and match? answers a boolean.

  4. What does this program print? puts format("%d plus %d is %d", 2, 3, 5) puts sprintf("%.2f", 3.14159)

    Answer

    2 plus 3 is 5 / 3.14

    %.2f rounds the float to two decimals.

  5. Which method replaces every match in a string?

    Answer

    gsub

    sub replaces the first match, gsub every match.

  6. What does this program print? p "a b c".split

    Answer

    ["a", "b", "c"]

    Without a separator, split cuts on runs of whitespace and drops the empty pieces.

  7. What does this program print? p "aaa".sub(/a/, "o")

    Answer

    "oaa"

    sub replaces only the first match and answers a new string, printed with quotes by p.

  8. What does "age 36" =~ /z+/ return?

    Answer

    nil, because nothing matches

    The operator answers the index of the first match, or nil when there is none.

  9. What does this program print? p format("%05.1f", 3.14159)

    Answer

    "003.1"

    The 5 is the total width, padded with zeros, and .1 keeps one decimal.

  10. What does this program print? p "x".match?(/y/)

    Answer

    false

    match? answers a plain boolean when the captures are not needed.

Article author: Arthur Isaev