-1

I'm pretty new to programming and i'm doing fundamentals on codewars.com and I'm having some trouble with this one. The objective is to take a group of integers, reverse them, and put them into an array. Here's my code. (I made the tf function to see what was going on in the code.)

def digitize(n)
  answer = n.to_s.split(//).reverse!
  def tf(it)
    str_test = it.is_a? String
    int_test = it.is_a? Integer
    puts "String: #{str_test}"
    puts "Integer: #{int_test}"
  end
  Array(answer)
  unless answer.is_a? Integer
    for item in answer
      item.to_i
      puts item
      tf(item)
    end
  end
  return answer
end

Sample test:

Test.assert_equals(digitize(35231),[1,3,2,5,3])

When tested, it returns:

1
String: true
Integer: false
3
String: true
Integer: false
2
String: true
Integer: false
5
String: true
Integer: false
3
String: true
Integer: false

Can one of you guys help me figure out where it goes wrong?

Zabieru
  • 1
  • 1
  • `to_i` does not mutate the object so `item.to_i` doesn't actually do anything in your application. This would need to be changed to `item = item.to_i`. Additionally `Array(answer)` also does nothing and `answer.is_a? Integer` will never be `true` and `str_test` will always be `false` once you cast and assign `item` using `item = item.to_i`. Oh yeah and your test will always fail. – engineersmnky May 14 '18 at 18:05
  • 1
    There's a lot of really *odd* Ruby code going on here. If you're learning Ruby I'd strongly recommend getting a reference book that has some consistency to it. An older version of the "[pickaxe book](http://ruby-doc.com/docs/ProgrammingRuby/)" is available online, the newer version [for sale](https://pragprog.com/book/ruby4/programming-ruby-1-9-2-0). This book uses idiomatic Ruby where conventions are clearly communicated. – tadman May 14 '18 at 18:14
  • "Can one of you guys help me figure out where it goes wrong?" – Can you narrow this down a bit more? To be honest, there is pretty much not a single line of code in there that makes sense, so it is a bit hard to answer that question with anything else except "Everywhere". It would be much better if you narrowed down your problem as much as possible into a [mcve] and ask about a concrete, specific problem and not "where is it going wrong". In fact, the output is 100% correct, so in some sense, nowhere is it going wrong. – Jörg W Mittag May 14 '18 at 19:18

1 Answers1

1

Assigning

item = item.to_i

Would fix the output in tf, but your returned answer would still be all strings. If you want to do this one by one like you're doing you would need to assign it back into the index of the array:

answer.each_with_index do |item, index|
  answer[index] = item.to_i
end

Though, an even better way to do this would be with map (returns a new array) or map! (in-place):

# return this line (make it the last line in the method) or make sure
# to re-assign answer
answer.map(&:to_i)
# or do this one to use `answer` later on with all integers.
answer.map!(&:to_i)

(See this question about that &:to_i syntax).

It should also be noted (maybe), that Rubyists in general don't like for loops and prefer each loops.

Also, the line:

Array(answer)

doesn't modify the answer in place, and returns it cast to an array, so the line is doing nothing:

a = "1"
Array(a) # => ["1"]
a # => "1"
a = Array(a) # => ["1"]
a # => ["1"]

You also, don't even need to do this, since answer is already an array from where you split it (You could also have used chars instead of split(//)). The line unless answer.is_a?(Integer) will thusly never be true.

The last major thing, I see is that in newer versions of ruby, there's a built-in method to do all this, digits:

 35231.digits # => [1, 3, 2, 5, 3]
Simple Lime
  • 10,790
  • 2
  • 17
  • 32