Topic: Really newbie stuff, i'm having trouble with one piece of code

I'm trying to write a simple program that shows you the stages of the 1089 thing. Where a 3 digit number, if reversed, minused from itself, then is reversed and added to itself you get 1089.

        puts 'This is the 1089, mathamagic calculator...'
        puts 'Please input any, YES ANY, 3 digit number, which is not a palindrome (010, 101, 454, 050... etc)'
        s = gets
                a = s.to_s[0..0]
                b = s.to_s[1..1]
                c = s.to_s[2..2]
                d = 100*a.to_i + 10*b.to_i + c.to_i
                e = d.to_i - (100*c.to_i + 10*b.to_i + a.to_i)
        If s.length != 4 && e != 0
        puts 'Please make sure it is not a palindrome and is 3 digits long'
        s = gets
                a = s.to_s[0..0]
                b = s.to_s[1..1]
                c = s.to_s[2..2]
                d = 100*a.to_i + 10*b.to_i + c.to_i
                e = d.to_i - (100*c.to_i + 10*b.to_i + a.to_i)
        end

a = s.to_s[0..0]
b = s.to_s[1..1]
c = s.to_s[2..2]
puts 'TEST'
puts a
puts b
puts c
d = 100*a.to_i + 10*b.to_i + c.to_i
puts 'So if we get ' + s.chomp + ' and minus the reverse of it, (' + (100*c.to_i + 10*b.to_i + a.to_i).to_s.chomp + '), from itself...'
e = d.to_i - (100*c.to_i + 10*b.to_i + a.to_i)
puts 'We get ' + e.to_s
    if e.to_i > 0
        f = e.to_s[0..0]
        g = e.to_s[1..1]
        h = e.to_s[2..2]
        puts 'TEST'
        puts f
        puts g
        puts h   
        i = 100*f.to_i + 10*g.to_i + h.to_i
        puts i
        j = i.to_i + (100*h.to_i + 10*g.to_i + f.to_i)
        puts j
        puts 'If we then get ' + e.to_s.chomp + ' and add it to the reverse of itself, (' + (100*h.to_i + 10*g.to_i + f.to_i).to_s.chomp + '), we get... '
        puts j
    else
        f = e.to_s[-1..-1]
        g = e.to_s[-2..-2]
        h = e.to_s[1..1]
        puts 'TEST'
        puts f
        puts g
        puts h
        i = -1*(100*f.to_i + 10*g.to_i + h.to_i)
        puts i
        j = e.to_i + i.to_i
        puts 'If we then get ' + e.to_s.chomp + ' and minus it from the reverse of itself, (' + i.to_s.chomp + '), we get... '
        puts j.to_s
end


I tried the IF at begining to until and while and many variations but i can't seem to get it so that, The original is not a palindrome (303, 101, 010) and IS 3 digits long.
I know about the sort of error of the 99, which i'll sort out once i can get the loop bit right at the begingin (if you type in 001... it will go to 99, and you have to do the second stage twice)

i know the method i chose is long, and i want it to be this way as i jsut want to get a feel for the programming.. i.e. i know i can just do s.reverse etc
but any help would be appreiciated
Cheers,
Jamie

Re: Really newbie stuff, i'm having trouble with one piece of code

I think you might be trying to fight why ruby is a good programming language. The code you wrote is very difficult to understand, and may make ruby a difficult language to use. Try:

def reverse(value)
  value.to_s.reverse.to_i
end

begin
  puts "Please enter a three digit non-palindrome (111, 232, etc.) number:"
  value = gets.chomp.to_i
end while not (100..999).include? value or value == reverse(value) 

regular_number = value
reverse_number = reverse(regular_number)
puts "The number entered is #{regular_number}, and the reverse is #{reverse_number}!"

absolute_difference_regular = (regular_number - reverse_number).abs
absolute_difference_reverse = reverse(absolute_difference_regular)
puts "The absolute difference is #{absolute_difference_regular} and the reverse is #{absolute_difference_reverse}!"

sum = absolute_difference_regular + absolute_difference_reverse
puts "The sum of the those two numbers is #{sum}! Now try some other numbers!"