Skip to content

feat: Ruby の自然な配列構文サポート(0-indexed)#311

Merged
takaokouji merged 11 commits into
developfrom
feature/ruby-array-syntax
Mar 15, 2026
Merged

feat: Ruby の自然な配列構文サポート(0-indexed)#311
takaokouji merged 11 commits into
developfrom
feature/ruby-array-syntax

Conversation

@takaokouji
Copy link
Copy Markdown

@takaokouji takaokouji commented Mar 15, 2026

Summary

Ruby の自然な配列構文でScratchのリストを操作できるようにする。list("$a") 形式を廃止し、$a[0]$a.push("x") 等の 0-indexed 配列構文に統一。

Refs #310

Implementation Steps

  • Phase 1: Ruby Generator — 配列構文出力(list("$a")@a、1-indexed → 0-indexed)
  • Phase 2: Ruby Generator — index/パターン検出(round-trip 最適化)
  • Phase 3: Ruby-to-Blocks Converter — 配列構文パース
  • Phase 4: 配列リテラル $a = [1, 2, 3] サポート
  • Phase 5: index メソッドの 0-indexed 変換
  • Phase Final: Integration Tests
  • Phase DoD: CI 完了待ち + ブラウザ確認
  • Phase 6: バージョンゲーティング — v1はlist()のみ、v2は配列構文のみ

Definition of Done

  • ユニットテスト pass
  • Integration テスト pass
  • lint pass
  • CI green
  • ブラウザ確認(Playwright MCP):
    • Ruby tab で $a = [1, 2, 3] → コードタブ → 正しいブロック
    • コードタブでリスト操作ブロック → Ruby tab → 0-indexed 配列構文
    • round-trip が正しい

@github-actions
Copy link
Copy Markdown

takaokouji and others added 10 commits March 15, 2026 17:43
Change the Ruby generator to output natural Ruby array syntax instead
of the list("$name") wrapper. List indices are converted from Scratch's
1-indexed to Ruby's 0-indexed. Literal indices are decremented directly;
expression indices generate "expr - 1".

Refs #310

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add comment-based pattern detection for clean round-trips:
- operator_subtract with @ruby:array:index comment passes through
  the .index() result without subtracting 1
- getListIndex detects operator_add(x, 1) with @ruby:array:index_offset
  comment and uses x directly as the 0-indexed value

Refs #310

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Implement converter handlers to recognize Ruby array method calls
($a.push, $a[0], $a.length, etc.) and convert them to Scratch list
blocks with proper 0-indexed to 1-indexed conversion.

- Add convertToListBlock helper to convert data_variable to data_listcontents
- Add adjustIndex helper for 0→1 index conversion with Primitive support
- Add << operator handler as alias for push
- Update [], length handlers to support array syntax conversion
- Gate conversion on version >= 2 to preserve v1 string behavior
- Update existing v2 tests: $a.length → data_lengthoflist, $a[0] → data_itemoflist

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Generator: detect clear + push sequence with @ruby:array:literal
comment and emit array literal syntax. Suppress push blocks marked
with @ruby:array:literal:element.

Converter: handle GlobalVariableWriteNode with ArrayNode value,
generating data_deletealloflist + data_addtolist blocks with
round-trip comments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When $a.index("thing") is converted to blocks in version 2,
wrap data_itemnumoflist in operator_subtract(result, 1) with
@ruby:array:index comment for round-trip support.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add 15 round-trip tests covering all array operations (push, <<, delete_at,
clear, insert, []=, [], index, length, include?, array literals) for both
global and instance variables. Fix helper to properly handle stage/sprite
variable separation for $ prefix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The generator now always outputs array syntax ($my_list.push instead of
list("$my_list").push). Update existing roundtrip tests to provide
expectedRuby with the new output format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Ruby version 1 only supports list() syntax; array syntax produces an
error. Ruby version 2 only supports array syntax; list() produces an
error. Added empty? handler for variables in v2 array syntax.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The include? handler in variables.js was creating data_listcontainsitem
blocks with type 'value' instead of 'value_boolean', causing
_processCondition to reject them in if-conditions with error
"condition is not boolean".

Fixes #10

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The empty? and include? handlers in variables.js called convertToListBlock
which throws in v1 for data_variable receivers. In v1, empty? should fall
through to the operators handler, and include? should work directly on
the receiver (pre-existing behavior).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@takaokouji takaokouji force-pushed the feature/ruby-array-syntax branch from 873f515 to d479302 Compare March 15, 2026 08:43
Add i18n support for the 3 error messages introduced in this PR using
defineMessages/react-intl pattern. Add Japanese translations to ja.js
and ja-Hira.js locale files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@takaokouji takaokouji merged commit b062142 into develop Mar 15, 2026
9 checks passed
@takaokouji takaokouji deleted the feature/ruby-array-syntax branch March 15, 2026 09:06
github-actions Bot pushed a commit that referenced this pull request Mar 15, 2026
…array-syntax

feat: Ruby の自然な配列構文サポート(0-indexed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant