[ruby-core:113830] [Ruby master Bug#19721] IO#timeout= can be called without required argument
From:
"ufuk (Ufuk Kayserilioglu) via ruby-core" <ruby-core@...>
Date:
2023-06-08 15:03:01 UTC
List:
ruby-core #113830
Issue #19721 has been updated by ufuk (Ufuk Kayserilioglu).
I believe the syntax `f.timeout=()` is _not_ calling the `timeout=` method with no parameters, but instead it is assigning `()` to `f.timeout` attribute. Since `()` in Ruby evaluates to `nil`, since it is an empty subexpression, in essence, that line should be equivalent to `f.timeout = nil`. That's why you get the `nil` value.
You can check that this expression gets turned into an assigment by looking at the AST generated from `f.timeout=()` on https://ruby-syntax-tree.github.io/:
```
program
(statements
((assign
(field (vcall (ident "f")) (period ".") (ident "timeout"))
(paren (statements ((void_stmt))))
))
)
)
```
----------------------------------------
Bug #19721: IO#timeout= can be called without required argument
https://bugs.ruby-lang.org/issues/19721#change-103476
* Author: andrykonchin (Andrew Konchin)
* Status: Open
* Priority: Normal
* ruby -v: 3.2.1
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN
----------------------------------------
```ruby
f = File.open("a.txt", "w")
f.timeout=() # => nil
```
`IO#timeout=` requires an argument (or it's supposed to require it) but if it's called as a method it seems the check is skipped and missing argument is treated as `nil` value.
If it's called with `#send` - then argument presence is checked:
```ruby
f.send :"timeout="
# ...:in `timeout=': wrong number of arguments (given 0, expected 1) (ArgumentError)
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/