Commit 71523c1d

71523c1d272cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Comparable#between? for int, float, and string

x.between?(a, b) compiles to (x >= a && x <= b) for numeric types, and strcmp-based comparison for strings.

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

Details

Commit
71523c1d272c
Parent
fead3d12c946

spinel_codegen.rbmodifiedView file

@@ -1596,6 +1596,9 @@
15961596 if mname == "any?" || mname == "all?" || mname == "none?"
15971597 return "bool"
15981598 end
1599+ if mname == "between?"
1600+ return "bool"
1601+ end
15991602 if mname == "nil?"
16001603 return "bool"
16011604 end
@@ -11347,6 +11350,22 @@
1134711350 if mname == "!"
1134811351 return "(!" + compile_expr(recv) + ")"
1134911352 end
11353+ if mname == "between?"
11354+ args_id = @nd_arguments[nid]
11355+ if args_id >= 0
11356+ aargs = get_args(args_id)
11357+ if aargs.length >= 2
11358+ rc = compile_expr(recv)
11359+ lo = compile_expr(aargs[0])
11360+ hi = compile_expr(aargs[1])
11361+ lt = infer_type(recv)
11362+ if lt == "string"
11363+ return "(strcmp(" + rc + ", " + lo + ") >= 0 && strcmp(" + rc + ", " + hi + ") <= 0)"
11364+ end
11365+ return "(" + rc + " >= " + lo + " && " + rc + " <= " + hi + ")"
11366+ end
11367+ end
11368+ end
1135011369 if mname == "<<"
1135111370 lt = infer_type(recv)
1135211371 if lt == "mutable_str"