Friday, 15 June 2012

Separating array with different objects into specific object arrays in ruby -


Imagine I have an array with different types of objects like:

  [pic] 1, the truth is, 'hello', true, 2, '', 'world']  

I am trying to create a function that is arranged with arrays of different objects Makes the array.

  [[1,2], ['Hello', '', 'world'], [true, true]]  

So far I Come with this:

  def_hebject arg arg.inject ([]) {| ACC, Maine | Flag = 0 acc.each Do | I | If i [0]! = Zero & amp; Value.class == i [0]. Class I & lt; & Lt; Value Flag = 1 End End If Flag == 0 ACC & lt; & Lt; [Value] end  

The problem is that apparently I get an error when I do:

  value.class == i [0] class  

Which is strange, because it makes sense to me. I'm new to Ruby and I appreciate some insights.

Your example fails, because you ac to inject is not updated at the end of the block just add AC before the closing } :

  # ... end Besides, I will do something like this:  
  [1, true, 'hello', truth, 2, '', 'world' ]. Group: By (& amp; Square). Value # = & gt; [[1, 2], [true, true], ["hello", "", "world"]]  

No comments:

Post a Comment