|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> => 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/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
> 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-forum.com/. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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/. |
|
![]() |
| Outils de la discussion | |
|
|