Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

MatchData#inspect

inspect → string

Returns a string representation of self:

  m = /.$/.match("foo")
  # => #<MatchData "o">
  m.inspect # => "#<MatchData \"o\">"

  m = /(.)(.)(.)/.match("foo")
  # => #<MatchData "foo" 1:"f" 2:"o" 3:"o">
  m.inspect # => "#<MatchData \"foo\" 1:\"f\" 2:\"o\

  m = /(.)(.)?(.)/.match("fo")
  # => #<MatchData "fo" 1:"f" 2:nil 3:"o">
  m.inspect # => "#<MatchData \"fo\" 1:\"f\" 2:nil 3:\"o\">"

Related: MatchData#to_s.
See also #hash #length #eql? #match also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0