[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: shortcut for x = [x] unless x.is_a?(Array

Siep Korteling

3/10/2008 10:57:00 PM

Aryk Grosz wrote:
> Is there any prettier or cleaner way to write
>
> x = [x] unless x.is_a?(Array)

x = x.to_a

Or am I overseeing something?

Regards,

Siep



--
Posted via http://www.ruby-....

10 Answers

Todd Benson

3/10/2008 11:11:00 PM

0

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 = [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 = {}
x = [x] unless x.is_a?(Array)
=> [{x}]

x = 5
x.to_a
=> some warning about default to_a being deprecated

Todd

Paul Mckibbin

3/10/2008 11:16:00 PM

0


> => some warning about default to_a being deprecated
>
> Todd

which is why x=Array(x) would be better.

Mac
--
Posted via http://www.ruby-....

Todd Benson

3/10/2008 11:19:00 PM

0

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 = [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 = {}
>
> x = [x] unless x.is_a?(Array)
> => [{x}]
>
> x = 5
> x.to_a
> => some warning about default to_a being deprecated

I should add, too, that Array[] is different than Array().

Todd

Todd Benson

3/11/2008 2:04:00 AM

0

On Mon, Mar 10, 2008 at 6:16 PM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
>
> > => some warning about default to_a being deprecated
> >
> > Todd
>
> which is why x=Array(x) would be better.
>
> Mac

Like your original response, it depends on what you want to do.

x = {}
Array[x] == 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 #{([x]).inspect}"
puts "Array(#{x.inspect}) gives #{Array(x).inspect}"
puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
#added this ^^^^^^ one
puts '============================'
end

Todd

Paul Mckibbin

3/11/2008 10:30:00 AM

0


> puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
> #added this ^^^^^^ one
> puts '============================'
> end
>
> Todd

Hi Todd,

Array[x] is equivalent to [x].

Mac
--
Posted via http://www.ruby-....

Todd Benson

3/11/2008 4:37:00 PM

0

On Tue, Mar 11, 2008 at 5:30 AM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
>
> > puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
> > #added this ^^^^^^ one
> > puts '============================'
> > end
> >
> > Todd
>
> Hi Todd,
>
> Array[x] is equivalent to [x].

Hi,

I know that now. Just wanted to make sure. Thanks.

Todd

Nikhil Warade

3/12/2008 1:30:00 AM

0

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-....

Todd Benson

3/12/2008 5:09:00 AM

0

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[x].inspect}"
> > > #added this ^^^^^^ one
> > > puts '============================'
> > > end
> > >
> > > Todd
> >
> > Hi Todd,
> >
> > Array[x] is equivalent to [x].
>
> Hi,
>
> I know that now. Just wanted to make sure. Thanks.

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 [x].

Todd

Paul Mckibbin

3/12/2008 12:32:00 PM

0

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-....

Aryk Grosz

4/20/2008 11:28:00 PM

0

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-....