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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 21.4MB
glibcpy 3.10–3.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.fixInstead 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.fixEnsure 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.fixAlways 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.