From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2023-10-18T16:32:58+00:00 Subject: [ruby-core:115088] [Ruby master Misc#19931] to_int is not for implicit conversion? Issue #19931 has been updated by Dan0042 (Daniel DeLorme). sawa (Tsuyoshi Sawada) wrote in #note-2: > Your description suggests a contrast between `to_int` and `to_i`, but while you showed a code example using `to_int`, you have not shown anything using `to_i`. If using `to_i` for explicit conversion, the example would be `def o.to_i; 1; end; 1 + o.to_i #=> 2` nobu (Nobuyoshi Nakada) wrote in #note-3: > Might consider adding another step there. > 3. try the implicit conversion. Yes, that's what I was talking about, and why it could work even for `1 + 1.5`. Any idea why this isn't done currently? Is it just that no one thought of it? matheusrich (Matheus Richard) wrote in #note-4: > If I understood this correctly, that would allow doing things like > ```ruby > 1 + "1" # => 2 > ``` No, because "1" doesn't respond to `#to_int` ---------------------------------------- Misc #19931: to_int is not for implicit conversion? https://bugs.ruby-lang.org/issues/19931#change-104960 * Author: Dan0042 (Daniel DeLorme) * Status: Open * Priority: Normal ---------------------------------------- While reviewing some implicit vs explicit conversion concepts, I discovered that arithmetic operations do not perform the implicit conversion I expected from #to_int ```ruby o = Object.new def o.to_int; 1; end 1 + o #TypeError ``` I understand there's the whole #coerce thing for numbers, but I had expected #to_int to fit neatly into this and cause the object to be implicitly coerced to Integer. So basically I thought that #to_i was for explicit conversion and #to_int for implicit conversion; is that not the case? Most of the internet seems to think that (to_int : to_i) relationship is like (to_str : to_s). But I can't seems to find authoritative documentation on the topic. -- 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/