Aryk Grosz wrote:
Is there any prettier or cleaner way to write
x = unless x.is_a?(Array)
x = x.to_a
Or am I overseeing something?
Regards,
Siep
···
--
Posted via http://www.ruby-forum.com/\.
Aryk Grosz wrote:
Is there any prettier or cleaner way to write
x = unless x.is_a?(Array)
x = x.to_a
Or am I overseeing something?
Regards,
Siep
--
Posted via http://www.ruby-forum.com/\.
#to_a can behave differently
x = {}
x = x.to_a
=>
x = {}
x = unless x.is_a?(Array)
=> [{x}]
x = 5
x.to_a
=> some warning about default to_a being deprecated
Todd
On Mon, Mar 10, 2008 at 5:57 PM, Siep Korteling <s.korteling@gmail.com> wrote:
Aryk Grosz wrote:
> Is there any prettier or cleaner way to write
>
> x = unless x.is_a?(Array)x = x.to_a
Or am I overseeing something?
Regards,
Siep
=> some warning about default to_a being deprecated
Todd
which is why x=Array(x) would be better.
Mac
--
Posted via http://www.ruby-forum.com/\.
I should add, too, that Array is different than Array().
Todd
On Mon, Mar 10, 2008 at 6:11 PM, Todd Benson <caduceass@gmail.com> wrote:
On Mon, Mar 10, 2008 at 5:57 PM, Siep Korteling <s.korteling@gmail.com> wrote:
> Aryk Grosz wrote:
> > Is there any prettier or cleaner way to write
> >
> > x = unless x.is_a?(Array)
>
> x = x.to_a
>
> Or am I overseeing something?
>
> Regards,
>
> Siep#to_a can behave differently
x = {}
x = x.to_a
=>x = {}
x = unless x.is_a?(Array)
=> [{x}]x = 5
x.to_a
=> some warning about default to_a being deprecated
Like your original response, it depends on what you want to do.
x = {}
Array == Array(x)
=> false
In this case -- with the result -- one encapsulates, the other
modifies. Same thing with NilClass and Range. I added a line to your
code to see this for sure...
TEST=[[1,2,3],(1..3),{:a=>:b},1,"test",true,nil,/123/]
TEST.each do |x|
puts x.class
puts '============================'
puts "#{x.inspect}.to_a gives #{x.to_a.inspect}"
puts "[#{x.inspect}] gives #{().inspect}"
puts "Array(#{x.inspect}) gives #{Array(x).inspect}"
puts "Array[#{x.inspect}] gives #{Array.inspect}"
#added this ^^^^^^ one
puts '============================'
end
Todd
On Mon, Mar 10, 2008 at 6:16 PM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
> => some warning about default to_a being deprecated
>
> Toddwhich is why x=Array(x) would be better.
Mac
Paul Mckibbin wrote:
=> some warning about default to_a being deprecated
Todd
which is why x=Array(x) would be better.
Mac
(1..3).to_a will not give any warning and will convert it to [1,2,3]
--
Posted via http://www.ruby-forum.com/\.
puts "Array[#{x.inspect}] gives #{Array.inspect}"
#added this ^^^^^^ one
puts '============================'
endTodd
Hi Todd,
Array is equivalent to .
Mac
--
Posted via http://www.ruby-forum.com/\.
Nikhil Warade wrote:
(1..3).to_a will not give any warning and will convert it to [1,2,3]
Object#to_a is being deprecated, so you will get the warning with
classes that don't implement their own. When they do, as is the case
with Range objects, you won't see it.
Since the only "hint" that was given in the initial question was that
Array was definitely a possibility as an input, so I assumed that an
unknown class could be set as x. In his actual problem this is almost
certainly more clearly restricted.
--
Posted via http://www.ruby-forum.com/\.
Hi,
I know that now. Just wanted to make sure. Thanks.
Todd
On Tue, Mar 11, 2008 at 5:30 AM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
> puts "Array[#{x.inspect}] gives #{Array.inspect}"
> #added this ^^^^^^ one
> puts '============================'
> end
>
> ToddHi Todd,
Array is equivalent to .
Hey guys,
I just read through this whole thread. I've really thought this through.
What I was really trying to do all along was this.
x = [x] unless x.is_a?(Array). I know this sounds redundant, buts that
what I want and thats all I want.
In the case of most types, the to_a and Array() can get you by, but when
you don't know what type it is (especially when it could be a hash), I
defined this function to flat out just do what I want it to do.
class Array
def self.wrap(value)
value.is_a?(Array) ? value : [value]
end
end
Simple, to the point, and no corner cases...is my solution.
So:
g = {:x=>1, :y=>2}
Array.wrap(g)[0].is_a?(Hash) ==> true
Done.
--
Posted via http://www.ruby-forum.com/.
I think in my head I was thinking of something different anyway...
x = [*x]
...which behaves the same as Array(x), except for x = nil, where it
behaves like .
Todd
On Tue, Mar 11, 2008 at 11:37 AM, Todd Benson <caduceass@gmail.com> wrote:
On Tue, Mar 11, 2008 at 5:30 AM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
>
> > puts "Array[#{x.inspect}] gives #{Array.inspect}"
> > #added this ^^^^^^ one
> > puts '============================'
> > end
> >
> > Todd
>
> Hi Todd,
>
> Array is equivalent to .Hi,
I know that now. Just wanted to make sure. Thanks.