Adam Akhtar wrote:
> ....to doing the opposite.
> 
> Although there is already a method call to flatten arrays i thought 
> since i created an unflatten one above i should have go at flattening.
> 
> It takes a nested array and then returns a flattened version
> 
> 
> 
> def flattenlist nestedlist
>   flatlist = []
> 
>   return flatlist if nestedlist.empty?
> 
>   if (nestedlist.first.is_a?(Array))
>     flatlist.push nestedlist.first.shift
>     nestedlist.delete_at(0)  if (nestedlist.first.empty?)
>   else #not array so just add
>     flatlist.push nestedlist.shift
>   end
> 
>   flatlist = flatlist + flattenlist(nestedlist)
>   flatlist
> end
> 
> What do you guys/girls think? give me your hints and ill go away and 
> improve it!
> I wasnt so happy with this part
> 
>   if (nestedlist.first.is_a?(Array))
>     flatlist.push nestedlist.first.shift
>     nestedlist.delete_at(0)  if (nestedlist.first.empty?)
> 
> 
> I have a feeling theres a delete method that after deleting the last 
> element in an empty array will delete the array itself.

If you'd like to know what methods are available for an object, try 
this:

a=[[1,2],[1,3],2,3]
puts a.methods.sort

You want to flatten this array? Hey, it's just there. If you want to 
know how it works, find a cmd-prompt and type "ri Array.flatten" .

Regards,

Siep
-- 
Posted via http://www.ruby-forum.com/.