Registry / serialization / tree-sitter-ruby

tree-sitter-ruby

JSON →
library0.23.1pypypi✓ verified 25d ago

tree-sitter-ruby provides the Ruby grammar for the Tree-sitter parsing library. It enables Python applications to parse Ruby source code into concrete syntax trees. The library is actively maintained, with version 0.23.1 being the current stable release, and new versions are released periodically to update grammar rules or address issues.

pip install tree-sitter tree-sitter-ruby
INSTALL
IMPORT
SIG · TREE-SITTER-RUBY
T
tree-sitter-ruby
serializationpythonv0.23.1
Install
1.6s avg
Import
11ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.23.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 21.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.006s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Language
from tree_sitter import Language
Required to load the compiled Ruby grammar.
Parser
from tree_sitter import Parser
The main class for parsing source code into a syntax tree.
tsruby.language()
import tree_sitter_ruby as tsruby RUBY_LANGUAGE = Language(tsruby.language())
The `tree_sitter_ruby` package exposes its compiled grammar via the `language()` function, which is then passed to `tree_sitter.Language`.

This quickstart demonstrates how to initialize the Ruby grammar with `tree-sitter`, create a parser, and parse a simple Ruby code snippet. It shows how to access the root of the resulting syntax tree and some basic node properties.

import tree_sitter_ruby as tsruby from tree_sitter import Language, Parser # Load the Ruby language grammar RUBY_LANGUAGE = Language(tsruby.language()) # Create a parser and set its language parser = Parser() parser.set_language(RUBY_LANGUAGE) # Ruby code to parse (must be bytes) ruby_code = b""" def hello_world(name) puts "Hello, #{{name}}!" end hello_world("Alice") """ # Parse the code tree = parser.parse(ruby_code) # Get the root node of the syntax tree root_node = tree.root_node print(f"Root node type: {root_node.type}") print(f"Number of children: {len(root_node.children)}") # Example of traversing a child node if root_node.children: first_child = root_node.children[0] print(f"First child type: {first_child.type}, text: {first_child.text.decode('utf8')}")
Debug
Known issues
breakingThe `Language.build_library` method was removed around `py-tree-sitter` version 0.21.x. Attempting to use it will raise an AttributeError.
fix
Instead of `Language.build_library`, directly `pip install tree-sitter-<language>` (e.g., `pip install tree-sitter-ruby`). The installed package provides a pre-compiled grammar via its `language()` function, like `Language(tree_sitter_ruby.language())`.
affects: tree-sitter>=0.21.0
gotchaThe `parser.parse()` method expects source code as a `bytes` object, not a standard Python string. Passing a string will result in a `TypeError` or unexpected parsing behavior.
fix
Ensure that your input source code is encoded into bytes, typically UTF-8, before passing it to the parser. For example, `code_string.encode('utf8')`.
affects: All versions
gotchaWhile `tree-sitter-ruby` provides the Ruby grammar, the core parsing functionality (e.g., `Parser` class, `Language` class) is provided by the `tree-sitter` PyPI package. Both need to be installed.
fix
Always ensure `pip install tree-sitter` is executed alongside `pip install tree-sitter-ruby` to have both the core bindings and the specific language grammar available.
affects: All versions
Upgrade
Version history
0.23.1latest on PyPI · released Nov 11, 2024
Audit
Dependencies
tree-sitterrequiredProvides the core Python bindings and parsing engine required to use Tree-sitter grammars. tree-sitter-ruby supplies only the Ruby language grammar itself.
Agent activity
18 hits · last 30 days
node
13
OpenAI (training)
1
Resources
tree-sitter-ruby — pip install tree-sitter-ruby · libregistry