Imagine I have an array with different types of objects such as:
[1,true,'hello',true,2,' ','world']
I'm trying to create a function that outputs an array of arrays with those objects separated.
[ [1,2] , ['hello', ' ', 'world'] , [true,true] ]
So far i've come with this:
def same_object arg
arg.inject([]){ |acc,value|
flag = 0
acc.each do |i|
if i[0] != nil && value.class == i[0].class
i << value
flag = 1
end
end
if flag == 0
acc << [value]
end
}
end
The problem is that apparently I get an error when I do:
value.class == i[0].class
Which is kind of weird, because for me it makes sense. I'm new to ruby and I'd appreciate some insight.