Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Datatype in Ruby Array or Other?

I'm having trouble determining what the datatype of the following variables are: A, B, C. Not too familiar with Regex or split so that could be part of the issue. The main issue here is that I cannot split C. Do I have to push the data from B to an array before that takes place and if so, how should I go about that?

ReadPath = File.read("ReadFile.txt")

A = ReadPath

B = A.split(/[\s,']/)

C = B.select.each_with_index { |str, i| i.even? }

C.split(/[\s,']/)

E = B.select.each_with_index { |str, i| i.odd? }

I'm guessing part of the problem is the datatype conversion:

A = string, B = string, C = array, .split is not an array method?

2 Answers

I recommend that you call

A.class, B.class, etc

This will tell you if it is a string, array, etc.

I'm guessing you are trying to get just the even indices, here's a good explanation:

http://stackoverflow.com/questions/1614147/odd-or-even-entries-in-a-ruby-array

Thanks so much Brandon!

I called class on A, B, C etc... and the split method changes the datatype from string to an array. But it is hard to imagine that there isn't another method like split used on arrays to do the same thing! I'm wondering what that is, or how I might improve my implementation...

And that is actually where I got the method for select by index, so excellent find if I do say so myself!