Check if num X is divisible by num Y I wrote some code for a course:


  Create a program that takes two numbers from the user then find out if the first is divisible by the second. If not divisible, let the user know what the remainder is.


Here is what I currently have. Is this right? If so, how could I improve it?

puts "Enter number one"
num1 = gets.chomp.to_i

puts "Enter number two"
num2 = gets.chomp.to_i

if num1 % num2 != 0
    p num1 % num2
else p "The numbers are divislbe."
end
Calculate the remainder once. Right now you calculate it to the comparison, and again if you need to output it.
Use .zero? for readable, idiomatic code 
Fix your linebreaks and indentation (don't put code on the same line as else
Consider what to do if the divisor is zero.
Optionally, extract the bit of logic that gets a number into a method


This is a (intentionally overwrought) example:

def get_int(message)
  puts message
  gets.chomp.to_i
end

numerator = get_int("Enter numerator")
divisor = get_int("Enter divisor")

if divisor.zero?
  puts "Can't divide by zero"
  exit
end

remainder = numerator % divisor

if remainder.zero?
  puts "#{numerator} is divisble by #{divisor}"
else
  puts "#{numerator} is not divisble by #{divisor}. Remainder is #{remainder}"
end