Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Ripper.tokenize

(::File | ::Ripper::_Gets | ::String src, ?::String filename, ?::Integer lineno, ?raise_errors: ::boolish) -> ::Array[::String]
tokenize(src, filename = '-', lineno = 1, **kw)

Tokenizes the Ruby program and returns an array of strings. The filename and lineno arguments are mostly ignored, since the return value is just the tokenized input. By default, this method does not handle syntax errors in src, use the raise_errors keyword to raise a SyntaxError for an error in src.

Example
p Ripper.tokenize("def m(a) nil end")
   # => ["def", " ", "m", "(", "a", ")", " ", "nil", " ", "end"]
See also .slice #compile_error .sexp_raw #warn also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0