# Problem with MODULO function?

**URL:** <https://www.ruby-forum.com/t/problem-with-modulo-function/59289>\
**Category:** Ruby\
**Created:** [May 3, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/problem-with-modulo-function/59289 "2006-05-03T14:14:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/problem-with-modulo-function/59289/1 "2006-05-03T14:14:15Z")

</div>

The results of my little script here confuse me. I looking for an odd  
number of pages in postscript files. When it’s an odd number, I add a  
blank, to make an even page count. In my test here, I’ve got 2 test  
postscript files. One has 11 pages; the other has 42 pages. As can be  
seen by my results below this script, it’s seeing both as odd numbered,  
and, it’s adding a blank to each. Is my formula for MOD not correct?  
I’ve tried it with “not equal to zero,” like here, and, I’ve tried it  
with “equal to one,” with the same results.

Dir.chdir(‘c:/scripts/ruby/temp’)  
psfiles = Dir.glob(’\*.ps’)  
psfiles.each do |psfile|  
File.open(psfile, “a”) do |writepage|  
File.read(psfile).scan(/%%Pages: (\d{1,5})\n/) do  
numberofpages = $1  
#Diagnostic to see the page counts of each file.  
puts numberofpages  
#If the page count is odd, then,  
#add a blank to make it an even page count.  
if (numberofpages % 2) !=0 then  
#Diagnostic–what is it seeing?  
puts “odd number of pages”  
writepage \<\< “showpage\n”  
end  
end  
end  
end

C:\scripts\RUBY\temp\>c:\scripts\ruby\images\test4.rb  
42  
odd number of pages  
11  
odd number of pages

Thank you, anyone, who can help me with this. . . .

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/problem-with-modulo-function/59289/2 "2006-05-03T15:19:44Z")

</div>

Peter B. wrote:

> The results of my little script here confuse me. I looking for an odd  
> number of pages in postscript files. When it’s an odd number, I add a  
> blank, to make an even page count. In my test here, I’ve got 2 test  
> postscript files. One has 11 pages; the other has 42 pages. As can be  
> seen by my results below this script, it’s seeing both as odd numbered,  
> and, it’s adding a blank to each. Is my formula for MOD not correct?  
> I’ve tried it with “not equal to zero,” like here, and, I’ve tried it  
> with “equal to one,” with the same results.
> 
> Dir.chdir(‘c:/scripts/ruby/temp’)  
> psfiles = Dir.glob(’\*.ps’)  
> psfiles.each do |psfile|  
> File.open(psfile, “a”) do |writepage|  
> File.read(psfile).scan(/%%Pages: (\d{1,5})\n/) do  
> numberofpages = $1  
> #Diagnostic to see the page counts of each file.  
> puts numberofpages  
> #If the page count is odd, then,  
> #add a blank to make it an even page count.  
> if (numberofpages % 2) !=0 then  
> #Diagnostic–what is it seeing?  
> puts “odd number of pages”  
> writepage \<\< “showpage\n”  
> end  
> end  
> end  
> end

The variable numberofpages contains a String instance, not a number.  
You’re calling String#% which is the sprintf-like formatting operator,  
not Fixnum#% which is the modulo operator. So “42” % 2 is the String  
“42”, which is not going to be equal to numeric 0.

You want to convert the string to a number with

numberofpages = Integer( $1 )
