Why Methods? Can anybody explain Prime method provided here


Hi I find that I not understand all of the code provided there, can anybody explain it?

def prime(n)
puts "That's not an integer." unless n.is_a? Integer
is_prime = true
for i in 2..n-1
if n % i == 0
is_prime = false
if is_prime
puts "#{n} is prime!"
puts "#{n} is not prime."

Especially I confused with this line: " for i in 2..n-1" what it does?
I find out that output will be the same if change that line to : "for i in 2...n"
So it's mean we don't need to divide by number itself because result obviously will be 1
And on line below we dividing all others numbers and see if result contains any remainder


This is a for statement that goes through a range of iterator variable values starting with 2, then one by one up to n-1. 2..n is a range.


n = 10
for i in 2..n-1
  print "#{i}, "
# 2, 3, 4, 5, 6, 7, 8, 9, 2..9

Notice that the last value in the range is included in the print out. We can ignore the last bit that is printed by the console, not our code. This is similar to what a JavaScript for statement would do. Consider the following:

var n = 10;
var u = [];
for (var i = 2; i < n; i++) {
console.log(u.join(', '));
// 2, 3, 4, 5, 6, 7, 8, 9

There is a slight difference but we can see the similarity, as well.