Commit 5e927e86

5e927e868e36Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified

Description

Add attr_accessor, for..in, loop, class methods

  • attr_accessor/attr_reader/attr_writer: auto-generate getter/setter methods from symbol arguments in class bodies
  • for..in with Range: PM_FOR_NODE → C for loop (inclusive/exclusive)
  • loop do...end: emit while(1) { body }
  • Class methods (def self.foo): emit without self parameter, callable via ClassName.method() syntax

Test: examples/bm_attr.rb — all patterns, output matches Ruby. All regressions pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
5e927e868e36
Parent
7cfabb8887a7

examples/bm_attr.rbaddedView file

1+# Test attr_accessor, attr_reader, attr_writer, class methods, for..in, loop
2+
3+class Point
4+ attr_accessor :x, :y
5+
6+ def initialize(x, y)
7+ @x = x
8+ @y = y
9+ end
10+
11+ def self.origin
12+ Point.new(0, 0)
13+ end
14+
15+ def distance_to(other)
16+ dx = @x - other.x
17+ dy = @y - other.y
18+ Math.sqrt(dx * dx + dy * dy)
19+ end
20+
21+ def to_s
22+ "(" + @x.to_s + ", " + @y.to_s + ")"
23+ end
24+end
25+
26+p1 = Point.new(3, 4)
27+puts p1.x # 3
28+puts p1.y # 4
29+p1.x = 10
30+puts p1.x # 10
31+
32+p2 = Point.origin
33+puts p2.x # 0
34+
35+# for..in with range
36+total = 0
37+for i in 1..10
38+ total += i
39+end
40+puts total # 55
41+
42+# loop with break
43+count = 0
44+loop do
45+ count += 1
46+ break if count >= 5
47+end
48+puts count # 5
49+
50+# Array#each with index (using each)
51+arr = (1..5).to_a
52+sum = 0
53+arr.each do |x|
54+ sum += x
55+end
56+puts sum # 15
57+
58+# String#to_s on object
59+puts p1.to_s # (10, 4)