Git Product home page Git Product logo

Comments (11)

solnic avatar solnic commented on June 28, 2024

yup, this will be done. related issue: #58

from virtus.

dkubb avatar dkubb commented on June 28, 2024

That works in almost all cases, except one that I know of:

Array("a\nb")  # => ["a\n", "b"]

It's one of those odd quirks that's snuck into ruby and never been removed for legacy reasons, but since we generally try to follow POLS in Virtus and aren't under any obligation to propagate ruby's inconsistencies, I think we should probably define the behaviour as something equivalent to:

def Virtus::Coercion::Object.to_array(value) Array(value) end

def Virtus::Coercion::String.to_array(value) [ value ] end

from virtus.

solnic avatar solnic commented on June 28, 2024

@dkubb seems like it's been fixed in 1.9

from virtus.

dkubb avatar dkubb commented on June 28, 2024

@solnic I wonder if backports fixes this for 1.8.

from virtus.

dkubb avatar dkubb commented on June 28, 2024

@solnic it looks like this is not solved in backports. I may submit a patch to that project, since I want to try to avoid patching Virtus to work-around 1.8 issues, except in extreme cases where there is no other choice. Ideally all 1.8 specific hacks should be pushed to backports.

from virtus.

vivien avatar vivien commented on June 28, 2024

@dkubb I agree with you, version specific snippets make the code really ugly, especially when they are Ruby inconsistencies...
About this issue, we can maybe use:

def Virtus::Coercion::Object.to_array(value) [ value ] end

It is pretty clear and should work everytimes.

from virtus.

solnic avatar solnic commented on June 28, 2024

@V0N this won't work cause if you pass in an array you'll end up with an array in an array :)

from virtus.

vivien avatar vivien commented on June 28, 2024

Oops, you're right. I think that Array({1 => 2}) # => [[1, 2]] is not what we want either, so Array() doesn't seem to fit here. Maybe we should go for a classic test?

value.is_a?(Array) ? value : [ value ]

from virtus.

solnic avatar solnic commented on June 28, 2024

@V0N actually no, we definitely want this behavior and that's what Virtus::Coercion::Hash.to_array already does

The test is not needed in Coercion::Object because it's the default coercion behavior for all objects. If we need a custom behavior then to_array should be overwritten in other coercion class. For example if we we want a custom behavior for strings then all we need to do is to implement Coercion::String.to_array.

from virtus.

dkubb avatar dkubb commented on June 28, 2024

yeah, the Array() method (actually it's really Kernel::Array()) tries to call #to_ary if it exists, then falls back to #to_a, and if that's not defined either it just returns [ obj ]. IMHO it's the most idiomatic way to convert a object to an Array in ruby.

see: http://rubydoc.info/stdlib/core/1.9.3/Kernel:Array

from virtus.

vivien avatar vivien commented on June 28, 2024

Great, thanks :)

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.