Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,7 @@ func (vm *VM) bootstrap() {
{vm.cHash, "to_hash"},
{vm.cString, "to_str"},
{vm.cInteger, "to_int"},
{vm.cRegexp, "to_regexp"},
} {
cls, meth := tc.cls, tc.meth
cls.smethods["try_convert"] = &Method{name: "try_convert", owner: cls,
Expand Down
5 changes: 5 additions & 0 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (vm *VM) registerIO() {
if fc, ok := vm.consts["File"].(*RClass).consts["Constants"].(*RClass); ok {
cIO.includes = append(cIO.includes, fc)
}
// IO.try_convert(obj): obj if it is an IO, else its #to_io conversion, else nil.
cIO.smethods["try_convert"] = &Method{name: "try_convert", owner: cIO,
native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
return vm.tryConvert(args[0], cIO, "to_io")
}}
defIOWrite(cIO)
defStringIORead(cIO) // IO carries the read protocol too ($stdin, File streams)
defIOReadExtra(cIO)
Expand Down
9 changes: 9 additions & 0 deletions internal/vm/try_convert_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ begin; Array.try_convert(Bad.new); rescue TypeError => e; p e.message; end`,
{`class BadI; def to_int; "s"; end; end
begin; Integer.try_convert(BadI.new); rescue TypeError => e; p e.message; end`,
`"can't convert BadI into Integer (BadI#to_int gives String)"`},
// Regexp.try_convert (via #to_regexp) and IO.try_convert (via #to_io).
{`p Regexp.try_convert(/x/)`, `/x/`},
{`p Regexp.try_convert("x")`, `nil`},
{`class RC; def to_regexp; /custom/; end; end; p Regexp.try_convert(RC.new)`, `/custom/`},
{`p IO.try_convert($stdout).equal?($stdout)`, `true`},
{`p IO.try_convert("x")`, `nil`},
{`class Bad2; def to_regexp; 42; end; end
begin; Regexp.try_convert(Bad2.new); rescue TypeError => e; p e.message; end`,
`"can't convert Bad2 into Regexp (Bad2#to_regexp gives Integer)"`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15870
15880