Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicitly convert through string and integer in Time::at offset parsing #2093

Open
lopopolo opened this issue Aug 21, 2022 · 0 comments
Open
Labels
A-ruby-core Area: Ruby Core types. B-mruby Backend: Implementation of artichoke-core using mruby.

Comments

@lopopolo
Copy link
Member

This doesn't look quite right.

[3.1.2] > class B; def to_str; 'B'; end; end
=> :to_str
[3.1.2] > Time.at(12, in: B.new)
=> 1970-01-01 02:00:12 +0200
[3.1.2] > class C; def to_str; raise 'crash'; end; end
=> :to_str
[3.1.2] > Time.at(12, in: C.new)
(irb):10:in `to_str': crash (RuntimeError)
        from <internal:timev>:274:in `at'
        from (irb):11:in `<main>'
        from /usr/local/var/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `load'
        from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `<main>'
[3.1.2] > class D; def to_int; raise 'dint'; end; def to_str; raise 'dstr'; end; end
=> :to_str
[3.1.2] > Time.at(12, in: D.new)
(irb):12:in `to_str': dstr (RuntimeError)
        from <internal:timev>:274:in `at'
        from (irb):13:in `<main>'
        from /usr/local/var/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `load'
        from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `<main>'

It looks like MRI prefers to implicitly convert through #to_str if it exists and otherwise will convert through #to_int. We can check for this by doing a Value::respond_to check.

Originally posted by @lopopolo in #1956 (comment)

@lopopolo lopopolo added A-ruby-core Area: Ruby Core types. B-mruby Backend: Implementation of artichoke-core using mruby. labels Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ruby-core Area: Ruby Core types. B-mruby Backend: Implementation of artichoke-core using mruby.
Development

No branches or pull requests

1 participant