Giter Site home page Giter Site logo

Comments (3)

solnic avatar solnic commented on August 24, 2024

Yes, I like that. We should also think about making sure that coercions are 'symmetrical' - we've discussed this once I believe.

from virtus.

dkubb avatar dkubb commented on August 24, 2024

@solnic sure, where it's possible. I believe we could make a method that coerces an Array into a Hash by passing it into Hash[], for example. I think we already have stuff to coerce Hash into an Array.

The general idea is that if Ruby provides conventions for conversions into specific primitives, then I want to use those methods by default if they're available. Coercion subclasses can override that if there's a more idiomatic way to convert to the primitive.

from virtus.

dkubb avatar dkubb commented on August 24, 2024

Ok, I added #to_hash, #to_string and #to_integer to Virtus::Coercion::Object. Marking this issue as closed.

I decided to only use the explicit coercion methods (#to_hash, #to_str, and #to_int respectively) because the other methods are used in a bit "fast and loose" style within ruby core/stlib.

Also nearly every object responds to #to_s, and we don't want to coerce stuff into strings and end up with the inspect output -- with virtus' coercions we're looking for cases where the coercion is explicit and in many cases can be reversed because there was no loss of information.

from virtus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.