{"id":2326,"library":"tree-sitter-ruby","title":"Tree-sitter Ruby Grammar","description":"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.","status":"active","version":"0.23.1","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-ruby","tags":["parsing","ruby","tree-sitter","grammar","ast"],"install":[{"cmd":"pip install tree-sitter tree-sitter-ruby","lang":"bash","label":"Install core bindings and Ruby grammar"}],"dependencies":[{"reason":"Provides the core Python bindings and parsing engine required to use Tree-sitter grammars. tree-sitter-ruby supplies only the Ruby language grammar itself.","package":"tree-sitter"}],"imports":[{"note":"Required to load the compiled Ruby grammar.","symbol":"Language","correct":"from tree_sitter import Language"},{"note":"The main class for parsing source code into a syntax tree.","symbol":"Parser","correct":"from tree_sitter import Parser"},{"note":"The `tree_sitter_ruby` package exposes its compiled grammar via the `language()` function, which is then passed to `tree_sitter.Language`.","symbol":"tsruby.language()","correct":"import tree_sitter_ruby as tsruby\nRUBY_LANGUAGE = Language(tsruby.language())"}],"quickstart":{"code":"import tree_sitter_ruby as tsruby\nfrom tree_sitter import Language, Parser\n\n# Load the Ruby language grammar\nRUBY_LANGUAGE = Language(tsruby.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(RUBY_LANGUAGE)\n\n# Ruby code to parse (must be bytes)\nruby_code = b\"\"\"\ndef hello_world(name)\n  puts \"Hello, #{{name}}!\"\nend\n\nhello_world(\"Alice\")\n\"\"\"\n\n# Parse the code\ntree = parser.parse(ruby_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Number of children: {len(root_node.children)}\")\n# Example of traversing a child node\nif root_node.children:\n    first_child = root_node.children[0]\n    print(f\"First child type: {first_child.type}, text: {first_child.text.decode('utf8')}\")\n","lang":"python","description":"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."},"warnings":[{"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())`.","message":"The `Language.build_library` method was removed around `py-tree-sitter` version 0.21.x. Attempting to use it will raise an AttributeError.","severity":"breaking","affected_versions":"tree-sitter>=0.21.0"},{"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')`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}