Comments (11)
yup, this will be done. related issue: #58
from virtus.
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.
@dkubb seems like it's been fixed in 1.9
from virtus.
@solnic I wonder if backports fixes this for 1.8.
from virtus.
@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.
@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.
@V0N this won't work cause if you pass in an array you'll end up with an array in an array :)
from virtus.
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.
@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.
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.
Great, thanks :)
from virtus.
Related Issues (20)
- Publish 1.0.6
- Avoid override of previously assigned attributes
- Put link to dry-rb in README.md to save time for new comers
- possible to dynamically add attributes? HOT 1
- What replaces `ValueObject::InstanceMethods::with`?
- Class not getting initialized HOT 1
- Bug: nil default value of Array attribute ignored HOT 1
- Coerce proc and strict mode don't match
- Default values aren't assigned when initialize method is present. HOT 1
- Array of Array of FooClass
- Strange Boolean behaviour HOT 5
- Validation from class attributes
- Show Embedded value on edit form
- Calling methods inside of a Virtus::Attribute
- Mark as unmaintaned HOT 2
- Hash.try_convert(attributes) errors on params in Rails 5
- FixedWidth Coercion HOT 2
- Date formats HOT 1
- How can I use array of tags(Strings) in a key of a hash? HOT 1
- Strict mode only works for basic values HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from virtus.